-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_guard_test.go
More file actions
77 lines (70 loc) · 2.73 KB
/
Copy pathperformance_guard_test.go
File metadata and controls
77 lines (70 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package scanner_test
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/randomcodespace/code-signal/internal/scan"
)
func TestDiffAnalyzesUniqueBlobKeysNotFileOccurrences(t *testing.T) {
const fileCount = 10000
repo := t.TempDir()
runPerfGit(t, repo, "init")
runPerfGit(t, repo, "config", "user.email", "scanner@example.test")
runPerfGit(t, repo, "config", "user.name", "Scanner Test")
writePerfFile(t, repo, "go.mod", "module example.test/many\n\ngo 1.26\n")
shared := "package many\n\n// TODO repeated aggregate issue\nfunc Value() int { return 1 }\n"
for i := 0; i < fileCount; i++ {
writePerfFile(t, repo, fmt.Sprintf("pkg/%05d/file.go", i), shared)
}
runPerfGit(t, repo, "add", ".")
runPerfGit(t, repo, "commit", "-m", "many duplicates")
ref := runPerfGitOutput(t, repo, "rev-parse", "HEAD")
var stats scan.Stats
diff, err := scan.Diff(context.Background(), repo, ref, ref, ".", scan.DiffOptions{Stats: &stats})
if err != nil {
t.Fatalf("Diff() error = %v", err)
}
if diff.TotalsDelta.Issues.Before != fileCount || diff.TotalsDelta.Issues.After != fileCount {
t.Fatalf("issues before/after = %d/%d, want %d duplicate file occurrences counted on both sides", diff.TotalsDelta.Issues.Before, diff.TotalsDelta.Issues.After, fileCount)
}
if stats.BuiltInAnalyses > 3 {
t.Fatalf("BuiltInAnalyses = %d, want analysis bounded by unique BlobKeys not %d file occurrences; stats=%#v", stats.BuiltInAnalyses, fileCount, stats)
}
if stats.BuiltInBlobKeys != stats.BuiltInAnalyses {
t.Fatalf("BuiltInBlobKeys = %d BuiltInAnalyses = %d, want one analysis per unique key", stats.BuiltInBlobKeys, stats.BuiltInAnalyses)
}
if stats.WorkersUsed <= 0 || stats.WorkersUsed > runtime.NumCPU() {
t.Fatalf("WorkersUsed = %d, want bounded by CPU count %d; stats=%#v", stats.WorkersUsed, runtime.NumCPU(), stats)
}
}
func writePerfFile(t *testing.T, repo, name, content string) {
t.Helper()
path := filepath.Join(repo, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", filepath.Dir(path), err)
}
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write %s: %v", name, err)
}
}
func runPerfGit(t *testing.T, repo string, args ...string) {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", repo}, args...)...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}
func runPerfGitOutput(t *testing.T, repo string, args ...string) string {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", repo}, args...)...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
return string(out[:len(out)-1])
}