Module: Ppbench

Defined in:
lib/ppbench.rb,
lib/ppbench/version.rb

Constant Summary collapse

R_COLORS =
[
    '0.5,0.5,0.5',
    '0.96,0.26,0.21',
    '0.25,0.31,0.71',
    '0.13,0.59,0.95',
    '0,0.59,0.53',
    '0.30,0.69,0.31',
    '0.8,0.86,0.22',
    '1,0.6,0.03',
    '1,0.6,0',
    '1,0.34,0.13'
]
R_NO_SYMBOL =
"16"
R_SYMBOLS =
"c(1,2,3,4,5,6,7,8,9,10)"
LOG_HEADER =
[
    "Machine Tag",
    "Experiment Tag",
    "Document Path",
    "Failed requests",
    "Concurrency Level",
    "Total transferred",
    "Time per request",
    "Transfer rate",
    "Requests per second",
    "Retries",
    "Response Code"
]
VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.add_comparisonplot(reference, serie, to_plot: :tpr, color: 'grey', symbol: 1, length: 500000, n: Ppbench::precision, nknots: Ppbench::precision) ⇒ Object

Adds a compare line to a comparison plot.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/ppbench.rb', line 291

def self.add_comparisonplot(
    reference,
    serie,
    to_plot: :tpr,
    color: 'grey',
    symbol: 1,
    length: 500000,
    n: Ppbench::precision,
    nknots: Ppbench::precision
)
  step = length / n
  references = reference.map { |v| [v[:length], v[to_plot]] }
  ref_values = 1.upto(n).map do |i|
    vs = references.select { |p| p[0] < i * step && p[0] >= (i - 1) * step }.map { |p| p[1] }

    if vs.empty?
      $stderr.puts precision_error(i * step)
      exit!
    end

    [
        i * step,
        vs.median
    ]
  end.to_h

  series = serie.map { |v| [v[:length], v[to_plot]] }
  serie_values = 1.upto(n).map do |i|
    vs = series.select { |p| p[0] < i * step && p[0] >= (i - 1) * step }.map { |p| p[1] }

    if vs.empty?
      $stderr.puts precision_error(i * step)
      exit!
    end

    [
        i * step,
        vs.median
    ]
  end.to_h

  xs = []
  ys = []

  ref_values.each do |x, y|
    if serie_values.key? x
      xs << x
      ys << serie_values[x] / y
    end
  end

  """
  xs=c(#{ xs * ',' })
  ys=c(#{ ys * ',' })
  median <- smooth.spline(xs, ys, nknots=#{nknots})
  lines(median, lwd=2, col=rgb(#{color}))
  """
end

.add_series(data, to_plot: :tpr, color: 'grey', symbol: 1, alpha: Ppbench::alpha, length: 500000, confidence: 90, no_points: false, with_bands: false) ⇒ Object

Adds a serie to a plot.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/ppbench.rb', line 272

def self.add_series(
    data,
    to_plot: :tpr,
    color: 'grey',
    symbol: 1,
    alpha: Ppbench::alpha,
    length: 500000,
    confidence: 90,
    no_points: false,
    with_bands: false
)
  """
  #{points(data, to_plot: to_plot, color: color, symbol: symbol, alpha: alpha) unless no_points }
  #{bands(data, to_plot: to_plot, color: color, length: length, confidence: confidence) if with_bands }
  """
end

.aggregate(data) ⇒ Object

Aggregate benchmark data. {

'weave': {
   'm.large': [{ machine: String, experiment: String, document: String, length: value, tpr: Integer, ... }]
   }, ...
},
'docker': { ... },
'bare': { ... }

}



204
205
206
207
208
209
210
211
212
213
# File 'lib/ppbench.rb', line 204

def self.aggregate(data)
  experiments = data.group_by { |entry| entry[:experiment] }
  experiments.map do |experiment, values|
    machines = values.group_by { |entry| entry[:machine] }
    [
        experiment,
        machines
    ]
  end.to_h
end

.alphaObject



43
44
45
# File 'lib/ppbench.rb', line 43

def self.alpha
  @alpha
end

.alpha=(v) ⇒ Object



39
40
41
# File 'lib/ppbench.rb', line 39

def self.alpha=(v)
  @alpha = v
end

.bands(data, to_plot: :tpr, n: Ppbench::precision, length: 500000, color: 'grey', confidence: 90, nknots: Ppbench::precision) ⇒ Object

