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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
#!/usr/bin/env ruby
require 'fileutils'
require 'yaml'
require 'csv'
require 'logger'
class AlgorithmTester
# block sizes
SIZES = %w{16 64 256 1024 8192 16384}
TESTS = [{
name: 'lscpu',
exec: %w{lscpu},
}, {
name: 'openssl',
exec: %w{openssl speed -mr -evp blake2b512 sha256 sha512 aes},
}]
CSV_COLS = {
all: %w{host algo size speed},
algo: %w{host size speed},
}
def self.run(app, args)
new(app, args).run
end
def initialize(app, args)
@log = ::Logger.new(STDERR)
# check command-line arguments
unless config_path = args.shift
raise "Usage: #{app} config.yaml"
end
# load config
@config = load_config(config_path)
log_level = (@config['log_level'] || 'info').upcase
@log.level = Logger.const_get((@config['log_level'] || 'info').upcase)
@log.debug { "log level = #{log_level}" }
end
def run
# create output directories
make_output_dirs
# connect to hosts in background, wait for all to complete
join(spawn_benchmarks)
# generate csvs and svgs, wait for all to complete
join(save(parse_data))
end
private
#
# Create output directories
#
def make_output_dirs
dirs = (%w{csvs svgs} + @config['hosts'].map { |row|
'hosts/%s' % [row['name']]
}).map { |dir|
'%s/%s' % [out_dir, dir]
}
@log.debug { 'creating output dirs: %s' % [dirs.join(', ')] }
FileUtils.mkdir_p(dirs)
end
#
# Spawn benchmarks in background and return a list of PIDs.
#
def spawn_benchmarks
# connect to hosts in background
@config['hosts'].reduce([]) do |r, row|
TESTS.reduce(r) do |r, test|
# build absolute path to output file
out_path = '%s/hosts/%s/%s.txt' % [
out_dir,
row['name'],
test[:name],
]
unless File.exists?(out_path)
# run command, append PID to results
r << bg(out_path, ssh(row['host'], test[:exec]))
end
r
end
end
end
#
# Parse openssl benchmark data into a map of algorithm => rows
#
def parse_data
@config['hosts'].reduce(Hash.new do |h, k|
h[k] = Hash.new do |h2, k2|
h2[k2] = { max: 0, rows: [] }
end
end) do |r, row|
# build absolute path to openssl data file
path = '%s/hosts/%s/openssl.txt' % [out_dir, row['name']]
# get arch
arch = row['pi'] ? 'arm' : 'intel'
lines = File.readlines(path).select { |line|
# match on result rows
line =~ /^\+F:/
}.each do |line|
# split to results
vals = line.strip.split(':')
# build algorithm name
algo = vals[2].gsub(/\s+/, '-')
# walk block sizes
SIZES.each_with_index do |size, i|
[{
algo: 'all',
arch: 'all',
}, {
algo: algo,
arch: 'all',
}, {
algo: 'all',
arch: arch,
}, {
algo: algo,
arch: arch,
}].each do |agg|
val = vals[i + 3].to_f
max = r[agg[:algo]][agg[:arch]][:max]
r[agg[:algo]][agg[:arch]][:max] = val if val > max
r[agg[:algo]][agg[:arch]][:rows] << if agg[:algo] == 'all'
# build row for all-*.csv
[row['name'], algo, size, val]
else
# row for algo-specific CSV
[row['name'], size, val]
end
end
end
end
r
end
end
#
# save results as CSV, generate SVGs in background, and
# return array of PIDs.
#
def save(data)
data.reduce([]) do |r, pair|
algo, arch_hash = pair
arch_hash.reduce(r) do |r, pair|
arch, arch_data = pair
# save csv
csv_path = save_csv(algo, arch, arch_data[:rows])
if algo != 'all'
max = get_max_value(data, algo, arch)
r << save_svg(algo, arch, max, csv_path)
end
# return list of pids
r
end
end
end
#
# save CSV of rows.
#
def save_csv(algo, arch, rows)
# build path to output csv
csv_path = '%s/csvs/%s-%s.csv' % [out_dir, algo, arch]
# write csv
CSV.open(csv_path, 'wb') do |csv|
# write column headers
csv << CSV_COLS[(algo == 'all') ? :all : :algo]
# write rows
rows.each do |row|
csv << row
end
end
# return csv path
csv_path
end
ARCH_TITLES = {
all: 'OpenSSL Speed: %s, All Systems',
arm: 'OpenSSL Speed: %s, Raspberry Pis Only',
intel: 'OpenSSL Speed: %s, Intel Only',
}
#
# Render CSV as SVG in background and return PID.
#
def save_svg(algo, arch, max, csv_path)
plot_path = '%s/plot.py' % [__dir__]
svg_path = '%s/svgs/%s-%s.svg' % [out_dir, algo, arch]
# make chart title
title = ARCH_TITLES[arch.intern] % [algo]
# calculate xlimit (round up to nearest 100)
# xlimit = ((algo =~ /^aes/) ? 400 : 2000).to_s
xlimit = (max / 104857600.0).ceil * 100
# build plot command
plot_cmd = [
'/usr/bin/python3',
plot_path,
csv_path,
svg_path,
title,
xlimit.to_s,
]
# create svg in background
bg('/dev/null', plot_cmd)
end
#
# get maximum value depending for chart
#
def get_max_value(data, algo, arch)
# get aes algorithms
aes_algos = data.keys.select { |k| k =~ /^aes-/ }
# calculate maximum value
max = if arch == 'all'
data['all']['all'][:max]
elsif aes_algos.include?(algo)
aes_algos.map { |k|
data[k][arch][:max]
}.reduce(0) { |rm, v|
v > rm ? v : rm
}
else
(data.keys - aes_algos).map { |k|
data[k][arch][:max]
}.reduce(0) { |rm, v|
v > rm ? v : rm
}
end
end
#
# Load config file and check for required keys.
#
def load_config(path)
# read/check config
YAML.load_file(path).tap do |r|
# check for required config keys
missing = %w{out_dir hosts}.reject { |key| r.key?(key) }
raise "Missing required config keys: #{missing}" if missing.size > 0
end
end
#
# join set of PIDs together
#
def join(set_name, pids = [])
@log.debug('join') do
'set = %s, pids = %s' % [set_name, pids.join(', ')]
end
pids.each do |pid|
Process.wait(pid)
raise "#{set_name} #{pid} failed" unless $?.success?
end
end
#
# Generate SSH command.
#
def ssh(host, cmd)
cmd = ['/usr/bin/ssh', host, *cmd]
cmd
end
#
# Spawn background task and return PID.
#
def bg(out_path, cmd)
@log.debug('bg') do
'out_path = %s, cmd = %s' % [out_path, cmd.join(' ')]
end
spawn(*cmd, in: '/dev/null', out: out_path, err: '/dev/null')
end
#
# Get output directory.
#
def out_dir
@config['out_dir']
end
end
# allow cli invocation
AlgorithmTester.run($0, ARGV) if __FILE__ == $0
|