fork of https://github.com/sourcegraph/zoekt
1package debugserver
2
3import (
4 "expvar"
5 "fmt"
6 "net/http"
7 "runtime"
8 "runtime/debug"
9 "time"
10)
11
12// expvarHandler is copied from package expvar and exported so that it
13// can be mounted on any ServeMux, not just http.DefaultServeMux.
14func expvarHandler(w http.ResponseWriter, r *http.Request) {
15 w.Header().Set("Content-Type", "application/json; charset=utf-8")
16 fmt.Fprintln(w, "{")
17 first := true
18 expvar.Do(func(kv expvar.KeyValue) {
19 if !first {
20 fmt.Fprintln(w, ",")
21 }
22 first = false
23 fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
24 })
25 fmt.Fprintln(w, "\n}")
26}
27
28func gcHandler(w http.ResponseWriter, r *http.Request) {
29 if r.Method != "POST" {
30 http.Error(w, "only POST is supported", http.StatusMethodNotAllowed)
31 return
32 }
33
34 t0 := time.Now()
35 runtime.GC()
36 fmt.Fprintf(w, "GC took %s\n", time.Since(t0))
37}
38
39func freeOSMemoryHandler(w http.ResponseWriter, r *http.Request) {
40 if r.Method != "POST" {
41 http.Error(w, "only POST is supported", http.StatusMethodNotAllowed)
42 return
43 }
44
45 t0 := time.Now()
46 debug.FreeOSMemory()
47 fmt.Fprintf(w, "FreeOSMemory took %s\n", time.Since(t0))
48}