Generates median lines and confidence bands for plots.



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/ppbench.rb', line 366

def self.bands(data, to_plot: :tpr, n: Ppbench::precision, length: 500000, color: 'grey', confidence: 90, nknots: Ppbench::precision)

  step = length / n
  points = data.map { |v| [v[:length], v[to_plot]] }
  values = 1.upto(n).map do |i|
    [
        i * step,
        points.select { |p| p[0] < i * step && p[0] >= (i - 1) * step }.map { |p| p[1] }
    ]
  end

  upper_confidence = 100 - (100 - confidence) / 2
  semi_upper_confidence = 100 - (100 - confidence / 2) / 2
  lower_confidence = (100 - confidence) / 2
  semi_lower_confidence = (100 - confidence / 2) / 2

  summary = values.map do |x,vs|

    if vs.empty?
      $stderr.puts precision_error(x)
      exit!
    end

    {
        :x => x,
        :lower => vs.percentile(lower_confidence),
        :semi_lower => vs.percentile(semi_lower_confidence),
        :median => vs.median,
        :semi_upper => vs.percentile(semi_upper_confidence),
        :upper => vs.percentile(upper_confidence)
    }
  end

  xs = "c(#{summary.map { |v| v[:x] } * ','})"
  medians = "c(#{summary.map { |v| v[:median] } * ','})"
  lowers = "c(#{summary.map { |v| v[:lower] } * ','})"
  semi_lowers = "c(#{summary.map { |v| v[:semi_lower] } * ','})"
  uppers = "c(#{summary.map { |v| v[:upper] } * ','})"
  semi_uppers = "c(#{summary.map { |v| v[:semi_upper] } * ','})"

  """
  xs = #{xs}
  medians = #{medians}
  lowers = #{lowers}
  semi_lowers = #{semi_lowers}
  uppers = #{uppers}
  semi_uppers = #{semi_uppers}

  low <- smooth.spline(xs, lowers, nknots=#{nknots})
  semi_low <- smooth.spline(xs, semi_lowers, nknots=#{nknots})
  up <- smooth.spline(xs, uppers, nknots=#{nknots})
  semi_up <- smooth.spline(xs, semi_uppers, nknots=#{nknots})
  median <- smooth.spline(xs, medians, nknots=#{nknots})
  polygon(c(low$x, rev(up$x)), c(low$y, rev(up$y)), col = rgb(#{color},alpha=0.10), border=NA)
  polygon(c(semi_low$x, rev(semi_up$x)), c(semi_low$y, rev(semi_up$y)), col = rgb(#{color},alpha=0.15), border=NA)
  lines(median, lwd=2, col=rgb(#{color}))
  lines(low, col=rgb(#{color},alpha=0.50), lty='dashed', lwd=0.5)
  lines(up, col=rgb(#{color},alpha=0.50), lty='dashed', lwd=0.5)
  """


end

.comparison_plotter(data, yaxis_max: 1.5, to_plot: :transfer_rate, machines: [], experiments: [], receive_window: 87380, xaxis_max: 500000, xaxis_steps: 10, xaxis_title: "", xaxis_unit: "", xaxis_divisor: 1000, yaxis_title: "", yaxis_unit: "%", title: "", subtitle: "", legend_position: "topright") ⇒ Object

Generates an R plot output script which can be used for plotting comparison plots of benchmark data.



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/ppbench.rb', line 492

