github.com/haraldrudell/parl@v0.4.176/parl_bench_test.go (about) 1 /* 2 © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package parl 7 8 import ( 9 "io/fs" 10 "os" 11 "os/exec" 12 "path" 13 "testing" 14 ) 15 16 var noParl = ` 17 package main 18 func main() {} 19 ` 20 21 var withParl = ` 22 package main 23 import "github.com/haraldrudell/parl" 24 func main() { _ = parl.ErrEndCallbacks } 25 ` 26 27 var withBtree = ` 28 package main 29 import "github.com/google/btree" 30 func main() { _ = btree.Ordered } 31 ` 32 33 // 67% of parl parse-time is caused by btree 34 // 35 // 231125 c66 36 // Running tool: /opt/homebrew/bin/go test -benchmem -run=^$ -bench ^BenchmarkParl$ github.com/haraldrudell/parl 37 // goos: darwin 38 // goarch: arm64 39 // pkg: github.com/haraldrudell/parl 40 // BenchmarkParl/no-parl.go-10 2454 484985 ns/op 5200 B/op 24 allocs/op 41 // BenchmarkParl/parl.go-10 2486 515141 ns/op 30156 parl-ns/op 5202 B/op 24 allocs/op 42 // BenchmarkParl/btree.go-10 2467 505130 ns/op 20145 btree-ns/op 5203 B/op 24 allocs/op 43 // PASS 44 // ok github.com/haraldrudell/parl 4.940s 45 func BenchmarkParl(b *testing.B) { 46 var benchs = []struct{ filename, metric, code string }{ 47 {"no-parl.go", "", noParl}, 48 {"parl.go", "parl-ns/op", withParl}, 49 {"btree.go", "btree-ns/op", withBtree}, 50 } 51 var uwx fs.FileMode = 0700 52 var d = b.TempDir() 53 var noParlMetric float64 54 for _, bench := range benchs { 55 var execCmd = exec.Cmd{Path: path.Join(d, bench.filename)} 56 os.WriteFile(execCmd.Path, []byte(bench.code), uwx) 57 if bench.metric != "" { 58 var preload = execCmd 59 preload.Run() // pre-run to ensure parl is stored in ~/go 60 } 61 b.Run(bench.filename, func(b *testing.B) { 62 var x exec.Cmd 63 for i := 0; i < b.N; i++ { 64 x = execCmd 65 x.Run() 66 } 67 b.StopTimer() 68 var elapsed = float64(b.Elapsed()) / float64(b.N) 69 if nUnit := bench.metric; nUnit == "" { 70 noParlMetric = elapsed 71 } else { 72 b.ReportMetric(elapsed-noParlMetric, nUnit) 73 } 74 }) 75 } 76 }