Skip to content

Commit bcf5a8a

Browse files
authored
Go benchmark test and fixed --cpu-profile option (deepnoodle-ai#257)
1 parent a4f6c54 commit bcf5a8a

File tree

7 files changed

+84
-12
lines changed

7 files changed

+84
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/risor
2+
/tmp*
13
.vscode
24
node_modules
35

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ test:
88
gotestsum --junitfile /tmp/test-reports/unit-tests.xml \
99
-- -coverprofile=coverage.out -covermode=atomic ./... ./cmd/risor/...
1010

11-
.PHONY: bench
12-
bench:
11+
.PHONY: pprof
12+
pprof:
1313
go build
14-
./risor -profile cpu.out ./benchmark/main.mon
14+
./risor --cpu-profile cpu.out ./examples/scripts/fibonacci.risor
1515
go tool pprof -http=:8080 ./cpu.out
1616

17+
.PHONY: bench
18+
bench:
19+
go test -bench=. -benchmem ./bench
20+
1721
# https://code.visualstudio.com/api/working-with-extensions/publishing-extension#packaging-extensions
1822
.PHONY: install-tools
1923
install-tools:

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ is already available which currently only offers syntax highlighting.
206206

207207
You can also make use of the [Risor TextMate grammar](./vscode/syntaxes/risor.grammar.json).
208208

209+
## Benchmarking
210+
211+
There are two Makefile commands that assist with benchmarking and CPU profiling:
212+
213+
```
214+
make bench
215+
make pprof
216+
```
217+
209218
## Contributing
210219

211220
Risor is intended to be a community project. You can lend a hand in various ways:

bench/bench_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package risor_test
2+
3+
import (
4+
"context"
5+
"log"
6+
"testing"
7+
8+
"github.com/risor-io/risor/compiler"
9+
"github.com/risor-io/risor/parser"
10+
"github.com/risor-io/risor/vm"
11+
)
12+
13+
func BenchmarkRisor_Fibonacci35(b *testing.B) {
14+
script := `
15+
func fibonacci(n) {
16+
if n <= 1 {
17+
return n
18+
}
19+
return fibonacci(n-1) + fibonacci(n-2)
20+
}
21+
fibonacci(35)
22+
`
23+
24+
ctx := context.Background()
25+
26+
ast, err := parser.Parse(ctx, script)
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
31+
code, err := compiler.Compile(ast)
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
b.ResetTimer()
37+
for i := 0; i < b.N; i++ {
38+
result, err := vm.Run(ctx, code)
39+
if err != nil {
40+
b.Fatal(err)
41+
}
42+
if result.Interface().(int64) != 9227465 {
43+
b.Fatalf("unexpected result: %v", result)
44+
}
45+
}
46+
}

cmd/risor/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"runtime/pprof"
89
"strings"
910
"time"
1011

@@ -132,6 +133,16 @@ var rootCmd = &cobra.Command{
132133
ctx := context.Background()
133134
processGlobalFlags()
134135

136+
if path := viper.GetString("cpu-profile"); path != "" {
137+
f, err := os.Create(path)
138+
if err != nil {
139+
fatal(err)
140+
}
141+
pprof.StartCPUProfile(f)
142+
defer pprof.StopCPUProfile()
143+
handleSigForProfiler()
144+
}
145+
135146
// Separate arguments belonging to the Risor CLI from those that are
136147
// to be passed to the script.
137148
var scriptArgs []string

cmd/risor/util.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,4 @@ func processGlobalFlags() {
173173
if viper.GetBool("no-color") {
174174
color.NoColor = true
175175
}
176-
if path := viper.GetString("cpu-profile"); path != "" {
177-
f, err := os.Create(path)
178-
if err != nil {
179-
fatal(err)
180-
}
181-
pprof.StartCPUProfile(f)
182-
defer pprof.StopCPUProfile()
183-
handleSigForProfiler()
184-
}
185176
}

examples/scripts/fibonacci.risor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
func fibonacci(n) {
3+
if n <= 1 {
4+
return n
5+
}
6+
return fibonacci(n-1) + fibonacci(n-2)
7+
}
8+
9+
fibonacci(35)

0 commit comments

Comments
 (0)