-
Notifications
You must be signed in to change notification settings - Fork 7
/
benchmark.go
42 lines (39 loc) · 1.32 KB
/
benchmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package html
import (
"bufio"
"fmt"
"io"
"net/http"
"runtime"
"time"
"github.com/Cloud-Foundations/Dominator/lib/wsyscall"
)
func benchmarkedHandler(handler func(io.Writer,
*http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
runtime.LockOSThread()
var startRusage, stopRusage wsyscall.Rusage
wsyscall.Getrusage(wsyscall.RUSAGE_THREAD, &startRusage)
startTime := time.Now()
writer := bufio.NewWriter(w)
defer writer.Flush()
defer fmt.Fprintln(writer, "</body>")
handler(writer, req)
durationReal := time.Since(startTime)
err := wsyscall.Getrusage(wsyscall.RUSAGE_THREAD, &stopRusage)
if err != nil {
fmt.Fprintf(writer,
"<br><font color=\"grey\">Render time: real: %s wbuf: %d B</font>\n",
durationReal, writer.Buffered())
return
}
var durationUser, durationSys int64
durationUser = (stopRusage.Utime.Sec - startRusage.Utime.Sec) * 1000000
durationUser += stopRusage.Utime.Usec - startRusage.Utime.Usec
durationSys = (stopRusage.Stime.Sec - startRusage.Stime.Sec) * 1000000
durationSys += stopRusage.Stime.Usec - startRusage.Stime.Usec
fmt.Fprintf(writer,
"<br><font color=\"grey\">Render time: real: %s, user: %d us, sys: %d us wbuf: %d B</font>\n",
durationReal, durationUser, durationSys, writer.Buffered())
}
}