Skip to content

Latest commit

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

GoDoclicense

Stopwatch

A small library to measure latency of things.

It can measure:

  1. Arbitrary closure's latency.

  2. Request latency via middleware pattern.

Five Minutes Tutorial

1. Closure

package main
import (
"fmt""github.com/didip/stopwatch"
)
funcmain() {
a:=1f:=func() {
fori:=1; i<=10; i++ {
a=a+1
}
}
latency:=stopwatch.Measure(f)
fmt.Printf("Latency in nanoseconds: %v, Result: %v\n", latency, a)
}

2. Middleware

package main
import (
"fmt""github.com/didip/stopwatch""net/http"
)
funcHelloHandler(w http.ResponseWriter, req*http.Request) {
w.Write([]byte("Hello, World!"))
}
funcmain() {
// 1. Create a channel to receive latency resulthelloHandlerLatencyChan:=make(chanint64)
// 2. Pull latency result asynchronously.gofunc() {
for {
select {
caselatency:=<-helloHandlerLatencyChan:
fmt.Printf("Latency of HelloHandler in nanoseconds: %v\n", latency)
}
}
}()
fmt.Println("Starting HTTP server on :12345")
http.Handle("/", stopwatch.LatencyFuncHandler(helloHandlerLatencyChan, []string{"GET"}, HelloHandler))
http.ListenAndServe(":12345", nil)
}

My other Go libraries

  • Tollbooth: Simple middleware to rate-limit HTTP requests.

  • Gomet: Simple HTTP client & server long poll library for Go. Useful for receiving live updates without needing Websocket.

About

A small library to measure latency of things.

Resources

Stars

35 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages