From ff2a6ab6f96a52b6b19f930d4f51764ae3477df6 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Tue, 3 Sep 2024 17:41:54 +0900 Subject: [PATCH] run parallel (#63) --- internal/engine.go | 28 ++++++++++++++++++++-------- internal/engine_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 internal/engine_test.go diff --git a/internal/engine.go b/internal/engine.go index f32a21d..dafd8ee 100644 --- a/internal/engine.go +++ b/internal/engine.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "strings" + "sync" "github.com/gnoswap-labs/tlin/internal/lints" tt "github.com/gnoswap-labs/tlin/internal/types" @@ -65,17 +66,28 @@ func (e *Engine) Run(filename string) ([]tt.Issue, error) { return nil, fmt.Errorf("error parsing file: %w", err) } + var wg sync.WaitGroup + var mu sync.Mutex + var allIssues []tt.Issue for _, rule := range e.rules { - if e.ignoredRules[rule.Name()] { - continue - } - issues, err := rule.Check(tempFile, node, fset) - if err != nil { - return nil, fmt.Errorf("error running lint rule: %w", err) - } - allIssues = append(allIssues, issues...) + wg.Add(1) + go func(r LintRule) { + defer wg.Done() + if e.ignoredRules[rule.Name()] { + return + } + issues, err := rule.Check(tempFile, node, fset) + if err != nil { + return + } + + mu.Lock() + allIssues = append(allIssues, issues...) + mu.Unlock() + }(rule) } + wg.Wait() filtered := e.filterUndefinedIssues(allIssues) diff --git a/internal/engine_test.go b/internal/engine_test.go new file mode 100644 index 0000000..b012c2c --- /dev/null +++ b/internal/engine_test.go @@ -0,0 +1,33 @@ +package internal + +import ( + "path/filepath" + "runtime" + "testing" +) + +func BenchmarkGoroutineEngine(b *testing.B) { + _, currentFile, _, _ := runtime.Caller(0) + testDataDir := filepath.Join(filepath.Dir(currentFile), "../testdata") + + engine, err := NewEngine(testDataDir) + if err != nil { + b.Fatalf("failed to create engine: %v", err) + } + + files, err := filepath.Glob(filepath.Join(testDataDir, "*/*.gno")) + if err != nil { + b.Fatalf("failed to list files: %v", err) + } + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + for _, file := range files { + _, err := engine.Run(file) + if err != nil { + b.Fatalf("failed to run engine: %v", err) + } + } + } +}