Skip to content

Commit 30416a1

Browse files
committed
test: add benchmarks for project filtering performance
* add benchmark tests for Filter function with different scenarios * test performance with 1000 projects for fuzzy matching, exact matching, no matches, and empty queries * add Nix script for running benchmarks across all packages
1 parent 90921ad commit 30416a1

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package projects
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
// generateProjects creates a slice of 'count' mock projects for benchmarking.
9+
func generateProjects(count int) []Project {
10+
projects := make([]Project, count)
11+
for i := range count {
12+
name := fmt.Sprintf("project-%04d", i)
13+
path := fmt.Sprintf("/repos/%s", name)
14+
projects[i] = Project{Name: name, Path: path}
15+
}
16+
return projects
17+
}
18+
19+
func BenchmarkFilter1000Projects(b *testing.B) {
20+
allProjects := generateProjects(1000)
21+
query := "proj-9" // A query that will match some projects, testing the fuzzy logic
22+
23+
for b.Loop() {
24+
Filter(allProjects, query)
25+
}
26+
}
27+
28+
func BenchmarkFilter1000Projects_NoMatch(b *testing.B) {
29+
allProjects := generateProjects(1000)
30+
query := "nonexistentquery" // A query that will not match any projects
31+
32+
for b.Loop() {
33+
Filter(allProjects, query)
34+
}
35+
}
36+
37+
func BenchmarkFilter1000Projects_ExactMatch(b *testing.B) {
38+
allProjects := generateProjects(1000)
39+
query := "project-0500" // A query that will exactly match one project
40+
41+
for b.Loop() {
42+
Filter(allProjects, query)
43+
}
44+
}
45+
46+
func BenchmarkFilter1000Projects_EmptyQuery(b *testing.B) {
47+
allProjects := generateProjects(1000)
48+
query := "" // An empty query should return all projects without fuzzy matching
49+
50+
for b.Loop() {
51+
Filter(allProjects, query)
52+
}
53+
}

nix/scripts/benchmark.nix

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{ writeShellApplication, go }:
2+
3+
writeShellApplication {
4+
name = "benchmark";
5+
runtimeInputs = [ go ];
6+
text = ''
7+
go test -bench=. ./...
8+
'';
9+
}

0 commit comments

Comments
 (0)