Skip to content

Instantly share code, notes, and snippets.

@MOZGIII
Created December 22, 2014 13:50
Show Gist options
  • Save MOZGIII/db0159fff3643e869465 to your computer and use it in GitHub Desktop.
Save MOZGIII/db0159fff3643e869465 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class State
attr_accessor :layers, :iterations
attr_accessor :success_rate, :time
def self.csv_headers
"layers,iterations,success_rate,time"
end
def to_csv_row
"#{layers},#{iterations},#{success_rate},#{time}"
end
def to_s
scale = "ms"
scaletime = @time
if scaletime > (10 * 1000)
scale = "s"
scaletime /= 1000
if scaletime > (10 * 60)
scale = "m"
scaletime /= 60
if scaletime > (10 * 60)
scale = "h"
scaletime /= 60
end
end
end
"%4d %5d %5.4f %10.4f%s" % [layers, iterations, success_rate, scaletime, scale]
end
end
def string_to_time(str)
ms = str.match(/\A(\d+(?:\.\d+)?)ms\z/)
ms = ms ? ms[1].to_f : 0
s = str.match(/\A(\d+(?:\.\d+)?)s\z/)
s = s ? s[1].to_f : 0
m = str.match(/\A(\d+(?:\.\d+)?)m(?:(\d+(?:\.\d+)?)s)?\z/)
m_m, m_s = 0, 0
m_m, m_s = m[1].to_f, m[2].to_f if m
h = str.match(/\A(\d+(?:\.\d+)?)h(?:(\d+(?:\.\d+)?)m)?\z/)
h_h, h_m = 0, 0
h_h, h_m = h[1].to_f, h[2].to_f if h
ms + 1000*(s + m_s + 60*(m_m + h_m + 60*h_h))
end
def try(layers, iterations)
val = `go run main.go --layers #{layers} --iter #{iterations}`
lines = val.split("\n")
success_rate, time = lines[-2, 2]
success_rate = success_rate.match(/\Asuccess rate:\s(.*)\z/)[1].to_f
time = string_to_time(time)
State.new.tap do |s|
s.layers = layers
s.iterations = iterations
s.success_rate = success_rate
s.time = time
end
rescue StandardError => e
puts "Unable to get job done"
puts val
puts success_rate, time
raise e
end
def try_avg(layers, iterations, attempts)
success_rate, time = 0, 0
attempts.times do
local_state = try(layers, iterations)
success_rate += local_state.success_rate
time += local_state.time
end
State.new.tap do |s|
s.layers = layers
s.iterations = iterations
s.success_rate = success_rate / attempts
s.time = time / attempts
end
end
class StepIter
def initialize(range, step = 1)
@range = range
@step = step
end
def each
@range.step(@step) { |*args| yield *args }
end
end
class GroupedPairsIter
def initialize(iterators)
@iterators = iterators
end
def each
@iterators.each do |pair|
outer_enum, inner_enum = pair
outer_enum.each do |outer|
inner_enum.each do |inner|
yield outer, inner
end
end
end
end
end
class TestMode
attr_accessor :layers
attr_accessor :iterations
def initialize(*args)
args = args.first if args.first.kind_of? Array
layers, iterations = args
end
end
iters_enum = [100, 1000, 5000, 10000]
File.open('stats.csv', 'w') do |f|
f.puts State.csv_headers
f.flush
gi = GroupedPairsIter.new([
[[100, 1000], [1, 2, 3, 4, 5]],
[[100, 1000], [10, 20, 50, 100]],
[[5000, 10000], [1, 2, 3, 4, 5]],
[[100,200,300,400,500], (1..10)], # a lot of micro tests
[[5000, 10000], [10, 20, 50, 100]],
[[100, 1000, 5000, 10000], [200, 500, 8000, 1000]]
])
gi.each do |iter, layers|
state = try_avg(layers, iter, 5)
f.puts state.to_csv_row
f.flush
puts state
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment