~shimon/forgeperf

ref: 48cf20c9d0c2e2b03ac21eaccad4737f6923f628 forgeperf/main.go -rw-r--r-- 4.9 KiB
48cf20c9Drew DeVault Update patchset discussion pages 2 years ago
                                                                                
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"math"
	"os"
	"os/exec"
	"sort"
	"time"

	"github.com/russross/blackfriday"
	"gopkg.in/yaml.v2"

	"git.sr.ht/~sircmpwn/forgeperf/schema"
)

func runReport(host, task, url string) *schema.LighthouseReport {
	if url == "" {
		return &schema.LighthouseReport{
			RequestedURL: url,
			Host:         host,
			Score:        -1,
		}
	}
	var (
		attempts int
		err      error = errors.New("...")
		f        *os.File
	)
	for err != nil && attempts < 3 {
		if attempts != 0 {
			log.Printf("...attempt %d... (%v)", attempts + 1, err)
		}
		f, err = os.Create(fmt.Sprintf("report/results/%s.%s.log", host, task))
		if err != nil {
			panic(err)
		}
		cmd := exec.Command("lighthouse", url,
			"--only-categories=performance,accessibility",
			"--emulated-form-factor=none",
			"--throttling.cpuSlowdownMultiplier=1",
			"--output=html",
			"--output=json",
			fmt.Sprintf("--output-path=report/results/%s.%s", host, task))
		cmd.Env = nil
		cmd.Stderr = f
		err = cmd.Run()
		f, err = os.Open(fmt.Sprintf("report/results/%s.%s.report.json", host, task))
		attempts += 1
	}

	if err != nil {
		return &schema.LighthouseReport{
			RequestedURL: url,
			Error:        err,
			Host:         host,
			Log:          fmt.Sprintf("results/%s.%s.log", host, task),
			Score:        0,
		}
	}

	data, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}

	report := &schema.LighthouseReport{}
	err = json.Unmarshal(data, report)
	if err != nil {
		panic(err)
	}
	report.Host = host
	report.RawData = fmt.Sprintf("results/%s.%s.report.json", host, task)
	report.HTMLData = fmt.Sprintf("results/%s.%s.report.html", host, task)
	report.Log = fmt.Sprintf("results/%s.%s.log", host, task)
	if report.Categories.Performance.Score != nil {
		report.Score = *report.Categories.Performance.Score
	}
	return report
}

func main() {
	if err := os.MkdirAll("report/results", 0755); err != nil {
		panic(err)
	}

	f, err := os.Open("spec.yml")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	data, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}

	spec := &schema.Spec{}
	err = yaml.Unmarshal(data, spec)
	if err != nil {
		panic(err)
	}

	for _, cat := range spec.Categories {
		log.Printf("Running reports for '%s'", cat.Name)
		for _, task := range cat.Tasks {
			log.Printf("Task: %s", task.Name)
			for host, url := range task.URLs {
				log.Println("->", host, url)
				task.Reports = append(task.Reports, runReport(
					host, task.ID, url))
			}
			sort.Slice(task.Reports, func(a, b int) bool {
				return task.Reports[a].Score > task.Reports[b].Score
			})
		}
	}

	out, err := os.Create("report/index.html")
	if err != nil {
		panic(err)
	}
	defer out.Close()

	templ := template.New("index.html").
		Funcs(map[string]interface{}{
			"score": func(in *float64) string {
				if in == nil {
					return "?"
				}
				return fmt.Sprintf("%.f", *in * 100.0)
			},
			"overallScore": func(task *schema.LighthouseReport) string {
				// We need a separate function because an error should be
				// considered a score of zero, but go templates don't handle
				// nil very well.
				if task.Categories.Performance.Score == nil {
					if task.RuntimeError.Message != "" {
						return "0"
					}
					return "?"
				}
				return fmt.Sprintf("%.f", *task.Categories.Performance.Score * 100.0)
			},
			"class": func(in *float64) string {
				if in == nil {
					return "grey"
				}
				if *in >= 0.9 {
					return "green"
				} else if *in >= 0.5 && *in < 0.9 {
					return "orange"
				} else {
					return "red"
				}
			},
			"overallClass": func(task *schema.LighthouseReport) string {
				if task.Categories.Performance.Score == nil {
					if task.RuntimeError.Message != "" {
						return "red"
					}
					return "grey"
				}
				in := *task.Categories.Performance.Score
				if in >= 0.9 {
					return "green"
				} else if in >= 0.5 && in < 0.9 {
					return "orange"
				} else {
					return "red"
				}
			},
			"kib": func(in int) string {
				return fmt.Sprintf("%.f KiB", math.Round(float64(in) / 1024.0))
			},
			"markdown": func(in string) template.HTML {
				return template.HTML(blackfriday.MarkdownCommon([]byte(in)))
			},
			"byhost": func(tasks []*schema.LighthouseReport) []*schema.LighthouseReport {
				t := append([]*schema.LighthouseReport(nil), tasks...)
				sort.Slice(t, func(a, b int) bool {
					return t[a].Host < t[b].Host
				})
				return t
			},
		})
	_, err = templ.ParseFiles("index.html")
	if err != nil {
		panic(err)
	}

	lastUpdate := time.Now().UTC().Format("January 2, 2006 at 15:04 UTC")
	err = templ.Execute(out, struct {
		Spec              *schema.Spec
		LastUpdate        string
		LighthouseVersion string
	}{
		Spec:              spec,
		LastUpdate:        lastUpdate,
		LighthouseVersion: spec.Categories[0].Tasks[0].Reports[0].LighthouseVersion,
	})
	if err != nil {
		panic(err)
	}

	exec.Command("cp", "index.css", "report").Run()

	log.Println("Done.")
}