Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions example/sub-packages/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package subpackages

import (
"bytes"
"sync"
)

// Buffer is a simple wrapper around bytes.Buffer for demonstration
type Buffer struct {
b bytes.Buffer
}

// Write writes data to the buffer
func (b *Buffer) Write(p []byte) (n int, err error) {
return b.b.Write(p)
}

// Read reads data from the buffer
func (b *Buffer) Read(p []byte) (n int, err error) {
return b.b.Read(p)
}

// String returns the buffer contents as a string
func (b *Buffer) String() string {
return b.b.String()
}

// Counter is a mutex-protected counter for synctest demonstration
type Counter struct {
mu sync.Mutex
value int
}

// Increment increments the counter
func (c *Counter) Increment() {
c.mu.Lock()
c.value++
c.mu.Unlock()
}

// Value returns the current counter value
func (c *Counter) Value() int {
c.mu.Lock()
defer c.mu.Unlock()
return c.value
}

// WriterFunc is a function type that implements io.Writer
type WriterFunc func(p []byte) (n int, err error)

// Write implements io.Writer
func (f WriterFunc) Write(p []byte) (n int, err error) {
return f(p)
}
118 changes: 118 additions & 0 deletions example/sub-packages/buffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package subpackages

import (
"bytes"
"io"
"testing"
"testing/fstest"
"testing/iotest"
"testing/synctest"
)

// BenchmarkBufferWrite benchmarks writing to a buffer using io.Writer interface
func BenchmarkBufferWrite(b *testing.B) {
buf := &Buffer{}
data := []byte("hello world hello world hello world")

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Write(data)
}
}

// BenchmarkBufferRead benchmarks reading from a buffer
func BenchmarkBufferRead(b *testing.B) {
buf := &Buffer{}
buf.Write([]byte("hello world hello world hello world"))
readBuf := make([]byte, 10)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Read(readBuf)
}
}

// BenchmarkIOTestReader benchmarks using iotest package
func BenchmarkIOTestReader(b *testing.B) {
data := []byte("hello world hello world hello world")
reader := bytes.NewReader(data)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Create a reader that returns 1 byte at a time
oneByteReader := iotest.OneByteReader(bytes.NewReader(data))
io.Copy(io.Discard, oneByteReader)
reader.Reset(data)
}
}

// BenchmarkFSTestFS benchmarks the fstest package
func BenchmarkFSTestFS(b *testing.B) {
fs := fstest.MapFS{
"hello.txt": {Data: []byte("hello world")},
"test.txt": {Data: []byte("test data")},
}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
data, _ := fs.ReadFile("hello.txt")
_ = data
}
}

// TestSyncTestConcurrency tests using synctest for concurrent operations
func TestSyncTestConcurrency(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
counter := &Counter{}
done := make(chan bool, 1)

// Simulate concurrent increments with goroutine
go func() {
for i := 0; i < 100; i++ {
counter.Increment()
}
done <- true
}()

// Wait for goroutine to complete
<-done

// Verify final value
finalVal := counter.Value()
if finalVal != 100 {
t.Errorf("expected counter to be 100, got %d", finalVal)
}
})
}

// BenchmarkCounterWithMutex benchmarks counter increments with mutex protection
func BenchmarkCounterWithMutex(b *testing.B) {
counter := &Counter{}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Increment()
}
}

