/
benchmark.go
37 lines (33 loc) · 1.07 KB
/
benchmark.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
package commands
import (
"fmt"
"time"
"github.com/montanaflynn/stats"
)
var benchmark = app.Command("benchmark", "Repeat build for ten seconds. Implies --profile.")
// benchmarkCommand builds the site repeatedly until at least 10 seconds has elapsed,
// and reports the trial times. Empirically, it the same mean but low variance as using
// a separate benchmark runner that invokes a new gojekyll process each time.
func benchmarkCommand() (err error) {
startTime := time.Now()
var samples []float64
for i := 0; time.Since(startTime) < 10*time.Second; i++ {
sampleStart := time.Now()
site, err := loadSite(*source, options)
if err != nil {
return err
}
_, err = site.Write()
if err != nil {
return err
}
dur := time.Since(sampleStart).Seconds()
samples = append(samples, dur)
quiet = true
fmt.Printf("Run #%d; %.1fs elapsed\n", i+1, time.Since(commandStartTime).Seconds())
}
median, _ := stats.Median(samples)
stddev, _ := stats.StandardDeviationSample(samples)
fmt.Printf("%d samples @ %.2fs ± %.2fs\n", len(samples), median, stddev)
return nil
}