Skip to content

Commit 5dad821

Browse files
committed
feat: add examples
1 parent 71b2809 commit 5dad821

File tree

9 files changed

+240
-0
lines changed

9 files changed

+240
-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/CodSpeedHQ/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/CodSpeedHQ/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/CodSpeedHQ/codspeed-go v0.0.0
6+
7+
replace github.com/CodSpeedHQ/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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package example
2+
3+
import "testing"
4+
5+
func BenchmarkFibonacci10(b *testing.B) {
6+
b.Run("fibonacci(10)", func(b *testing.B) {
7+
b.Run("fibonacci(10)", func(b *testing.B) {
8+
for i := 0; i < b.N; i++ {
9+
fibonacci(10)
10+
}
11+
})
12+
13+
})
14+
}
15+
16+
func BenchmarkFibonacci20_Loop(b *testing.B) {
17+
for b.Loop() {
18+
fibonacci(20)
19+
}
20+
}
21+
22+
func BenchmarkFibonacci20_bN(b *testing.B) {
23+
for i := 0; i < b.N; i++ {
24+
fibonacci(20)
25+
}
26+
}
27+
28+
// func BenchmarkFibonacci30(b *testing.B) {
29+
// b.Run("fibonacci(30)", func(b *testing.B) {
30+
// this shouldn't be executed
31+
// })
32+
// }

example/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example
2+
3+
go 1.24.3

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+
}

example/sleep_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package example
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func busyWait(duration time.Duration) {
9+
start := time.Now()
10+
for time.Since(start) < duration {
11+
// Busy wait loop
12+
}
13+
}
14+
15+
func BenchmarkSleep100ns(b *testing.B) {
16+
for i := 0; i < b.N; i++ {
17+
busyWait(100 * time.Nanosecond)
18+
}
19+
}
20+
21+
func BenchmarkSleep1us(b *testing.B) {
22+
for i := 0; i < b.N; i++ {
23+
busyWait(1 * time.Microsecond)
24+
}
25+
}
26+
27+
func BenchmarkSleep10us(b *testing.B) {
28+
for i := 0; i < b.N; i++ {
29+
busyWait(10 * time.Microsecond)
30+
}
31+
}
32+
33+
func BenchmarkSleep100us(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
busyWait(100 * time.Microsecond)
36+
}
37+
}
38+
39+
func BenchmarkSleep1ms(b *testing.B) {
40+
for i := 0; i < b.N; i++ {
41+
busyWait(1 * time.Millisecond)
42+
}
43+
}
44+
45+
func BenchmarkSleep10ms(b *testing.B) {
46+
for i := 0; i < b.N; i++ {
47+
busyWait(10 * time.Millisecond)
48+
}
49+
}
50+
51+
func BenchmarkSleep50ms(b *testing.B) {
52+
for i := 0; i < b.N; i++ {
53+
busyWait(50 * time.Millisecond)
54+
}
55+
}

0 commit comments

Comments
 (0)