-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
44 lines (35 loc) · 1.11 KB
/
examples_test.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
43
44
package pprofhijackmiddleware_test
import (
"fmt"
"log"
"net/http"
pprofhijackmiddleware "github.com/miguelcnf/go-pprof-hijack-middleware"
)
var (
customHandler = func(w http.ResponseWriter, r *http.Request) {}
helloHandler = func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "hello")
}
)
func ExampleCPUProfile() {
mux := http.NewServeMux()
// regular handler, not hijacked
mux.Handle("/hello", http.HandlerFunc(helloHandler))
// hijack a single handler
cpuProfileHijackHandler := pprofhijackmiddleware.CPUProfile(http.HandlerFunc(customHandler))
mux.Handle("/custom", cpuProfileHijackHandler)
err := http.ListenAndServe(":8080", mux)
if err != http.ErrServerClosed {
log.Fatalf("server closed unexpectedly: %v", err)
}
}
func ExampleMemProfile() {
mux := http.NewServeMux()
mux.Handle("/hello", http.HandlerFunc(helloHandler))
mux.Handle("/custom", http.HandlerFunc(customHandler))
// hijack all registered handlers
err := http.ListenAndServe(":8080", pprofhijackmiddleware.MemProfile(mux))
if err != http.ErrServerClosed {
log.Fatalf("server closed unexpectedly: %v", err)
}
}