-
Notifications
You must be signed in to change notification settings - Fork 317
/
matcher.rb
executable file
·270 lines (240 loc) · 6.64 KB
/
matcher.rb
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env ruby
#
# SPDX-FileCopyrightText: Copyright 2013-present Greg Hurrell and contributors.
# SPDX-License-Identifier: BSD-2-Clause
%w[ext lib].each do |dir|
path = File.expand_path("../../ruby/command-t/#{dir}", File.dirname(__FILE__))
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
end
require 'command-t/ext'
require 'command-t/util'
require 'benchmark'
require 'ostruct'
require 'yaml'
data = YAML.load_file(
File.expand_path('../../data/benchmark.yml', File.dirname(__FILE__))
)
log = File.expand_path('../../data/log.yml', File.dirname(__FILE__))
log_data = File.exist?(log) ? YAML.load_file(log) : []
threads = CommandT::Util.processor_count
puts "Starting benchmark run (PID: #{Process.pid})"
now = Time.now.to_s
TIMES = ENV.fetch('TIMES', 20).to_i
results = TIMES.times.map do
Benchmark.bmbm do |b|
data['tests'].each do |test|
scanner = OpenStruct.new(:paths => test['paths'])
matcher = CommandT::Matcher.new(scanner)
b.report(test['name']) do
test['times'].times do
test['queries'].each do |query|
([''] + query.split(//)).reduce('') do |acc, char|
query = acc + char
matcher.sorted_matches_for(
query,
:threads => threads,
:recurse => ENV.fetch('RECURSE', '1') == '1'
)
query
end
end
end
end
end
end
end
DIFFERENCE = 0
ABSOLUTE = 1
SIGN = 2
# Test for significance via Wilcoxon Signed Rank test.
#
# @see http://vassarstats.net/textbook/ch12a.html
def significance(last, current)
return 0.0 if last.length != current.length
table = last.zip(current).map do |l, c|
[
l - c, # difference
(l - c).abs, # absolute difference
(l - c).zero? ? nil : (l - c) / (l - c).abs, # signedness (-1 or +1)
]
end
table = table.select { |diff, abs, sig| !diff.zero? }
table = table.sort do |(a_diff, a_abs, a_sig), (b_diff, b_abs, b_sig)|
a_abs <=> b_abs
end
rank = 1
table = table.map do |row|
count = 0
rank = table.map.with_index do |(diff, abs, sig), i|
if abs == row[ABSOLUTE]
count += 1
i + 1
else
nil
end
end.compact.reduce(0) { |acc, val| acc + val }.to_f / count
row + [row[SIGN] * rank]
end
n = table.length
w = table.reduce(0) { |acc, (diff, abs, sig, signed_rank)| acc + signed_rank }
if n < 10
p_value = 0
thresholds = [
[],
[],
[],
[],
[],
[[15, 0.05]],
[[17, 0.05], [21, 0.025]],
[[22, 0.05], [25, 0.025], [28, 0.01]],
[[26, 0.05], [30, 0.025], [34, 0.01], [36, 0.005]],
[[29, 0.05], [35, 0.025], [39, 0.01], [43, 0.005]],
][n]
while limit = thresholds.pop do
if w.abs >= limit[0]
p_value = limit[1]
break
end
end
else
sd = Math.sqrt(n * (n + 1) * (2 * n + 1) / 6)
z = ((w - 0.5) / sd).abs
if z >= 3.291
p_value = 0.0005
elsif z >= 2.576
p_value = 0.005
elsif z >= 2.326
p_value = 0.01
elsif z >= 1.960
p_value = 0.025
elsif z >= 1.645
p_value = 0.05
else
p_value = 0
end
end
p_value
end
results = results.reduce({}) do |acc, run|
run.each do |result|
acc[result.label] ||= {}
acc[result.label]['real'] ||= []
acc[result.label]['real'] << result.real
acc[result.label]['total'] ||= []
acc[result.label]['total'] << result.total
end
acc
end
previous = YAML.load_file(log).last['results'] rescue nil
results.keys.each do |label|
test = results[label]
test['real (best)'] = test['real'].min
test['total (best)'] = test['total'].min
test['real (avg)'] = test['real'].reduce(:+) / test['real'].length
test['real (+/-)'] = previous && previous[label] &&
(test['real (avg)'] - previous[label]['real (avg)']) / test['real (avg)'] * 100
test['real (significance)'] = significance(previous[label]['real'], test['real']) if previous && previous[label]
test['total (avg)'] = test['total'].reduce(:+) / test['total'].length
test['total (+/-)'] = previous && previous[label] &&
(test['total (avg)'] - previous[label]['total (avg)']) / test['total (avg)'] * 100
test['total (significance)'] = significance(previous[label]['total'], test['total']) if previous && previous[label]
test['real (variance)'] = test['real'].reduce(0) { |acc, value|
acc + (test['real (avg)'] - value) ** 2
} / test['real'].length
test['total (variance)'] = test['total'].reduce(0) { |acc, value|
acc + (test['total (avg)'] - value) ** 2
} / test['total'].length
test['real (sd)'] = Math.sqrt(test['real (variance)'])
test['total (sd)'] = Math.sqrt(test['total (variance)'])
end
log_data.push({
'time' => now,
'results' => results,
})
File.open(log, 'w') { |f| f.write(log_data.to_yaml) }
def print_table(rows)
rows.each do |row|
row.each.with_index do |cell, i|
width = rows.reduce(0) { |acc, row| row[i].length > acc ? row[i].length : acc }
if i.zero?
print align(cell, width)
else
print(' ' + align(cell, width))
end
end
puts
end
end
def align(str, width)
if str.respond_to?(:justify)
case str.justify
when :center
('%*s%s%*s' % [
((width - str.length) / 2.0).round,
'',
str,
((width - str.length) / 2.0).round,
'',
])[0...width]
when :left
'%-*s' % [width, str]
else
'%*s' % [width, str]
end
else
'%*s' % [width, str]
end
end
AnnotatedString = Struct.new(:length, :to_s, :justify)
def center(str)
AnnotatedString.new(str.length, str, :center)
end
def float(x)
'%.5f' % x
end
def parens(x)
"(#{x})"
end
def trim(str)
str.sub(/0+\z/, '')
end
def maybe(value, default = '')
if value
yield value
else
default
end
end
puts "\n\nSummary of cpu time and (wall-clock time):\n"
headers = [
[
'',
center('best'),
center('avg'),
center('sd'),
center('+/-'),
center('p'),
center('(best)'),
center('(avg)'),
center('(sd)'),
center('+/-'),
center('p'),
]
]
rows = headers + results.map do |(label, data)|
[
label,
float(data['total (best)']),
float(data['total (avg)']),
float(data['total (sd)']),
maybe(data['total (+/-)'], center('?')) { |value| '[%+0.1f%%]' % value },
maybe(data['total (significance)']) { |value| value > 0 ? trim(float(value)) : '' },
parens(float(data['real (best)'])),
parens(float(data['real (avg)'])),
parens(float(data['real (sd)'])),
maybe(data['real (+/-)'], center('?')) { |value| '[%+0.1f%%]' % value },
maybe(data['real (significance)']) { |value| value > 0 ? trim(float(value)) : '' },
]
end
print_table(rows)