server/stats.go
crusader db8b6e524e ing
2017-10-26 21:08:50 +09:00

121 lines
2.9 KiB
Go

package server
import (
"io"
"sync"
"time"
)
type ConnStats struct {
// The number of request performed.
RequestCount uint64
// The total aggregate time for all request in milliseconds.
//
// This time can be used for calculating the average response time
// per request:
// avgRequesttime = RequestTime / RequestCount
RequestTime uint64
// The number of bytes written to the underlying connections.
BytesWritten uint64
// The number of bytes read from the underlying connections.
BytesRead uint64
// The number of Read() calls.
ReadCalls uint64
// The number of Read() errors.
ReadErrors uint64
// The number of Write() calls.
WriteCalls uint64
// The number of Write() errors.
WriteErrors uint64
// The number of Dial() calls.
DialCalls uint64
// The number of Dial() errors.
DialErrors uint64
// The number of Accept() calls.
AcceptCalls uint64
// The number of Accept() errors.
AcceptErrors uint64
// lock is for 386 builds. See https://github.com/valyala/gorpc/issues/5 .
lock sync.Mutex
}
// AvgRequestTime returns the average Request execution time.
//
// Use stats returned from ConnStats.Snapshot() on live Client and / or Server,
// since the original stats can be updated by concurrently running goroutines.
func (cs *ConnStats) AvgRequestTime() time.Duration {
return time.Duration(float64(cs.RequestTime)/float64(cs.RequestCount)) * time.Millisecond
}
// AvgRequestBytes returns the average bytes sent / received per Request.
//
// Use stats returned from ConnStats.Snapshot() on live Client and / or Server,
// since the original stats can be updated by concurrently running goroutines.
func (cs *ConnStats) AvgRequestBytes() (send float64, recv float64) {
return float64(cs.BytesWritten) / float64(cs.RequestCount), float64(cs.BytesRead) / float64(cs.RequestCount)
}
// AvgRequestCalls returns the average number of write() / read() syscalls per Request.
//
// Use stats returned from ConnStats.Snapshot() on live Client and / or Server,
// since the original stats can be updated by concurrently running goroutines.
func (cs *ConnStats) AvgRequestCalls() (write float64, read float64) {
return float64(cs.WriteCalls) / float64(cs.RequestCount), float64(cs.ReadCalls) / float64(cs.RequestCount)
}
type writerCounter struct {
w io.Writer
cs *ConnStats
}
type readerCounter struct {
r io.Reader
cs *ConnStats
}
func newWriterCounter(w io.Writer, cs *ConnStats) io.Writer {
return &writerCounter{
w: w,
cs: cs,
}
}
func newReaderCounter(r io.Reader, cs *ConnStats) io.Reader {
return &readerCounter{
r: r,
cs: cs,
}
}
func (w *writerCounter) Write(p []byte) (int, error) {
n, err := w.w.Write(p)
w.cs.incWriteCalls()
if err != nil {
w.cs.incWriteErrors()
}
w.cs.addBytesWritten(uint64(n))
return n, err
}
func (r *readerCounter) Read(p []byte) (int, error) {
n, err := r.r.Read(p)
r.cs.incReadCalls()
if err != nil {
r.cs.incReadErrors()
}
r.cs.addBytesRead(uint64(n))
return n, err
}