Skip to content

Commit

Permalink
Merge pull request #2 from leifg/benchmarks
Browse files Browse the repository at this point in the history
add benchmarks
  • Loading branch information
leifg committed Jan 12, 2014
2 parents 5f50918 + 5a323a4 commit 6cd1d05
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
74 changes: 74 additions & 0 deletions benchmarks/data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module BenchmarkData
extend self

def nested_wrapper
:person
end

def row
{
first_name: 'Jazmyn',
last_name: 'Willms',
gender: 'female',
phone_number: '485-675-9228',
cell_phone: '1-172-435-9402 x4907',
street_name: 'Becker Inlet',
street_number: '15a',
city: 'Carolynchester',
zip: '38189',
country: 'USA',
}
end

def row_string_keys
stringify_keys(row)
end

def row_nested
{
nested_wrapper => row
}
end

def row_nested_string_keys
{
nested_wrapper.to_s => row_string_keys
}
end

private
def stringify_keys hash
hash.keys.each do |key|
hash[key.to_s] = hash.delete(key)
end
hash
end

class SimpleMappingSymbol < Morfo::Base
BenchmarkData.row.keys.each do |field|
map field, :"#{field}_mapped"
end
end

class SimpleMappingString < Morfo::Base
BenchmarkData.row_string_keys.keys.each do |field|
map field, "#{field}_mapped"
end
end

class NestedMappingSymbol < Morfo::Base
BenchmarkData.row_nested.each do |key, value|
value.keys.each do |field|
map [key, field], :"#{field}_mapped"
end
end
end

class NestedMappingString < Morfo::Base
BenchmarkData.row_nested_string_keys.each do |key, value|
value.keys.each do |field|
map [key, field], "#{field}_mapped"
end
end
end
end
46 changes: 46 additions & 0 deletions benchmarks/run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'morfo'
require 'benchmark'
require './benchmarks/data'

iterations = 100
batch_size = 10000

definitions = [
{
label: 'Simple (strings)',
row: BenchmarkData.row_string_keys,
morf_class: BenchmarkData::SimpleMappingString
},
{
label: 'Simple (symbols)',
row: BenchmarkData.row,
morf_class: BenchmarkData::SimpleMappingSymbol
},
{
label: 'Nested (strings)',
row: BenchmarkData.row_nested_string_keys,
morf_class: BenchmarkData::NestedMappingString
},
{
label: 'Nested (symbols)',
row: BenchmarkData.row_nested,
morf_class: BenchmarkData::NestedMappingSymbol
},
]

definitions.each do |defintition|
defintition.merge!(
data: Array.new(batch_size){ defintition[:row] }
)
end

Benchmark.bm(20) do |x|
definitions.each do |defintition|
x.report(defintition[:label]) do
iterations.times do
defintition[:morf_class].morf(defintition[:data])
end
end
end
end

0 comments on commit 6cd1d05

Please sign in to comment.