Repository navigation
Expand file tree
/
Copy pathbench_filesystem.go
More file actions
164 lines (157 loc) · 5.12 KB
/
Copy pathbench_filesystem.go
File metadata and controls
164 lines (157 loc) · 5.12 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package contexting
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"time"
)
func benchExecutable(name string) (string, error) {
binary, err := exec.LookPath(name)
if err != nil {
return "", fmt.Errorf("%s binary not found in PATH", name)
}
return binary, nil
}
// The saved tree defines the shared file corpus, including its ignore policy.
// Reject stale entries instead of comparing different sets of files silently.
func benchIndexedFiles(index *ContextIndex, maxBytes int) (map[string]string, error) {
files := make(map[string]string)
if index == nil || index.Tree == nil {
return files, fmt.Errorf("benchmark requires a file tree")
}
directories := make(map[string]bool)
var invalid error
walkTree(index.Tree, func(node *Node) {
if invalid != nil {
return
}
rel, err := filepath.Rel(index.Tree.FullPath, node.FullPath)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
invalid = fmt.Errorf("indexed path outside project root: %s", node.FullPath)
return
}
path := filepath.Join(index.RootPath, rel)
info, err := os.Lstat(path)
if err != nil || info.Mode()&os.ModeSymlink != 0 || (node != index.Tree && !directories[filepath.Dir(path)]) {
invalid = fmt.Errorf("indexed path unavailable or replaced by a symlink: %s; rebuild with ctxt init", path)
return
}
if node.Type == "directory" && info.IsDir() {
directories[path] = true
return
}
if node.Type == "file" && info.Mode().IsRegular() {
if node.ModTime != 0 && (node.ModTime != info.ModTime().UnixNano() || node.Size != info.Size()) {
invalid = fmt.Errorf("indexed file changed: %s; rebuild with ctxt init", path)
return
}
if maxBytes <= 0 || info.Size() <= int64(maxBytes) {
files[path] = filepath.ToSlash(rel)
}
} else {
invalid = fmt.Errorf("indexed entry changed type: %s; rebuild with ctxt init", path)
}
})
return files, invalid
}
func benchCommand(name, root string, args []string) ([]string, error) {
binary, err := benchExecutable(name)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
command := exec.CommandContext(ctx, binary, args...)
command.Dir = root
output, err := command.Output()
if err != nil && !((name == "grep" || name == "rg") && isExitCode1(err)) {
if ctx.Err() != nil {
return nil, fmt.Errorf("%s command: %w", name, ctx.Err())
}
return nil, fmt.Errorf("%s command failed: %w", name, err)
}
var paths []string
for _, value := range strings.Split(string(output), "\x00") {
if value != "" {
paths = append(paths, filepath.Clean(value))
}
}
return paths, nil
}
func benchFilenameSearch(name, query string, expected []string, index *ContextIndex, opts SearchOptions, prepared BenchCorpus) EngineResult {
start := time.Now()
corpus := prepared.files
args := []string{index.RootPath, "-type", "f", "-print0"}
if name == "fd" {
args = []string{"--absolute-path", "--type", "f", "--hidden", "--no-ignore", "--print0", "--search-path", index.RootPath}
}
candidates, err := benchCommand(name, index.RootPath, args)
var paths []string
tokens := tokenize(query)
for _, candidate := range candidates {
if rel, ok := corpus[candidate]; ok && tokensMatchPath(rel, tokens) {
paths = append(paths, rel)
}
}
sort.Strings(paths)
return benchResult(name, paths, expected, opts.Limit, start, err)
}
func benchContentSearch(name, query string, expected []string, index *ContextIndex, opts SearchOptions, maxBytes int, prepared BenchCorpus) EngineResult {
start := time.Now()
if _, err := benchExecutable(name); err != nil {
return benchResult(name, nil, expected, opts.Limit, start, err)
}
corpus := prepared.contentFiles
tokens := tokenize(query)
if len(tokens) == 0 {
return benchResult(name, nil, expected, opts.Limit, start, nil)
}
args := []string{"-I", "-l", "-i", "-F", "--null"}
if name == "rg" {
args = []string{"--files-with-matches", "--null", "--ignore-case", "--fixed-strings", "--no-ignore"}
}
for _, token := range tokens {
args = append(args, "-e", token)
}
args = append(args, "--")
files := make([]string, 0, len(corpus))
for file := range corpus {
files = append(files, file)
}
sort.Strings(files)
var paths []string
// Bound argv size on supported platforms; each command uses literal patterns.
for start := 0; start < len(files); {
end, bytes := start, 0
for end < len(files) && (end == start || bytes+len(files[end])+1 <= 8000) {
bytes += len(files[end]) + 1
end++
}
batch := append(append([]string(nil), args...), files[start:end]...)
matches, err := benchCommand(name, index.RootPath, batch)
if err != nil {
return benchResult(name, nil, expected, opts.Limit, time.Now(), err)
}
for _, file := range matches {
if rel, ok := corpus[file]; ok {
paths = append(paths, rel)
}
}
start = end
}
sort.Strings(paths)
return benchResult(name, paths, expected, opts.Limit, start, nil)
}
func tokensMatchPath(path string, tokens []string) bool {
lower := strings.ToLower(filepath.ToSlash(path))
for _, token := range tokens {
if strings.Contains(lower, token) {
return true
}
}
return false
}