Add a benchmark like this:
benchmark "default website pages" => "http://localhost:3000" do get "/" get "/blog" get "/imprint" get "/admin", :basic_auth => ['admin', 'secret'] end benchmark "login and logout" => "http://localhost:3000" do post "/login", :params => { :name => "test@user.com", :password => "secret" } post "/logout", :params => { :name => "test@user.com" } end
# File lib/bigbench/benchmark.rb, line 101 def self.benchmark(options) Benchmark.add(options, &Proc.new) if block_given? end
List all available benchmarks
# File lib/bigbench/benchmark.rb, line 106 def self.benchmarks Benchmark.all end
Checks if all necessary config options are set and raises an
InvalidOptions
exception if not
# File lib/bigbench/configuration.rb, line 111 def self.check_config! raise Configuration::InvalidOptions.new unless config.valid? true end
Set and retreive the config values like this:
BigBench.config.duration # => 10.seconds BigBench.config.duration = 20.minutes
# File lib/bigbench/configuration.rb, line 106 def self.config Configuration.config end
Configure the benchmark by supplying a hash of options like this:
BigBench.configure = { :duration => 10.seconds, :output => "test.ljson", :users => 20, :basic_auth => ['username', 'secret'] }
Those values can then be set and retreived with
BigBench.config.duration
, …
# File lib/bigbench/configuration.rb, line 97 def self.configure=(config) Configuration.configure=(config) end
Returns the duration of all benchmarks - ergo the duration of the longest one
# File lib/bigbench/benchmark.rb, line 111 def self.duration Benchmark.max_duration end
Loads a test from a string file that is either parsed from a local file or retreived from the key-value store.
benchmark_string = 'benchmark "index page" => "http://localhost:3000" do get "/" end' BigBench.load_test!(benchmark_string)
# File lib/bigbench/runner.rb, line 46 def self.load_test!(test) BigBench::Benchmark.reset! eval(test) check_config! BigBench::Output.loaded_tests end
To setup a post processor simply do this:
post_process do # Some code that is executed after the tests, like a database update, twitter post, email etc. end
Or use one of the predefined post processor or write one yourself:
post_process :statistics post_process BigBench::PostProcessor::Statistics post_process "statistics"
All the upper lines include the same post processor. Symbols and strings are camelized and constantized as the module name.
Every post processor block or module supports the following methods and has full ActionView::Helper support.
A method that iterates through every collected tracking. It automatically returns a hash with a single tracking of the following form:
{ :elapsed => 2.502132, :start => 1333986292.1755981, :stop => 1333986293.618884, :duration => 1443, :benchmark => "index page", :url => "http://www.google.de/", :path => "/", :method => "get", :status => 200 }
It can be used like this:
post_process do total_trackings, total_errors = 0, 0 each_tracking do |tracking| total_trackings += 1 total_errors += 1 unless tracking[:status] == 200 end Twitter.post "Just run BigBench with #{total_trackings} trackings and #{total_errors} errors." end
# File lib/bigbench/post_processor.rb, line 176 def self.post_process processor = nil, options = {} raise PostProcessor::InvalidProcessor.new if processor.nil? && !block_given? block_given? ? PostProcessor.add(processor, options, &Proc.new) : PostProcessor.add(processor, options) end
List all initialized post processors
# File lib/bigbench/post_processor.rb, line 182 def self.post_processors PostProcessor.all end
Runs all initialized benchmarks
# File lib/bigbench/runner.rb, line 32 def self.run! BigBench::Output.running_benchmarks Runner.run! BigBench::Output.finished_running_benchmarks end
Runs all initialized post processors after the trackings have been written
# File lib/bigbench/post_processor.rb, line 187 def self.run_post_processors! PostProcessor.run! end
Writes the locals trackings from the benchmark to a file
# File lib/bigbench/tracker.rb, line 45 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.to_json + "\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
# File lib/bigbench/tracker.rb, line 64 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
Writes the trackings of a bot to the redis after he finishes the run
# File lib/bigbench/tracker.rb, line 28 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.to_json) counter += 1 end end BigBench::Output.finished_writing_trackings(counter) end