forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpvar.go
48 lines (42 loc) · 1.07 KB
/
expvar.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
45
46
47
48
package debugserver
import (
"expvar"
"fmt"
"net/http"
"runtime"
"runtime/debug"
"time"
)
// expvarHandler is copied from package expvar and exported so that it
// can be mounted on any ServeMux, not just http.DefaultServeMux.
func expvarHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintln(w, "{")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintln(w, ",")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintln(w, "\n}")
}
func gcHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "only POST is supported", http.StatusMethodNotAllowed)
return
}
t0 := time.Now()
runtime.GC()
fmt.Fprintf(w, "GC took %s\n", time.Since(t0))
}
func freeOSMemoryHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "only POST is supported", http.StatusMethodNotAllowed)
return
}
t0 := time.Now()
debug.FreeOSMemory()
fmt.Fprintf(w, "FreeOSMemory took %s\n", time.Since(t0))
}