-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
173 lines (142 loc) · 3.85 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gocraft/web"
"github.com/mlctrez/gflamescope/gfutil"
"github.com/mlctrez/gflamescope/heatmap"
"github.com/mlctrez/gflamescope/stack"
"github.com/mlctrez/zipbackpack/httpfs"
)
var root string
type Context struct{}
func StackList(rw web.ResponseWriter, req *web.Request) {
// TODO: pluggable file storage
var fileNames []string
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// this assumes a single directory
if !info.IsDir() {
fileNames = append(fileNames, strings.Replace(path, root, "", 1)[1:])
}
return nil
})
json.NewEncoder(rw).Encode(&fileNames)
}
func parseForm(rw web.ResponseWriter, req *web.Request) bool {
if err := req.ParseForm(); err != nil {
log.Println(err)
rw.WriteHeader(http.StatusBadRequest)
return false
}
return true
}
func HeatMap(rw web.ResponseWriter, req *web.Request) {
// /heatmap/?filename=perf.stacks01&rows=50
if !parseForm(rw, req) {
return
}
filename := req.FormValue("filename")
absPath := filepath.Join(root, filename)
rows, err := strconv.Atoi(req.FormValue("rows"))
if err != nil {
rows = 50
}
file, err := os.Open(absPath)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
defer file.Close()
offsets := heatmap.GenerateOffsets(bufio.NewScanner(file))
hm := heatmap.GenerateHeatMap(offsets, rows)
json.NewEncoder(rw).Encode(&hm)
}
func Stack(rw web.ResponseWriter, req *web.Request) {
// /stack?filename=perf.stacks01&start=18.62&end=19.6
if !parseForm(rw, req) {
return
}
filename := req.FormValue("filename")
absPath := filepath.Join(root, filename)
file, err := os.Open(absPath)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
defer file.Close()
stackStart, stackEnd := stack.CalculateStackRange(bufio.NewScanner(file))
start, end := stackStart, stackEnd
if req.FormValue("end") != "" {
reqEnd := gfutil.MustParseFloat(req.FormValue("end"))
if (stackStart + reqEnd) > stackEnd {
fmt.Println("ERROR: ", stackStart, stackEnd, req.URL.String())
rw.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
return
}
end = stackStart + reqEnd
}
if req.FormValue("start") != "" {
reqStart := gfutil.MustParseFloat(req.FormValue("start"))
start = start + reqStart
if start > end {
fmt.Println("ERROR: ", stackStart, stackEnd, req.URL.String())
rw.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
return
}
}
// start and end are now the range that we want
file, err = os.Open(absPath)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
defer file.Close()
root := stack.CreateFlameGraph(file, start, end)
json.NewEncoder(rw).Encode(root)
}
func main() {
rp := flag.String("root", "examples", "directory (and sub directories) where perf files are located")
flag.Parse()
if fi, err := os.Stat(*rp); err != nil {
panic(err)
} else {
if !fi.IsDir() {
panic("root is not a directory")
}
absPath, err := filepath.Abs(*rp)
if err != nil {
panic(err)
}
root = absPath
}
router := web.New(Context{})
//option := web.StaticOption{IndexFile: "_index.html"}
router.Middleware(web.LoggerMiddleware)
sf, err := httpfs.NewStaticFileSystem("")
if err != nil {
panic(err)
}
mwf := web.StaticMiddlewareFromDir(sf)
router.Middleware(func(w web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
// http.FileServer does a permanent redirect of /index.html to /
// so for this path we serve /_index.html to avoid a redirect loop
if req.URL.Path == "/" {
req.URL.Path = "/_index.html"
}
mwf(w, req, next)
})
router.Get("/stack/list", StackList)
router.Get("/heatmap/", HeatMap)
router.Get("/stack", Stack)
http.ListenAndServe(":8080", router)
}