diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 87df39c..03f4eeb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: fi - name: go test - run: go test ./... -race -count=1 -timeout 300s + run: go test ./... -race -count=1 -timeout 240s security: runs-on: ubuntu-latest diff --git a/bench/score.go b/bench/score.go index 59fb71a..c3a0e85 100644 --- a/bench/score.go +++ b/bench/score.go @@ -1,6 +1,7 @@ package bench import ( + "context" "encoding/json" "fmt" "io" @@ -15,6 +16,7 @@ import ( "github.com/BurntSushi/toml" "github.com/noeljackson/deepsec/internal/core" "github.com/noeljackson/deepsec/internal/scanner" + scannerast "github.com/noeljackson/deepsec/internal/scanner/ast" ) type ScoreOptions struct { @@ -224,12 +226,22 @@ func Score(taskIDs []string, opts ScoreOptions) (*RunResult, error) { noise[m.Slug()] = m.NoiseTier() } + // Build one wazero runtime up front and reuse it across every + // scoreTask. Wazevo's WASM compile is ~10s per task with all 12 + // grammars; without sharing, a 30-task corpus blows past CI's + // 5-minute test timeout. + astRT, err := scannerast.NewRuntime(context.Background(), scannerast.DefaultGrammars()) + if err != nil { + return nil, fmt.Errorf("init shared AST runtime: %w", err) + } + defer astRT.Close(context.Background()) + now := time.Now().UTC().Format("20060102T150405Z") outDir := filepath.Join(opts.OutDir, now) result := &RunResult{} agg := accumulator{} for _, id := range taskIDs { - tr, err := scoreTask(id, opts.TasksDir, outDir, opts.CandidateExplosion, noise, opts.ExtraMatcherPaths) + tr, err := scoreTask(id, opts.TasksDir, outDir, opts.CandidateExplosion, noise, opts.ExtraMatcherPaths, astRT) if err != nil { return nil, err } @@ -263,7 +275,7 @@ type scoredTask struct { slugNoisy map[string]int } -func scoreTask(id, tasksDir, outDir string, explosionThreshold float64, noise map[string]scanner.NoiseTier, extraMatcherPaths []string) (scoredTask, error) { +func scoreTask(id, tasksDir, outDir string, explosionThreshold float64, noise map[string]scanner.NoiseTier, extraMatcherPaths []string, astRT *scannerast.Runtime) (scoredTask, error) { taskDir := filepath.Join(tasksDir, id) key, err := LoadAnswerKey(filepath.Join(taskDir, "answer.yaml")) if err != nil { @@ -291,6 +303,7 @@ func scoreTask(id, tasksDir, outDir string, explosionThreshold float64, noise ma MatcherOnly: cfg.MatcherOnly, MatcherExclude: cfg.MatcherExclude, ExtraMatcherPaths: extraMatcherPaths, + SharedASTRuntime: astRT, }) if err != nil { return scoredTask{}, err diff --git a/internal/scanner/scan.go b/internal/scanner/scan.go index bec30bf..7dd59ea 100644 --- a/internal/scanner/scan.go +++ b/internal/scanner/scan.go @@ -30,6 +30,11 @@ type Options struct { // re-evaluated even when both its content and the matcher pack are // unchanged since the last scan. ForceRescan bool + // SharedASTRuntime lets a caller hoist the wazero runtime across + // many Scan calls — bench.scoreTask reuses one runtime across all + // fixtures instead of paying the ~10s wazevo compile per task. When + // nil, Scan constructs a per-call runtime as before. + SharedASTRuntime *scannerast.Runtime } // LanguageStat is one row of the per-language scan summary. @@ -85,12 +90,16 @@ func Scan(opts Options) (*Outcome, error) { return nil, err } active, skipped := splitGated(reg, tech, opts.Root) - astRT, err := runtimeForActiveAST(active, reg) - if err != nil { - return nil, err - } - if astRT != nil { - defer astRT.Close(context.Background()) + astRT := opts.SharedASTRuntime + if astRT == nil { + built, err := runtimeForActiveAST(active, reg) + if err != nil { + return nil, err + } + if built != nil { + defer built.Close(context.Background()) + } + astRT = built } files, err := WalkProject(opts.Root) @@ -198,12 +207,16 @@ func ScanFiles(opts Options, files []string, source string) (*FilesOutcome, erro return nil, err } active, skipped := splitGated(reg, tech, opts.Root) - astRT, err := runtimeForActiveAST(active, reg) - if err != nil { - return nil, err - } - if astRT != nil { - defer astRT.Close(context.Background()) + astRT := opts.SharedASTRuntime + if astRT == nil { + built, err := runtimeForActiveAST(active, reg) + if err != nil { + return nil, err + } + if built != nil { + defer built.Close(context.Background()) + } + astRT = built } packHash := reg.PackHash()