Skip to content

Instantly share code, notes, and snippets.

@WhisperingChaos
Last active February 11, 2022 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhisperingChaos/da17b8cb06d66d5d95a5285109b5af93 to your computer and use it in GitHub Desktop.
Save WhisperingChaos/da17b8cb06d66d5d95a5285109b5af93 to your computer and use it in GitHub Desktop.
Trace Latency Gaps
package main
import (
"net/http"
_ "net/http/pprof"
"time"
)
func init() {
go proListener()
}
func proListener() {
http.ListenAndServe(":2112", nil)
}
func main() {
sr := make(chan naught)
go msgSend(sr)
go msgReceive(sr)
select {}
}
func msgSend(
msg chan<- naught,
) {
for {
msg <- naught{}
}
}
func msgReceive(
msg <-chan naught,
) {
tkr := time.NewTicker(1 * time.Millisecond)
for {
<-msg
<-tkr.C
}
}
type naught struct{}
//go:build latencycompute
package main
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"time"
)
func main() {
programVersion()
sr := make(chan naught)
go msgSend(sr)
go msgReceive(sr)
select {}
}
var eBUILD_TAG string
func programVersion() {
gover := runtime.Version()
_, filePath, _, _ := runtime.Caller(0)
fileNm := filepath.Base(filePath)
fileNm = strings.TrimPrefix(strings.TrimSuffix(fileNm, ".go"), "main_")
fmt.Printf("GO_COMPILER_VERSION=%s\nBUILD_TAG=%s\n", gover, eBUILD_TAG)
}
func msgSend(
msg chan<- naught,
) {
for {
msg <- naught{}
}
}
func msgReceive(
msg <-chan naught,
) {
tkr := time.NewTicker(1 * time.Millisecond)
tkprior := time.Now()
for {
<-msg
tkcurr := <-tkr.C
tkdiff := tkcurr.Sub(tkprior)
if tkdiff > time.Duration(5*time.Millisecond) {
fmt.Printf("Latency %d \n", tkdiff)
}
tkprior = tkcurr
}
}
type naught struct{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment