2017-10-26 11:14:00 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-11-01 05:22:52 +00:00
|
|
|
type ServerStats struct {
|
2017-10-26 11:14:00 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2017-11-01 05:22:52 +00:00
|
|
|
// Use stats returned from ServerStats.Snapshot() on live Client and / or Server,
|
2017-10-26 11:14:00 +00:00
|
|
|
// since the original stats can be updated by concurrently running goroutines.
|
2017-11-01 05:22:52 +00:00
|
|
|
func (ss *ServerStats) AvgRequestTime() time.Duration {
|
|
|
|
return time.Duration(float64(ss.RequestTime)/float64(ss.RequestCount)) * time.Millisecond
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AvgRequestBytes returns the average bytes sent / received per Request.
|
|
|
|
//
|
2017-11-01 05:22:52 +00:00
|
|
|
// Use stats returned from ServerStats.Snapshot() on live Client and / or Server,
|
2017-10-26 11:14:00 +00:00
|
|
|
// since the original stats can be updated by concurrently running goroutines.
|
2017-11-01 05:22:52 +00:00
|
|
|
func (ss *ServerStats) AvgRequestBytes() (send float64, recv float64) {
|
|
|
|
return float64(ss.BytesWritten) / float64(ss.RequestCount), float64(ss.BytesRead) / float64(ss.RequestCount)
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AvgRequestCalls returns the average number of write() / read() syscalls per Request.
|
|
|
|
//
|
2017-11-01 05:22:52 +00:00
|
|
|
// Use stats returned from ServerStats.Snapshot() on live Client and / or Server,
|
2017-10-26 11:14:00 +00:00
|
|
|
// since the original stats can be updated by concurrently running goroutines.
|
2017-11-01 05:22:52 +00:00
|
|
|
func (ss *ServerStats) AvgRequestCalls() (write float64, read float64) {
|
|
|
|
return float64(ss.WriteCalls) / float64(ss.RequestCount), float64(ss.ReadCalls) / float64(ss.RequestCount)
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type writerCounter struct {
|
|
|
|
w io.Writer
|
2017-11-01 05:22:52 +00:00
|
|
|
ss *ServerStats
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type readerCounter struct {
|
|
|
|
r io.Reader
|
2017-11-01 05:22:52 +00:00
|
|
|
ss *ServerStats
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 05:22:52 +00:00
|
|
|
func newWriterCounter(w io.Writer, ss *ServerStats) io.Writer {
|
2017-10-26 11:14:00 +00:00
|
|
|
return &writerCounter{
|
|
|
|
w: w,
|
2017-11-01 05:22:52 +00:00
|
|
|
ss: ss,
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 05:22:52 +00:00
|
|
|
func newReaderCounter(r io.Reader, ss *ServerStats) io.Reader {
|
2017-10-26 11:14:00 +00:00
|
|
|
return &readerCounter{
|
|
|
|
r: r,
|
2017-11-01 05:22:52 +00:00
|
|
|
ss: ss,
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writerCounter) Write(p []byte) (int, error) {
|
|
|
|
n, err := w.w.Write(p)
|
2017-11-01 05:22:52 +00:00
|
|
|
w.ss.incWriteCalls()
|
2017-10-26 11:14:00 +00:00
|
|
|
if err != nil {
|
2017-11-01 05:22:52 +00:00
|
|
|
w.ss.incWriteErrors()
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
2017-11-01 05:22:52 +00:00
|
|
|
w.ss.addBytesWritten(uint64(n))
|
2017-10-26 11:14:00 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readerCounter) Read(p []byte) (int, error) {
|
|
|
|
n, err := r.r.Read(p)
|
2017-11-01 05:22:52 +00:00
|
|
|
r.ss.incReadCalls()
|
2017-10-26 11:14:00 +00:00
|
|
|
if err != nil {
|
2017-11-01 05:22:52 +00:00
|
|
|
r.ss.incReadErrors()
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
2017-11-01 05:22:52 +00:00
|
|
|
r.ss.addBytesRead(uint64(n))
|
2017-10-26 11:14:00 +00:00
|
|
|
return n, err
|
|
|
|
}
|