module BigBench

Constants

VERSION

Public Class Methods

benchmark(options) click to toggle source

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
benchmarks() click to toggle source

List all available benchmarks

# File lib/bigbench/benchmark.rb, line 106
def self.benchmarks
  Benchmark.all
end
config() click to toggle source

Set and retreive the config values like this:

BigBench.config.duration # => 10.seconds
BigBench.config.duration = 20.minutes
# File lib/bigbench/configuration.rb, line 73
def self.config
  Configuration.config
end
configure() { |config| ... } click to toggle source

Configure the benchmark by supplying a hash of options like this:

BigBench.configure do |config|
  config.duration    = 10.seconds,
  config.output      = "test.ljson",
  config.users       = 20,
  config.basic_auth  = ['username', 'secret']
end

Those values can then be set and retreived with BigBench.config.duration, …

# File lib/bigbench/configuration.rb, line 62
def self.configure
  raise "No block to configure given" unless block_given?
  yield(Configuration.config)
  Configuration.config
end
duration() click to toggle source

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
load_test!(test) click to toggle source

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)
  BigBench::Output.loaded_tests
end
post_process(processor = nil, options = {}) click to toggle source

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.

Available Methods in Processors

Every post processor block or module supports the following methods and has full ActionView::Helper support.

each_tracking

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 188
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
post_processors() click to toggle source

List all initialized post processors

# File lib/bigbench/post_processor.rb, line 194
def self.post_processors
  PostProcessor.all
end
run!() click to toggle source

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
run_post_processors!() click to toggle source

Runs all initialized post processors after the trackings have been written

# File lib/bigbench/post_processor.rb, line 199
def self.run_post_processors!
  PostProcessor.run!
end
write_local_trackings_to_file!() click to toggle source

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
write_store_trackings_to_file!() click to toggle source

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
write_trackings_to_store!() click to toggle source

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