// BenchmarkWriterFunc benchmarks using a function as a writer
func BenchmarkWriterFunc(b *testing.B) {
writeCount := 0
fn := WriterFunc(func(p []byte) (n int, err error) {
writeCount++
return len(p), nil
})

data := []byte("hello world")

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
fn.Write(data)
}
}
1 change: 1 addition & 0 deletions go-runner/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub fn build_binary<P: AsRef<Path>>(runner_go_path: P) -> anyhow::Result<std::pa
let output = Command::new("go")
.args(&args)
.current_dir(module_root)
.env("GOWORK", "off") // Disable workspace mode to avoid -mod flag conflicts
.output()
.context("Failed to execute go build command")?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,59 @@ expression: packages
}
]
},
{
"raw_package": "[raw_package]",
"benchmarks": [
{
"name": "BenchmarkBufferRead",
"module_path": "example/sub-packages",
"import_alias": "benchmarkbufferread_2338334068071879025",
"qualified_name": "benchmarkbufferread_2338334068071879025.BenchmarkBufferRead",
"file_path": "sub-packages/buffer_test.go",
"is_external": false
},
{
"name": "BenchmarkBufferWrite",
"module_path": "example/sub-packages",
"import_alias": "benchmarkbufferwrite_2338334068071879025",
"qualified_name": "benchmarkbufferwrite_2338334068071879025.BenchmarkBufferWrite",
"file_path": "sub-packages/buffer_test.go",
"is_external": false
},
{
"name": "BenchmarkCounterWithMutex",
"module_path": "example/sub-packages",
"import_alias": "benchmarkcounterwithmutex_2338334068071879025",
"qualified_name": "benchmarkcounterwithmutex_2338334068071879025.BenchmarkCounterWithMutex",
"file_path": "sub-packages/buffer_test.go",
"is_external": false
},
{
"name": "BenchmarkFSTestFS",
"module_path": "example/sub-packages",
"import_alias": "benchmarkfstestfs_2338334068071879025",
"qualified_name": "benchmarkfstestfs_2338334068071879025.BenchmarkFSTestFS",
"file_path": "sub-packages/buffer_test.go",
"is_external": false
},
{
"name": "BenchmarkIOTestReader",
"module_path": "example/sub-packages",
"import_alias": "benchmarkiotestreader_2338334068071879025",
"qualified_name": "benchmarkiotestreader_2338334068071879025.BenchmarkIOTestReader",
"file_path": "sub-packages/buffer_test.go",
"is_external": false
},
{
"name": "BenchmarkWriterFunc",
"module_path": "example/sub-packages",
"import_alias": "benchmarkwriterfunc_2338334068071879025",
"qualified_name": "benchmarkwriterfunc_2338334068071879025.BenchmarkWriterFunc",
"file_path": "sub-packages/buffer_test.go",
"is_external": false
}
]
},
{
"raw_package": "[raw_package]",
"benchmarks": [
Expand Down
8 changes: 6 additions & 2 deletions go-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ pub fn run_benchmarks<P: AsRef<Path>>(
let binary_path = match builder::build_binary(&runner_path) {
Ok(binary_path) => binary_path,
Err(e) => {
error!("Failed to build {}: {e}", package.name);
continue;
if cfg!(test) {
panic!("Failed to build {}: {e}", package.name);
} else {
error!("Failed to build {}: {e}", package.name);
continue;
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,84 @@ source: go-runner/src/integration_tests.rs
expression: results
---
[
{
"creator": {
"name": "codspeed-go",
"version": "[version]",
"pid": "[pid]"
},
"instrument": {
"type": "walltime"
},
"benchmarks": [
{
"name": "BenchmarkBufferRead",
"uri": "example/sub-packages/buffer_test.go::BenchmarkBufferRead",
"config": {
"warmup_time_ns": null,
"min_round_time_ns": null,
"max_time_ns": null,
"max_rounds": null
},
"stats": "[stats]"
},
{
"name": "BenchmarkBufferWrite",
"uri": "example/sub-packages/buffer_test.go::BenchmarkBufferWrite",
"config": {
"warmup_time_ns": null,
"min_round_time_ns": null,
"max_time_ns": null,
"max_rounds": null
},
"stats": "[stats]"
},
{
"name": "BenchmarkCounterWithMutex",
"uri": "example/sub-packages/buffer_test.go::BenchmarkCounterWithMutex",
"config": {
"warmup_time_ns": null,
"min_round_time_ns": null,
"max_time_ns": null,
"max_rounds": null
},
"stats": "[stats]"
},
{
"name": "BenchmarkFSTestFS",
"uri": "example/sub-packages/buffer_test.go::BenchmarkFSTestFS",
"config": {
"warmup_time_ns": null,
"min_round_time_ns": null,
"max_time_ns": null,
"max_rounds": null
},
"stats": "[stats]"
},
{
"name": "BenchmarkIOTestReader",
"uri": "example/sub-packages/buffer_test.go::BenchmarkIOTestReader",
"config": {
"warmup_time_ns": null,
"min_round_time_ns": null,
"max_time_ns": null,
"max_rounds": null
},
"stats": "[stats]"
},
{
"name": "BenchmarkWriterFunc",
"uri": "example/sub-packages/buffer_test.go::BenchmarkWriterFunc",
"config": {
"warmup_time_ns": null,
"min_round_time_ns": null,
"max_time_ns": null,
"max_rounds": null
},
"stats": "[stats]"
}
]
},
{
"creator": {
"name": "codspeed-go",
Expand Down
Loading
Loading