2016-11-30 03:28:40 +00:00
|
|
|
package paasio
|
|
|
|
|
2017-08-15 14:55:28 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2016-11-30 03:28:40 +00:00
|
|
|
const testVersion = 3
|
|
|
|
|
2017-08-15 14:55:28 +00:00
|
|
|
func NewReadCounter(r io.Reader) ReadCounter {
|
|
|
|
return &readCounter{
|
|
|
|
r: r,
|
|
|
|
lock: new(sync.Mutex),
|
|
|
|
}
|
2016-11-30 03:28:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 14:55:28 +00:00
|
|
|
type readCounter struct {
|
|
|
|
r io.Reader
|
|
|
|
bytesRead int64
|
|
|
|
ops int
|
|
|
|
lock *sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *readCounter) Read(p []byte) (int, error) {
|
|
|
|
m, err := rc.r.Read(p)
|
|
|
|
rc.lock.Lock()
|
|
|
|
rc.bytesRead += int64(m)
|
|
|
|
rc.ops++
|
|
|
|
rc.lock.Unlock()
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *readCounter) ReadCount() (n int64, ops int) {
|
|
|
|
rc.lock.Lock()
|
|
|
|
n, ops = rc.bytesRead, rc.ops
|
|
|
|
rc.lock.Unlock()
|
|
|
|
return n, ops
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWriteCounter(w io.Writer) WriteCounter {
|
|
|
|
return &writeCounter{
|
|
|
|
w: w,
|
|
|
|
lock: new(sync.Mutex),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeCounter struct {
|
|
|
|
w io.Writer
|
|
|
|
bytesWrote int64
|
|
|
|
ops int
|
|
|
|
lock *sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *writeCounter) Write(p []byte) (int, error) {
|
|
|
|
m, err := wc.w.Write(p)
|
|
|
|
wc.lock.Lock()
|
|
|
|
wc.bytesWrote += int64(m)
|
|
|
|
wc.ops++
|
|
|
|
wc.lock.Unlock()
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *writeCounter) WriteCount() (n int64, ops int) {
|
|
|
|
wc.lock.Lock()
|
|
|
|
n, ops = wc.bytesWrote, wc.ops
|
|
|
|
wc.lock.Unlock()
|
|
|
|
return n, ops
|
2016-11-30 03:28:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 14:55:28 +00:00
|
|
|
type ReadWriter interface {
|
|
|
|
io.Reader
|
|
|
|
io.Writer
|
2016-11-30 03:28:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 14:55:28 +00:00
|
|
|
func NewReadWriteCounter(rw ReadWriter) ReadWriteCounter {
|
|
|
|
return &readWriteCounter{
|
|
|
|
r: NewReadCounter(rw),
|
|
|
|
w: NewWriteCounter(rw),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type readWriteCounter struct {
|
|
|
|
r ReadCounter
|
|
|
|
w WriteCounter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *readWriteCounter) Read(p []byte) (int, error) {
|
|
|
|
return rw.r.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *readWriteCounter) Write(p []byte) (int, error) {
|
|
|
|
return rw.w.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *readWriteCounter) ReadCount() (n int64, ops int) {
|
|
|
|
return rw.r.ReadCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *readWriteCounter) WriteCount() (n int64, ops int) {
|
|
|
|
return rw.w.WriteCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nr *nopReader) ReadCount() (n int64, ops int) {
|
2016-11-30 03:28:40 +00:00
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
2017-08-15 14:55:28 +00:00
|
|
|
func (nw *nopWriter) WriteCount() (n int64, ops int) {
|
|
|
|
return 0, 0
|
2016-11-30 03:28:40 +00:00
|
|
|
}
|