forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-perf-dash.go
150 lines (129 loc) · 4.28 KB
/
node-perf-dash.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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"
)
// TODO(yguo0905): Put each component into its own package.
const (
// TODO(coufon): make them configurable?
pollDuration = 10 * time.Minute
errorDelay = 10 * time.Second
maxBuilds = 100
)
var (
addr = flag.String("address", ":8080", "The address to serve web data on")
www = flag.Bool("www", true, "If true, start a web-server to server performance data")
wwwDir = flag.String("dir", "www", "If non-empty, add a file server for this directory at the root of the web server")
builds = flag.Int("builds", maxBuilds, "Total builds number")
datasource = flag.String("datasource", "google-gcs", "Source of test data. Options include 'local', 'google-gcs'")
localDataDir = flag.String("local-data-dir", "", "The path to test data directory")
tracing = flag.Bool("tracing", false, "If true, try to get tracing data from Kubelet log")
jenkinsJob = flag.String("jenkins-job", "kubelet-benchmark-gce-e2e-ci", "The Jenkins projects to display, separated by ,")
)
var (
// allTestData stores all parsed perf and time series data in memeory
// for each job.
allTestData = map[string]TestToBuildData{}
// allTestInfo stores the test description of all tests.
allTestInfo = TestInfo{Info: make(map[string]string)}
)
func main() {
var (
downloader Downloader
jobs JobList
err error
)
fmt.Print("Starting Node Performance Dashboard...\n")
flag.Parse()
if *builds > maxBuilds || *builds < 0 {
fmt.Printf("Invalid builds number: %d", *builds)
*builds = maxBuilds
}
switch *datasource {
case "local":
downloader = NewLocalDownloader()
case "google-gcs":
downloader = NewGoogleGCSDownloader()
default:
fmt.Fprintf(os.Stderr, "Unsupported data source: %s.\n", *datasource)
os.Exit(1)
}
jobs = strings.Split(*jenkinsJob, ",")
fmt.Printf("The Jenkins jobs to display: %v\n", jobs)
// Initialize test result map.
for _, job := range jobs {
allTestData[job] = TestToBuildData{}
}
// Grab test results but not start webserver.
if !*www {
for _, job := range jobs {
err = Parse(allTestData, &allTestInfo, job, downloader)
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching data: %v\n", err)
os.Exit(1)
}
prettyResult, err := json.MarshalIndent(allTestData, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error formating data: %v\n", err)
os.Exit(1)
}
fmt.Printf("Result: %v\n", string(prettyResult))
}
return
}
// Create a data collection goroutine for each Jenkins Job.
for _, job := range jobs {
// TODO(coufon): currently we do not need lock, but be aware of that.
go func(job string) {
for {
fmt.Printf("Fetching new data...\n")
err = Parse(allTestData, &allTestInfo, job, downloader)
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching data: %v\n", err)
time.Sleep(errorDelay)
continue
}
time.Sleep(pollDuration)
}
}(job)
}
// Create a http handler for each Jenkins Job.
for _, job := range jobs {
http.Handle(fmt.Sprintf("/data/%s", job), allTestData[job])
}
http.Handle("/testinfo", &allTestInfo)
http.Handle("/jobs", &jobs)
http.Handle("/", http.FileServer(http.Dir(*wwwDir)))
http.ListenAndServe(*addr, nil)
}
// JobList is the list containing all Jenkins projects.
type JobList []string
func (j *JobList) ServeHTTP(res http.ResponseWriter, req *http.Request) {
data, err := json.Marshal(j)
if err != nil {
res.Header().Set("Content-type", "text/html")
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(fmt.Sprintf("<h3>Internal Error</h3><p>%v", err)))
return
}
res.Header().Set("Content-type", "application/json")
res.WriteHeader(http.StatusOK)
res.Write(data)
}