Files
cburn/internal/pipeline/bench_test.go
teernisse 892f578565 fix: linter compliance and code quality improvements across codebase
Address golangci-lint findings and improve error handling throughout:

Package doc comments:
- Add canonical "// Package X ..." comments to source, model, config,
  pipeline, cli, store, and main packages for godoc compliance.

Security & correctness:
- Fix directory permissions 0o755 -> 0o750 in store/cache.go Open()
  (gosec G301: restrict group write on cache directory)
- Fix config.Save() to check encoder error before closing file, preventing
  silent data loss on encode failure
- Add //nolint:gosec annotations with justifications on intentional
  patterns (constructed file paths, manual bounds checking, config fields)
- Add //nolint:nilerr on intentional error-swallowing in scanner WalkDir
- Add //nolint:revive on stuttering type names (ModelStats, ModelUsage)
  that would break too many call sites to rename

Performance (perfsprint):
- Replace fmt.Sprintf("%d", n) with strconv.FormatInt(n, 10) in format.go
  FormatTokens() and FormatNumber() hot paths
- Clean up redundant fmt.Sprintf patterns in FormatCost and FormatDelta

Code cleanup:
- Convert if-else chain to switch in parser.go skipJSONString() for clarity
- Remove unused indexedResult struct from pipeline/loader.go
- Add deferred cache.Close() in pipeline/bench_test.go to prevent leaks
- Add deferred cache.Close() in cmd/root.go data loading path
- Fix doc comment alignment in scanner.go decodeProjectName
- Remove trailing blank line in cmd/costs.go
- Fix duplicate "/day" suffix in cmd/summary.go cost-per-day formatting
- Rename shadowed variable 'max' -> 'maxVal' in cli/render.go Sparkline
2026-02-20 16:07:26 -05:00

93 lines
1.7 KiB
Go

package pipeline
import (
"os"
"path/filepath"
"testing"
"cburn/internal/source"
"cburn/internal/store"
)
func BenchmarkLoad(b *testing.B) {
homeDir, _ := os.UserHomeDir()
claudeDir := filepath.Join(homeDir, ".claude")
b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := Load(claudeDir, true, nil)
if err != nil {
b.Fatal(err)
}
_ = result
}
}
func BenchmarkParseFile(b *testing.B) {
homeDir, _ := os.UserHomeDir()
claudeDir := filepath.Join(homeDir, ".claude")
files, err := source.ScanDir(claudeDir)
if err != nil {
b.Fatal(err)
}
// Find the largest file for worst-case benchmarking
var biggest source.DiscoveredFile
var biggestSize int64
for _, f := range files {
info, err := os.Stat(f.Path)
if err != nil {
continue
}
if info.Size() > biggestSize {
biggestSize = info.Size()
biggest = f
}
}
b.Logf("Benchmarking largest file: %s (%.1f KB)", biggest.Path, float64(biggestSize)/1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := source.ParseFile(biggest)
if result.Err != nil {
b.Fatal(result.Err)
}
}
}
func BenchmarkScanDir(b *testing.B) {
homeDir, _ := os.UserHomeDir()
claudeDir := filepath.Join(homeDir, ".claude")
b.ResetTimer()
for i := 0; i < b.N; i++ {
files, err := source.ScanDir(claudeDir)
if err != nil {
b.Fatal(err)
}
_ = files
}
}
func BenchmarkLoadWithCache(b *testing.B) {
homeDir, _ := os.UserHomeDir()
claudeDir := filepath.Join(homeDir, ".claude")
cache, err := store.Open(CachePath())
if err != nil {
b.Fatal(err)
}
defer func() { _ = cache.Close() }()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cr, err := LoadWithCache(claudeDir, true, cache, nil)
if err != nil {
b.Fatal(err)
}
_ = cr
}
}