module BigBench # Post processors are run after all test are finished and the results are written to the output file. # A post processor can either be a block of code, or an existing post processor module. 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}[http://api.rubyonrails.org/classes/ActionView/Helpers.html] 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 # module PostProcessor @processors = [] # Runs all post processors in the order they were defined def self.run! all.each{ |processor| processor.run! } end # A single processor, be it a block or a module is always mapped into this class class Processor include ActionView::Helpers attr_accessor :proc attr_accessor :runs attr_accessor :options # Creates the processor with the block or a processor def initialize(processor = nil, options = {}, &block) @proc = processor_to_proc(processor) || block @runs, @options = 0, options end # Run the block of code or the run! method of the processor def run! @runs += 1 Context.module_exec(@options, &@proc) end private def processor_to_proc(processor) return nil if processor.nil? processor = processor_to_module(processor) processor.module_eval do class << self include Environment include ActionView::Helpers end end processor.method(:run!).to_proc end def processor_to_module(processor) return processor if processor.is_a? Module module_name = "BigBench::PostProcessor::#{processor.to_s.camelize}" module_constant = module_name.constantize return module_constant if module_constant.is_a? Module require module_name.underscore.sub('big_bench', 'bigbench') module_constant end end # Adds a new processor def self.add(processor = nil, options = nil, &block) @processors << Processor.new(processor, options, &block) end # Returns all initialized processors def self.all @processors end # Resets all post processors def self.reset! @processors = [] Environment.reset! end # The context in which post processor procs are evaluated module Context class << self include Environment include ActionView::Helpers end end # Thrown if a processor wasn't initialized the right way class InvalidProcessor < StandardError def message "You have to either specify a valid post processor or a block" end end 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. # # == Available Methods in Processors # # Every post processor block or module supports the following methods and has full # {ActionView::Helper}[http://api.rubyonrails.org/classes/ActionView/Helpers.html] 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 # 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 def self.post_processors PostProcessor.all end # Runs all initialized post processors after the trackings have been written def self.run_post_processors! PostProcessor.run! end end