Permalink
Browse files

Refactor.

  • Loading branch information...
mdub committed Oct 20, 2011
1 parent 4fe8f14 commit 99cd436656383a0e5ffc8570252a842e3d88456c
Showing with 103 additions and 72 deletions.
  1. +4 −0 Gemfile
  2. +99 −72 benchmark/benchmark.rb
View
@@ -1,3 +1,7 @@
source :rubygems
gemspec
+
+gem "ruby-prof"
+gem "clamp"
+gem "json"
View
171 benchmark/benchmark.rb 100644 → 100755
@@ -1,110 +1,137 @@
+#! /usr/bin/env ruby
+
$:.unshift File.expand_path("../../lib", __FILE__)
require "rubygems"
+require "clamp"
require "benchmark"
require "ostruct"
require "representative/json"
require "representative/nokogiri"
require "representative/xml"
-@books = [
+$books = [
OpenStruct.new(
- :title => "Sailing for old dogs",
- :authors => ["Jim Watson"],
- :published => OpenStruct.new(
- :by => "Credulous Print",
- :year => 1994
- )
+ :title => "Sailing for old dogs",
+ :authors => ["Jim Watson"],
+ :published => OpenStruct.new(
+ :by => "Credulous Print",
+ :year => 1994
+ )
),
OpenStruct.new(
- :title => "On the horizon",
- :authors => ["Zoe Primpton", "Stan Ford"],
- :published => OpenStruct.new(
- :by => "McGraw-Hill",
- :year => 2005
- )
+ :title => "On the horizon",
+ :authors => ["Zoe Primpton", "Stan Ford"],
+ :published => OpenStruct.new(
+ :by => "McGraw-Hill",
+ :year => 2005
+ )
),
OpenStruct.new(
- :title => "The Little Blue Book of VHS Programming",
- :authors => ["Henry Nelson"],
- :rating => "****"
+ :title => "The Little Blue Book of VHS Programming",
+ :authors => ["Henry Nelson"],
+ :rating => "****"
)
]
-def represent_books_using(r)
+class RepresentativeBenchmark < Clamp::Command
+
+ ALL_STRATEGIES = %w(builder nokogiri json to_json)
- r.list_of :books, @books do
- r.element :title
- r.list_of :authors
- r.element :published do
- r.element :by
- r.element :year
+ def self.validate_strategy(strategy)
+ unless ALL_STRATEGIES.member?(strategy)
+ raise ArgumentError, "invalid strategy: #{strategy}"
end
+ strategy
end
-end
+ subcommand "bm", "Benchmark" do
-def iterations
- 1000
-end
+ option ["-n", "--iterations"], "N", "number of iterations", :default => 1000, &method(:Integer)
+
+ parameter "[STRATEGY] ...", "representation strategies\n(default: #{ALL_STRATEGIES.join(", ")})", :attribute_name => "strategies" do |strategies|
+ strategies.each { |strategy| RepresentativeBenchmark.validate_strategy(strategy) }
+ end
-def bm
- Benchmark.bm(12) do |x|
- %w(rep_xml rep_nokogiri rep_json use_to_json).each do |method|
- x.report(method) do
- iterations.times do
- send(method)
+ def execute
+ self.strategies = ALL_STRATEGIES if strategies.empty?
+ Benchmark.bm(12) do |x|
+ strategies.each do |strategy|
+ x.report(strategy) do
+ iterations.times do
+ send("with_#{strategy}")
+ end
+ end
end
end
end
+
end
- nil
-end
-def rep_xml
- xml = Builder::XmlMarkup.new(:indent => 2)
- r = Representative::Xml.new(xml)
- represent_books_using(r)
- xml.target!
-end
+ subcommand ["print", "p"], "Show output of a specified strategy" do
-def rep_nokogiri
- r = Representative::Nokogiri.new
- represent_books_using(r)
- r.to_xml
-end
+ parameter "STRATEGY", "one of: #{ALL_STRATEGIES.join(", ")}" do |strategy|
+ RepresentativeBenchmark.validate_strategy(strategy)
+ end
-def rep_json
- r = Representative::Json.new
- represent_books_using(r)
- r.to_json
-end
+ def execute
+ puts send("with_#{strategy}")
+ end
+
+ end
+
+ private
-def use_to_json
- book_data = @books.map do |book|
- {
- :title => book.title,
- :authors => book.authors,
- :published => if book.published
- {
- :by => book.published.by,
- :year => book.published.year
- }
+ def represent_books_using(r)
+
+ r.list_of :books, $books do
+ r.element :title
+ r.list_of :authors
+ r.element :published do
+ r.element :by
+ r.element :year
end
- }
+ end
+
+ end
+
+ def with_builder
+ xml = Builder::XmlMarkup.new(:indent => 2)
+ r = Representative::Xml.new(xml)
+ represent_books_using(r)
+ xml.target!
end
- book_data.to_json
-end
-action = ARGV.first
-if action
- result = nil
- iterations.times do
- result = send(action)
+ def with_nokogiri
+ r = Representative::Nokogiri.new
+ represent_books_using(r)
+ r.to_xml
end
- puts result
-else
- bm
+
+ def with_json
+ r = Representative::Json.new
+ represent_books_using(r)
+ r.to_json
+ end
+
+ def with_to_json
+ book_data = $books.map do |book|
+ {
+ :title => book.title,
+ :authors => book.authors,
+ :published => if book.published
+ {
+ :by => book.published.by,
+ :year => book.published.year
+ }
+ end
+ }
+ end
+ book_data.to_json
+ end
+
end
+
+RepresentativeBenchmark.run

0 comments on commit 99cd436

Please sign in to comment.