Skip to content

Commit

Permalink
run parallel (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon authored Sep 3, 2024
1 parent 63910dc commit ff2a6ab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
28 changes: 20 additions & 8 deletions internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
33 changes: 33 additions & 0 deletions internal/engine_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}

0 comments on commit ff2a6ab

Please sign in to comment.