def self.comparison_plotter(
    data,
    yaxis_max: 1.5,
    to_plot: :transfer_rate,
    machines: [],
    experiments: [],
    receive_window: 87380,
    xaxis_max: 500000,
    xaxis_steps: 10,
    xaxis_title: "",
    xaxis_unit: "",
    xaxis_divisor: 1000,
    yaxis_title: "",
    yaxis_unit: "%",
    title: "",
    subtitle: "",
    legend_position: "topright"
)
  series_data = []
  series_names = []
  series_colors = R_COLORS

  ref = true
  for exp in experiments
    for machine in machines
      reference = ref ? 'Reference: ' : ''
      ref = false
      if (data.include? exp) && (data[exp].include? machine)
        series_data << data[exp][machine]
        series_names << "'#{reference}#{Ppbench::experiment(exp)} on #{Ppbench::machine(machine)}'"
      end
    end
  end

  colors = "c(#{series_colors.map { |c| "rgb(#{c})" } * ','})"

  sym = 1;
  r = "#{prepare_comparisonplot(yaxis_max, receive_window: receive_window, length: xaxis_max, title: title, subtitle: subtitle, xaxis_title: xaxis_title, xaxis_unit: xaxis_unit, yaxis_title: yaxis_title, yaxis_unit: yaxis_unit)}\n"

  reference = series_data.first

  for serie in series_data
    r += add_comparisonplot(reference, serie, to_plot: to_plot, color: series_colors.shift, symbol: sym, length: xaxis_max)
    sym = sym + 1
  end

  r + """
  xa = seq(0, #{xaxis_max}, by=#{xaxis_max/xaxis_steps})
  ya = seq(0, #{yaxis_max}, by=#{0.1})
  axis(1, at = xa, labels = paste(xa/#{xaxis_divisor}, '#{xaxis_unit}', sep = '' ))
  axis(2, at = ya, labels = paste(ya * 100, '#{yaxis_unit}', sep = '' ))
  legend('#{legend_position}', cex=0.9, pch=c(#{R_NO_SYMBOL}), col=#{colors}, c(#{series_names * ',' }),box.col=rgb(1,1,1,0), bg=rgb(1,1,1,0.75))
  """
end

.experiment(key) ⇒ Object



24
25
26
27
28
29
# File 'lib/ppbench.rb', line 24

def self.experiment(key)
  return key if @naming.empty?
  return key unless @naming.key?('experiments')
  name = @naming['experiments'][key]
  name == nil ? key : name
end

.filter(data, maxsize: 2 ** 64, experiments: [], machines: [], fails: 0) ⇒ Object

Filter benchmark data.



187
188
189
190
191
192
193
# File 'lib/ppbench.rb', line 187

def self.filter(data, maxsize: 2 ** 64, experiments: [], machines: [], fails: 0)
  data.select { |entry| entry[:tpr] > 0 }
      .select { |entry| entry[:failed] <= fails }
      .select { |entry| entry[:length] <= maxsize }
      .select { |entry| machines.include?(entry[:machine]) || machines.empty? }
      .select { |entry| experiments.include?(entry[:experiment]) || experiments.empty? }
end

.load_data(files) ⇒ Object

Load CSV files and conversion to better analyzable format (List of hashes)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ppbench.rb', line 164

def self.load_data(files)
  files.map do |file|
    rows = CSV.read(file, headers: true)

    rows.map do |row|
      {
          :experiment => row.key?('Experiment Tag') ? row['Experiment Tag'] : nil,
          :machine => row.key?('Machine Tag') ? row['Machine Tag'] : nil,
          :document => row.key?('Document Path') ? row['Document Path'] : nil,
          :length => row.key?('Total transferred') ? row['Total transferred'].to_i : nil,
          :failed => row.key?('Failed requests') ? row['Failed requests'].to_i : nil,
          :tpr => row.key?('Time per request') ? row['Time per request'].to_f : nil,
          :transfer_rate => row.key?('Transfer rate') ? row['Transfer rate'].to_f : nil,
          :rps => row.key?('Requests per second') ? row['Requests per second'].to_f : nil,
          :retries => row.key?('Retries') ? row['Retries'].to_i : nil,
          :response_code => row.key?('Response Code') ? row['Response Code'].to_i : nil
      }
    end
  end.flatten
end

.machine(key) ⇒ Object



17
18
19
20
21
22
# File 'lib/ppbench.rb', line 17

def self.machine(key)
  return key if @naming.empty?
  return key unless @naming.key?('machines')
  name = @naming['machines'][key]
  name == nil ? key : name
end

.maximum(data, of: :tpr) ⇒ Object

Determines biggest value of aggregated data.



217
218
219
220
221
222
223
224
225
226
# File 'lib/ppbench.rb', line 217

def self.maximum(data, of: :tpr)
  y = 0
  for experiment, machines in data
    for machine, values in machines
      m = values.max_by { |e| e[of] }
      y = (y > m[of] ? y : m[of])
    end
  end
  y
end

.naming=(json) ⇒ Object



13
14
15
# File 'lib/ppbench.rb', line 13

def self.naming=(json)
  @naming = json
end

.plotter(data, to_plot: :tpr, machines: [], experiments: [], receive_window: 87380, xaxis_max: 500000, confidence: 90, no_points: false, with_bands: false, yaxis_max: 10000000, yaxis_steps: 10, xaxis_steps: 10, xaxis_title: "", xaxis_unit: "", xaxis_divisor: 1000, yaxis_title: "", yaxis_unit: "", yaxis_divisor: 1000000, title: "", subtitle: "", legend_position: "topright") ⇒ Object

Generates an R plot output script which can be used for plotting benchmark data as scatter plot with optional confidence bands.



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/ppbench.rb', line 432

def self.plotter(
    data,
    to_plot: :tpr,
    machines: [],
    experiments: [],
    receive_window: 87380,
    xaxis_max: 500000,
    confidence: 90,
    no_points: false,
    with_bands: false,
    yaxis_max: 10000000,
    yaxis_steps: 10,
    xaxis_steps: 10,
    xaxis_title: "",
    xaxis_unit: "",
    xaxis_divisor: 1000,
    yaxis_title: "",
    yaxis_unit: "",
    yaxis_divisor: 1000000,
    title: "",
    subtitle: "",
    legend_position: "topright"
)
  series_data = []
  series_names = []
  series_colors = R_COLORS

  for exp in experiments
    for machine in machines
      if (data.include? exp) && (data[exp].include? machine)
        series_data << data[exp][machine]
        series_names << "'#{Ppbench::experiment(exp)} on #{Ppbench::machine(machine)}'"
      end
    end
  end

  colors = "c(#{series_colors.map { |c| "rgb(#{c})" } * ','})"

  sym = 1;
  r = "#{prepare_plot(yaxis_max, receive_window: receive_window, length: xaxis_max, title: title, xaxis_title: xaxis_title, xaxis_unit: xaxis_unit, yaxis_title: yaxis_title, yaxis_unit: yaxis_unit, subtitle: subtitle)}\n"

  for serie in series_data
    r += add_series(serie, to_plot: to_plot, with_bands: with_bands, no_points: no_points, color: series_colors.shift, symbol: sym, length: xaxis_max, confidence: confidence)
    sym = sym + 1
  end

  symbols = no_points ? R_NO_SYMBOL : R_SYMBOLS

  r + """
  xa = seq(0, #{xaxis_max}, by=#{xaxis_max/xaxis_steps})
  ya = seq(0, #{yaxis_max}, by=#{yaxis_max/yaxis_steps})
  axis(1, at = xa, labels = paste(xa/#{xaxis_divisor}, '#{xaxis_unit}', sep = ' ' ))
  axis(2, at = ya, labels = paste(ya/#{yaxis_divisor}, '#{yaxis_unit}', sep = ' ' ))
  legend('#{legend_position}', cex=0.9, pch=#{symbols}, col=#{colors}, c(#{series_names * ',' }),box.col=rgb(1,1,1,0), bg=rgb(1,1,1,0.75))
  """
end

.points(data, to_plot: :tpr, color: 'grey', alpha: Ppbench::alpha, symbol: 1) ⇒ Object

Generates scatter plot of points for plots.



352
353
354
355
356
357
358
359
360
361
362
# File 'lib/ppbench.rb', line 352

def self.points(data, to_plot: :tpr, color: 'grey', alpha: Ppbench::alpha, symbol: 1)
  points = data.map { |v| [v[:length], v[to_plot]] }
  xs = "c(#{points.map { |e| e[0] } * ','})"
  ys = "c(#{points.map { |e| e[1] } * ','})"

  """
  xs = #{xs}
  ys = #{ys}
  points(x=xs,y=ys, col=rgb(#{color},alpha=#{ alpha }), pch=#{ symbol })
  """
end

.precisionObject



35
36
37
# File 'lib/ppbench.rb', line 35

def self.precision
  @precision
end

.precision=(v) ⇒ Object



31
32
33
# File 'lib/ppbench.rb', line 31

def self.precision=(v)
  @precision = v
end

.precision_error(length) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/ppbench.rb', line 47

def self.precision_error(length)
  """
  Sorry, we have not enough data for messages of about #{length} byte length.
  You may want to reduce the precision with the global --precision flag.
  Current precison is #{Ppbench::precision}.
  So you could collect more data (preferred) or reduce the precision value.
  """
end

.prepare_comparisonplot(maxy, receive_window: 87300, length: 50000, xaxis_title: "Message Length (kB)", xaxis_unit: "kB", yaxis_title: "Relative performance compared with reference experiment (%)", yaxis_unit: "%", title: "Relative performance (Data Transfer Rate)", subtitle: "") ⇒ Object

Prepares a plot to present relative comparisons.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ppbench.rb', line 251

def self.prepare_comparisonplot(
    maxy,
    receive_window: 87300,
    length: 50000,
    xaxis_title: "Message Length (kB)",
    xaxis_unit: "kB",
    yaxis_title: "Relative performance compared with reference experiment (%)",
    yaxis_unit: "%",
    title: "Relative performance (Data Transfer Rate)",
    subtitle: ""
)
  recwindow = receive_window == 0 ? '' : "abline(v = seq(#{receive_window}, #{length}, by=#{receive_window}), lty='dashed')"

  """
  plot(x=c(0), y=c(0), xlim=c(0, #{length}), ylim=c(0, #{maxy}), main='#{title}\\n(#{subtitle})', xlab='#{xaxis_title} (#{xaxis_unit})', ylab='#{yaxis_title} (#{yaxis_unit})', xaxt='n', yaxt='n', pch=NA)
 	#{recwindow if receive_window < length}
  """
end

.prepare_plot(maxy, receive_window: 87380, length: 500000, xaxis_title: "Message Length", xaxis_unit: "kB", yaxis_title: "Transfer Rate", yaxis_unit: "MB/sec", title: "Data Transfer Rates", subtitle: "") ⇒ Object

Prepares a plot to present absolute values.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ppbench.rb', line 230

def self.prepare_plot(
    maxy,
    receive_window: 87380,
    length: 500000,
    xaxis_title: "Message Length",
    xaxis_unit: "kB",
    yaxis_title: "Transfer Rate",
    yaxis_unit: "MB/sec",
    title: "Data Transfer Rates",
    subtitle: ""
)
  recwindow = receive_window == 0 ? '' : "abline(v = seq(#{receive_window}, #{length}, by=#{receive_window}), lty='dashed')"

  """
  plot(x=c(0), y=c(0), xlim=c(0, #{length}), ylim=c(0, #{maxy}), main='#{title}\\n(#{subtitle})', xlab='#{xaxis_title} (#{xaxis_unit})', ylab='#{yaxis_title} (#{yaxis_unit})', xaxt='n', yaxt='n', pch=NA)
 	#{recwindow if receive_window < length }
  """
end

.run_bench(host, log, machine_tag: '', experiment_tag: '', timeout: 60, repetitions: 10, coverage: 0.1, min: 1, max: 500000, concurrency: 10) ⇒ Object

Runs a benchmark against a host and stores benchmark data in a log file.



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
# File 'lib/ppbench.rb', line 89

def self.run_bench(host, log, machine_tag: '', experiment_tag: '', timeout: 60, repetitions: 10, coverage: 0.1, min: 1, max: 500000, concurrency: 10)
  rounds = ((max - min) * coverage).to_i

  CSV.open(log, 'w', write_headers: true, headers: Ppbench::LOG_HEADER, force_quotes: true) do |logger|

    logfile = Mutex.new
    progress = ProgressBar.new("Running", rounds)

    webclient = HTTPClient.new

    Parallel.each(1.upto(rounds), in_threads: concurrency) do |_|

      length = Random.rand(min..max)
      document = "/mping/#{length}"

      results = {
          duration: [],
          length: [],
          code: [],
          retries: [],
          fails: []
      }
      begin
        #uri = URI("#{host}#{document}")
        1.upto(repetitions) do
          answer = {}
          Timeout::timeout(timeout) do
            response = webclient.get("#{host}#{document}").body
            answer = JSON.parse(response)
          end
          results[:duration] << answer['duration']
          results[:length] << answer['length']
          results[:code] << answer['code']
          results[:retries] << answer['retries']
          results[:fails] << (answer['code'] == 200 ? 0 : 1)
        end
      rescue Exception => e
        print ("Timeout of '#{host}#{document}'")
        print ("#{e}")
      end

      unless results[:duration].empty?
        time_taken = results[:duration].mean # in milliseconds
        length = results[:length].median # message length
        transfer_rate = results[:length].sum * 1000 / results[:duration].sum
        code = results[:code].first # HTTP response code
        retries = results[:retries].sum # Amount of retries
        failed = results[:fails].sum # Amount of fails

        requests_per_second = 1000 / time_taken

        logfile.synchronize do
          progress.inc

          logger << [
              "#{machine_tag}",
              "#{experiment_tag}",
              "#{document}",
              "#{failed}",
              "#{concurrency}",
              "#{length}",
              "#{time_taken}",
              "#{transfer_rate}",
              "#{requests_per_second}",
              "#{retries}",
              "#{code}"
          ]
        end
      end
    end
  end
end