module BigBench
# The tracker records all request results a benchmark makes. When the benchmark is finished, the trackings are written depending on the mode
# of BigBench:
#
# [config.mode == :bot] All trackings are appended to the redis store's tracking key
# [config.mode == :local] All trackings are written straight to the file defined by config.output
# [config.mode == :bots] All trackings are imported from the redis store's tracking key and written to the file defined by config.output
#
module Tracker
class Tracker
attr_accessor :trackings
def initialize
@trackings = []
end
def track object
@trackings << object.to_json
end
end
end
# Writes the trackings of a bot to the redis after he finishes the run
def self.write_trackings_to_store!
trackings, counter = 0, 0
BigBench.benchmarks.each{ |benchmark| trackings += benchmark.tracker.trackings.size }
BigBench::Output.writing_trackings(trackings)
BigBench.benchmarks.each do |benchmark|
benchmark.tracker.trackings.each do |tracking|
BigBench::Output.wrote_trackings(counter) if counter % 100 == 0
BigBench::Store.add_tracking(tracking)
counter += 1
end
end
BigBench::Output.finished_writing_trackings(counter)
end
# Writes the locals trackings from the benchmark to a file
def self.write_local_trackings_to_file!
trackings, counter = 0, 0
BigBench.benchmarks.each{ |benchmark| trackings += benchmark.tracker.trackings.size }
BigBench::Output.writing_trackings(trackings)
File.open(BigBench.config.output, "w+") do |file|
BigBench.benchmarks.each do |benchmark|
benchmark.tracker.trackings.each do |tracking|
BigBench::Output.wrote_trackings(counter) if counter % 100 == 0
file.write tracking + "\n"
counter += 1
end
end
end
BigBench::Output.finished_writing_trackings(counter)
end
# Gathers the trackings from the redis and writes them to a local file
def self.write_store_trackings_to_file!
trackings, counter = BigBench::Store.count_trackings, 0
BigBench::Output.writing_trackings(trackings)
File.open(BigBench.config.output, "w+") do |file|
while tracking = BigBench::Store.pop_tracking do
BigBench::Output.wrote_trackings(counter) if counter % 100 == 0
file.write tracking + "\n"
counter += 1
end
end
BigBench::Output.finished_writing_trackings(counter)
end
end