Reporting Real-time Performance of Channels
Go ships with great tools for runtime performance monitoring. However, for one of the projects I am working on I needed tailored metrics around three channels that do the primary communication between the larger components of the processing service.
I wanted to be able to print realtime stats on movement and blocking of these channels only each minute. So I created ChannelPerf
:
https://gist.github.com/elliotchance/7db9eb40a935f5526b62f0bcf77b7d2c
How does it work?
For the most part it is a drop in replacement-ish with how you would normally use a channel:
// ch := make(chan interface{}, 1)
ch := NewChannelPerf(1, "Some name")
// ch <- "foo"
ch.Send("foo")
// next, ok := <-ch
next, ok := ch.Receive()
// close(ch)
ch.Close()
// for next := range ch {
// fmt.Println(next)
// }
for {
next, ok := ch.Receive()
if !ok {
break
}
fmt.Println(next)
}
The name (second parameter of NewChannelPerf
) is used to label statistics with ch.String()
:
fmt.Println(ch)
Displays something like:
Some name (clock: 1.387381ms, sent: 3 in 1.745µs, received: 3 in 1.268469ms)
clock
is how long it has been tracking statistics. Statistics can…