Skip to content
Browse files

Run all drivers in a single benchmark run

  • Loading branch information...
1 parent c68e3e9 commit b6e963cf8911e102358c2aa7e17b9639ec2939fb @nicksieger committed
Showing with 37 additions and 46 deletions.
  1. +6 −5 Rakefile
  2. +31 −37 lib/xmlbench/harness.rb
  3. +0 −4 spec/spec_helper.rb
View
11 Rakefile
@@ -42,8 +42,9 @@ desc "Fetch new data"
task :check_data => URLs.keys
namespace :bench do
- def run_file(f)
- Harness.run_parser(f =~ %r{(xmlbench/.*)\.rb} && $1, URLs.keys.sort, ENV['N'] && ENV['N'].to_i)
+ def run_files(paths)
+ parsers = paths.map {|f| f =~ %r{(xmlbench/.*)\.rb} && $1 }
+ Harness.run_parsers(parsers, URLs.keys.sort, ENV['N'] && ENV['N'].to_i)
rescue => e
puts e.message
if e.message =~ /undefined method/
@@ -55,7 +56,7 @@ namespace :bench do
desc "Run the benchmarks on all parsers."
task :all => :check_data do
- FileList['lib/xmlbench/**/*.rb'].each {|f| run_file(f) }
+ run_files(FileList['lib/xmlbench/**/*.rb'])
end
Dir['lib/xmlbench/*'].each do |dir|
@@ -63,7 +64,7 @@ namespace :bench do
basename = File.basename(dir)
desc "Run the #{basename} parser benchmarks."
task basename => :check_data do
- FileList["#{dir}/**/*.rb"].each {|f| run_file(f) }
+ run_files(FileList["#{dir}/**/*.rb"])
end
end
end
@@ -71,7 +72,7 @@ end
task :bench => :check_data do
fail "specify parser with PARSERS=lib/xmlbench/somefile.rb" unless ENV["BENCH"]
- FileList[*(ENV["BENCH"].split(/\s*,\s*/))].each {|f| run_file(f) }
+ run_files(FileList[*(ENV["BENCH"].split(/\s*,\s*/))])
end
require 'spec/rake/spectask'
View
68 lib/xmlbench/harness.rb
@@ -20,7 +20,7 @@ def perform(xml_input)
end
class Driver
- attr_reader :label, :parser
+ attr_reader :label
def initialize(label, parser)
@label = label
if @label =~ /bench\/(.*)/
@@ -29,8 +29,8 @@ def initialize(label, parser)
@parser = parser
end
- def prepare(*args)
- @input = @parser.prepare_input(args[0])
+ def prepare(arg)
+ @input = @parser.prepare_input(arg)
end
def run
@@ -41,28 +41,26 @@ def run
# Default number of iterations.
DEFAULT_ITERATIONS = 100
- def initialize(driver, num_iterations)
- @driver = driver
+ def initialize(drivers, num_iterations)
+ @drivers = drivers
@num_iterations = num_iterations
end
def run_bench(*args)
Benchmark.bmbm do |x|
- args.each do |arg|
- begin
- @driver.prepare(arg)
- x.report(@driver.label + ": " + arg.name) { @num_iterations.times { @driver.run } }
- rescue => e
- puts e.message, *e.backtrace
+ @drivers.each do |driver|
+ args.each do |arg|
+ begin
+ driver.prepare(arg)
+ x.report(driver.label + ": " + arg.path) { @num_iterations.times { driver.run } }
+ rescue => e
+ puts e.message, *e.backtrace
+ end
end
end
end
end
- def runnable?
- @driver.parser
- end
-
# Redefine this method in specific parser code to create a parser.
#
# class Harness
@@ -74,30 +72,26 @@ def runnable?
def self.parser
end
- def self.create_harness(parser_name, num_iterations = DEFAULT_ITERATIONS)
- load "#{parser_name}.rb"
- driver = Driver.new(parser_name, parser)
- new(driver, num_iterations)
+ def self.create_harness(parsers, num_iterations = nil)
+ num_iterations ||= DEFAULT_ITERATIONS
+ drivers = parsers.map do |parser_name|
+ load "#{parser_name}.rb"
+ parser
+ if p = parser
+ Driver.new(parser_name, p)
+ else
+ puts "Skipping #{parser_name}; no suitable driver available on this VM"
+ end
+ end.compact
+ new(drivers, num_iterations)
end
# Run the parser 'n' times on the given list of files.
- def self.run_parser(parser, files, n)
- docs = files.map do |f|
- stream = File.new(f)
- (class << stream; self; end).instance_eval do
- define_method(:name) { f }
- end
- stream
- end
- args = [parser]; args << n if n
- harness = Harness.create_harness(*args)
- if harness.runnable?
- puts "Running #{parser}"
- harness.run_bench(*docs)
- else
- puts "Skipping #{parser}; no suitable driver available on this VM"
- end
-# ensure
-# docs.each {|d| d.close rescue nil }
+ def self.run_parsers(parsers, files, n)
+ docs = files.map { |f| File.new(f) }
+ harness = Harness.create_harness(parsers, n)
+ harness.run_bench(*docs)
+ ensure
+ docs.each {|d| d.close rescue nil }
end
end
View
4 spec/spec_helper.rb
@@ -1,8 +1,4 @@
require 'spec'
-require 'xmlbench/harness'
-
-$LOAD_PATH.unshift File.dirname(__FILE__) + '/..'
-
require 'xmlbench/all'
require File.expand_path(File.dirname(__FILE__)) + '/driver_helper'

0 comments on commit b6e963c

Please sign in to comment.
Something went wrong with that request. Please try again.