From 5a323a4337c8c99e07b69de9e053bdb75bcf4900 Mon Sep 17 00:00:00 2001 From: Leif Gensert Date: Sun, 12 Jan 2014 16:12:47 +0100 Subject: [PATCH] add benchmark for simple mapping --- benchmarks/data.rb | 74 ++++++++++++++++++++++++++++++++++++++++++++++ benchmarks/run.rb | 46 ++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 benchmarks/data.rb create mode 100644 benchmarks/run.rb diff --git a/benchmarks/data.rb b/benchmarks/data.rb new file mode 100644 index 0000000..8a2e649 --- /dev/null +++ b/benchmarks/data.rb @@ -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 diff --git a/benchmarks/run.rb b/benchmarks/run.rb new file mode 100644 index 0000000..138f087 --- /dev/null +++ b/benchmarks/run.rb @@ -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 +