Skip to content

Commit ef30c20

Browse files
committed
feat: add examples
1 parent 82d977b commit ef30c20

File tree

8 files changed

+190
-0
lines changed

8 files changed

+190
-0
lines changed

example-codspeed/cli/runner.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//go:build codspeed
2+
// +build codspeed
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"io"
9+
"reflect"
10+
"time"
11+
12+
example "example"
13+
14+
codspeed_testing "github.com/AvalancheHQ/codspeed-go/testing/testing"
15+
)
16+
17+
type corpusEntry = struct {
18+
Parent string
19+
Path string
20+
Data []byte
21+
Values []any
22+
Generation int
23+
IsSeed bool
24+
}
25+
26+
type simpleDeps struct{}
27+
28+
func (d simpleDeps) ImportPath() string { return "" }
29+
func (d simpleDeps) MatchString(pat, str string) (bool, error) { return true, nil }
30+
func (d simpleDeps) SetPanicOnExit0(bool) {}
31+
func (d simpleDeps) StartCPUProfile(io.Writer) error { return nil }
32+
func (d simpleDeps) StopCPUProfile() {}
33+
func (d simpleDeps) StartTestLog(io.Writer) {}
34+
func (d simpleDeps) StopTestLog() error { return nil }
35+
func (d simpleDeps) WriteProfileTo(string, io.Writer, int) error { return nil }
36+
37+
func (d simpleDeps) CoordinateFuzzing(
38+
fuzzTime time.Duration,
39+
fuzzN int64,
40+
minimizeTime time.Duration,
41+
minimizeN int64,
42+
parallel int,
43+
corpus []corpusEntry,
44+
types []reflect.Type,
45+
corpusDir,
46+
cacheDir string,
47+
) error {
48+
return nil
49+
}
50+
func (d simpleDeps) RunFuzzWorker(fn func(corpusEntry) error) error {
51+
return nil
52+
}
53+
func (d simpleDeps) ReadCorpus(dir string, types []reflect.Type) ([]corpusEntry, error) {
54+
return nil, nil
55+
}
56+
func (d simpleDeps) CheckCorpus(vals []any, types []reflect.Type) error {
57+
return nil
58+
}
59+
func (d simpleDeps) ResetCoverage() {}
60+
func (d simpleDeps) SnapshotCoverage() {}
61+
func (d simpleDeps) InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
62+
return "", nil, nil
63+
}
64+
65+
func main() {
66+
var tests = []codspeed_testing.InternalTest{}
67+
var fuzzTargets = []codspeed_testing.InternalFuzzTarget{}
68+
var examples = []codspeed_testing.InternalExample{}
69+
var benchmarks = []codspeed_testing.InternalBenchmark{
70+
{
71+
Name: "BenchmarkFibonacci10",
72+
F: example.BenchmarkFibonacci10,
73+
},
74+
{
75+
Name: "BenchmarkFibonacci20",
76+
F: example.BenchmarkFibonacci20,
77+
},
78+
}
79+
80+
for i := 0; i < len(benchmarks); i++ {
81+
fmt.Printf("Benchmark %d: %s\n", i, benchmarks[i].Name)
82+
}
83+
84+
m := codspeed_testing.MainStart(simpleDeps{}, tests, benchmarks, fuzzTargets, examples)
85+
m.Run()
86+
}

example-codspeed/fib.go

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

example-codspeed/fib_codspeed.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package example
2+
3+
import (
4+
testing "github.com/AvalancheHQ/codspeed-go/compat/testing"
5+
)
6+
7+
func BenchmarkFibonacci10(b *testing.B) {
8+
// b.Run("fibonacci(40)", func(b *testing.B) {
9+
// for i := 0; i < b.N; i++ {
10+
// fibonacci(10)
11+
// }
12+
// })
13+
// b.Run("fibonacci(20)", func(b *testing.B) {
14+
// for i := 0; i < b.N; i++ {
15+
// fibonacci(20)
16+
// }
17+
// })
18+
b.RunParallel(func(b *testing.PB) {
19+
for b.Next() {
20+
fibonacci(30)
21+
}
22+
})
23+
}
24+
25+
func BenchmarkFibonacci20(b *testing.B) {
26+
for b.Loop() {
27+
fibonacci(30)
28+
}
29+
}
30+
31+
// func BenchmarkFibonacci30(b *testing.B) {
32+
// for i := 0; i < b.N; i++ {
33+
// fibonacci(30)
34+
// }
35+
// }

example-codspeed/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module example
2+
3+
go 1.24.3
4+
5+
require github.com/AvalancheHQ/codspeed-go v0.0.0
6+
7+
replace github.com/AvalancheHQ/codspeed-go => ..

example/fib.go

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

example/fib_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package example
2+
3+
import testing "github.com/AvalancheHQ/codspeed-go/compat/testing"
4+
5+
func BenchmarkFibonacci10(b *testing.B) {
6+
for i := 0; i < b.N; i++ {
7+
fibonacci(10)
8+
}
9+
10+
b.Run("fibonacci(40)", func(b *testing.B) {
11+
for i := 0; i < b.N; i++ {
12+
fibonacci(10)
13+
}
14+
})
15+
}
16+
17+
func BenchmarkFibonacci20(b *testing.B) {
18+
for b.Loop() {
19+
fibonacci(20)
20+
}
21+
}
22+
23+
func BenchmarkFibonacci30(b *testing.B) {
24+
b.Run("fibonacci(30)", func(b *testing.B) {
25+
b.Run("fibonacci(30)", func(b *testing.B) {
26+
b.Run("fibonacci(30)", func(b *testing.B) {
27+
for i := 0; i < b.N; i++ {
28+
fibonacci(30)
29+
}
30+
})
31+
})
32+
})
33+
}

example/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module example
2+
3+
go 1.24.3
4+
5+
require github.com/AvalancheHQ/codspeed-go v0.0.0
6+
7+
replace github.com/AvalancheHQ/codspeed-go => ..

example/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package example
2+
3+
func main() {
4+
// This is a placeholder for the main function.
5+
// The actual implementation will depend on the specific requirements of the application.
6+
}

0 commit comments

Comments
 (0)