Skip to content

Commit 547833f

Browse files
committed
feat(go-runner): add support for package main benches
1 parent e753907 commit 547833f

20 files changed

+338
-28
lines changed

Cargo.lock

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go-runner/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ tempfile = "3.14"
2424
[dev-dependencies]
2525
insta = { version = "1.43", features = ["json", "redactions"] }
2626
rstest = "0.26"
27+
test-log = "0.2.18"

go-runner/src/builder/discovery.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ impl GoPackage {
9393
}
9494
};
9595

96-
// We can't import packages that are declared as `main`
97-
if file.pkg_name.name == "main" {
98-
warn!("Skipping file with main package: {file_path:?}");
99-
continue;
100-
}
101-
10296
// First, collect all benchmark function names from this file
10397
let mut found_benchmarks = Vec::new();
10498
for decl in &file.decl {

go-runner/src/builder/patcher.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ pub fn patch_go_source(source: &str) -> anyhow::Result<String> {
114114
Ok(source)
115115
};
116116

117+
let source = replace_package_main(source.into())?;
117118
let source = replace_import(
118-
source.to_string(),
119+
source,
119120
"testing",
120121
"testing \"github.com/CodSpeedHQ/codspeed-go/compat/testing\"",
121122
)?;
@@ -133,6 +134,24 @@ pub fn patch_go_source(source: &str) -> anyhow::Result<String> {
133134
Ok(source)
134135
}
135136

137+
/// Replace `package main` with `package main_compat` to allow importing it from other
138+
fn replace_package_main(source: String) -> anyhow::Result<String> {
139+
let parsed = gosyn::parse_source(&source)?;
140+
141+
// Only replace if package name is "main"
142+
if parsed.pkg_name.name != "main" {
143+
return Ok(source);
144+
}
145+
146+
// pkg_name.pos is the position of the identifier "main" in the source
147+
let name_start = parsed.pkg_name.pos;
148+
let name_end = name_start + parsed.pkg_name.name.len();
149+
150+
let mut result = source;
151+
result.replace_range(name_start..name_end, "main_compat");
152+
Ok(result)
153+
}
154+
136155
#[cfg(test)]
137156
mod tests {
138157
use super::*;
@@ -244,6 +263,19 @@ import (
244263
"fmt"
245264
"github.com/thejerf/slogassert"
246265
)
266+
"#;
267+
268+
const PACKAGE_MAIN: &str = r#"package main
269+
270+
import "testing"
271+
272+
func BenchmarkExample(b *testing.B) {
273+
// benchmark code
274+
}
275+
276+
func TestExample(t *testing.T) {
277+
s := "package main"
278+
}
247279
"#;
248280

249281
#[rstest]
@@ -261,6 +293,7 @@ import (
261293
"multiline_import_with_testing_string",
262294
MULTILINE_IMPORT_WITH_TESTING_STRING
263295
)]
296+
#[case("package_main", PACKAGE_MAIN)]
264297
fn test_patch_go_source(#[case] test_name: &str, #[case] source: &str) {
265298
let result = patch_go_source(source).unwrap();
266299
assert_snapshot!(test_name, result);

go-runner/src/builder/snapshots/codspeed_go_runner__builder__discovery__tests__discover_benchmarks.snap

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,59 @@ expression: packages
342342
}
343343
]
344344
},
345+
{
346+
"raw_package": {
347+
"Dir": "[package_dir]",
348+
"Name": "main",
349+
"ImportPath": "github.com/SimonWaldherr/golang-benchmarks/floodfill [github.com/SimonWaldherr/golang-benchmarks/floodfill.test]",
350+
"TestGoFiles": [
351+
"floodfill_test.go"
352+
],
353+
"TestImports": [
354+
"testing"
355+
],
356+
"CompiledGoFiles": [
357+
"floodfill_test.go"
358+
],
359+
"Module": {
360+
"Path": "github.com/SimonWaldherr/golang-benchmarks",
361+
"Dir": "[module_dir]",
362+
"GoMod": "[go_mod_path]",
363+
"GoVersion": "1.18",
364+
"Main": true
365+
}
366+
},
367+
"benchmarks": [
368+
{
369+
"name": "BenchmarkFloodFillBFS",
370+
"module_path": "github.com/SimonWaldherr/golang-benchmarks/floodfill",
371+
"import_alias": "benchmarkfloodfillbfs_18158933623445949257",
372+
"qualified_name": "benchmarkfloodfillbfs_18158933623445949257.BenchmarkFloodFillBFS",
373+
"file_path": "floodfill/floodfill_test.go"
374+
},
375+
{
376+
"name": "BenchmarkFloodFillDFS",
377+
"module_path": "github.com/SimonWaldherr/golang-benchmarks/floodfill",
378+
"import_alias": "benchmarkfloodfilldfs_18158933623445949257",
379+
"qualified_name": "benchmarkfloodfilldfs_18158933623445949257.BenchmarkFloodFillDFS",
380+
"file_path": "floodfill/floodfill_test.go"
381+
},
382+
{
383+
"name": "BenchmarkFloodFillRecursive",
384+
"module_path": "github.com/SimonWaldherr/golang-benchmarks/floodfill",
385+
"import_alias": "benchmarkfloodfillrecursive_18158933623445949257",
386+
"qualified_name": "benchmarkfloodfillrecursive_18158933623445949257.BenchmarkFloodFillRecursive",
387+
"file_path": "floodfill/floodfill_test.go"
388+
},
389+
{
390+
"name": "BenchmarkFloodFillStack4Way",
391+
"module_path": "github.com/SimonWaldherr/golang-benchmarks/floodfill",
392+
"import_alias": "benchmarkfloodfillstack4way_18158933623445949257",
393+
"qualified_name": "benchmarkfloodfillstack4way_18158933623445949257.BenchmarkFloodFillStack4Way",
394+
"file_path": "floodfill/floodfill_test.go"
395+
}
396+
]
397+
},
345398
{
346399
"raw_package": {
347400
"Dir": "[package_dir]",

go-runner/src/builder/snapshots/codspeed_go_runner__builder__patcher__tests__already_patched_import.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
source: src/builder/patcher.rs
2+
source: go-runner/src/builder/patcher.rs
33
expression: result
44
---
5-
package main
5+
package main_compat
66

77
import testing "github.com/CodSpeedHQ/codspeed-go/compat/testing"
88

go-runner/src/builder/snapshots/codspeed_go_runner__builder__patcher__tests__import_at_end_of_block.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
source: src/builder/patcher.rs
2+
source: go-runner/src/builder/patcher.rs
33
expression: result
44
---
5-
package main
5+
package main_compat
66

77
import (
88
"fmt"

go-runner/src/builder/snapshots/codspeed_go_runner__builder__patcher__tests__import_testing_and_slogassert.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
source: go-runner/src/builder/patcher.rs
33
expression: result
44
---
5-
package main
5+
package main_compat
66
import (
77
testing "github.com/CodSpeedHQ/codspeed-go/compat/testing"
88
"fmt"

go-runner/src/builder/snapshots/codspeed_go_runner__builder__patcher__tests__import_with_comments.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
source: src/builder/patcher.rs
2+
source: go-runner/src/builder/patcher.rs
33
expression: result
44
---
5-
package main
5+
package main_compat
66

77
import (
88
"fmt"

go-runner/src/builder/snapshots/codspeed_go_runner__builder__patcher__tests__import_with_extra_whitespace.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
source: src/builder/patcher.rs
2+
source: go-runner/src/builder/patcher.rs
33
expression: result
44
---
5-
package main
5+
package main_compat
66

77
import (
88
"fmt"

0 commit comments

Comments
 (0)