-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
48 lines (42 loc) · 1.31 KB
/
main.go
File metadata and controls
48 lines (42 loc) · 1.31 KB
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
45
46
47
48
package main
import (
"fmt"
"net/http"
"os"
"net/http/pprof"
_ "net/http/pprof"
"runtime"
"time"
)
const (
port = ":9999"
)
var calls = 0
func sayHello(w http.ResponseWriter, r *http.Request) {
h, _ := os.Hostname()
calls++
fmt.Fprintf(w, "Hello! You have called me %d times from %s!", calls, h)
}
func main() {
go func() {
for {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Println("\nAlloc = ", m.Alloc / 1024)
fmt.Println("TotalAlloc = ", m.TotalAlloc / 1024)
fmt.Println("Sys = ", m.Sys / 1024)
fmt.Println("NumGC = ", m.NumGC)
time.Sleep(15 * time.Second)
}
}()
fmt.Println("Started http Server",port)
r := http.NewServeMux()
r.HandleFunc("/", sayHello)
// Register pprof handlers
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
http.ListenAndServe(port, r)
}