Skip to content

Commit

Permalink
echosrv: Log request rate to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenart committed Aug 25, 2018
1 parent 68e46ad commit 0d315c8
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/cmd/echosrv/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package main

import (
"log"
"net/http"
"net/http/httputil"
"os"
"sync/atomic"
"time"
)

func main() {
count := uint64(0)
go func(last time.Time) {
ticks := time.Tick(time.Second)
for range ticks {
rate := float64(atomic.SwapUint64(&count, 0)) / time.Since(last).Seconds()
last = time.Now()
log.Printf("Rate: %.3f/s", rate)
}
}(time.Now())

http.ListenAndServe(os.Args[1], http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddUint64(&count, 1)
bs, _ := httputil.DumpRequest(r, true)
w.Write(bs)
}))
Expand Down

0 comments on commit 0d315c8

Please sign in to comment.