benchmarks/encode.rb in pump-0.5.1 vs benchmarks/encode.rb in pump-0.6.0
- old
+ new
@@ -2,21 +2,30 @@
require 'rubygems'
require 'benchmark'
require 'pump'
-require 'ox' rescue nil
-require 'active_model' rescue nil
+require 'ox'
+require 'oj'
+require 'yajl'
+require 'active_model'
class Person < Struct.new(:name, :age, :created_at)
include ActiveModel::Serializers::Xml if defined?(ActiveModel)
+ include ActiveModel::Serializers::JSON if defined?(ActiveModel)
def attributes
{'name' => name, 'age' => age, 'created_at' => created_at}
end
end
+pump_json = Pump::Json.new('person', [
+ {:age => :age, :attributes => {:type => 'integer'}},
+ {:"created-at" => :created_at, :typecast => :xmlschema, :attributes => {:type => 'datetime'}, :never_nil => true},
+ {:name => :name}
+])
+
# Not optimized pump
pump = Pump::Xml.new('person', [
{:age => :age, :attributes => {:type => 'integer'}},
{:"created-at" => :created_at, :typecast => :xmlschema, :attributes => {:type => 'datetime'}, :never_nil => true},
{:name => :name}
@@ -75,10 +84,16 @@
times = ARGV[1] ? ARGV[1].to_i : 1000
puts "Starting benchmark serializing array with #{array.size} entries #{times} times\n\n"
Benchmark.bmbm { |x|
+ x.report("Pump::Json#encode") {
+ times.times {
+ pump_json.encode(array)
+ }
+ }
+
x.report("Pump::Xml#encode") {
times.times {
pump.encode(array)
}
}
@@ -95,13 +110,35 @@
serialize_with_ox(array)
}
}
end
+ if defined?(Oj)
+ x.report("Oj") {
+ times.times {
+ Oj.dump(array.map(&:attributes), :mode => :compat)
+ }
+ }
+ end
+
+ if defined?(Yajl)
+ x.report("Yajl") {
+ times.times {
+ Yajl::Encoder.encode(array.map(&:attributes))
+ }
+ }
+ end
+
if defined?(ActiveModel)
- x.report("ActiveModel#serialize") {
+ x.report("ActiveModel#to_xml") {
times.times {
array.to_xml
+ }
+ }
+
+ x.report("ActiveModel#to_json") {
+ times.times {
+ array.to_json
}
}
end
}