From 74bb873a94cbf269848ca2bfaf8b065d7e04417e Mon Sep 17 00:00:00 2001 From: Shishir Kakaraddi Date: Thu, 1 Feb 2018 18:00:18 -0800 Subject: [PATCH] add hash benchmarking to performance tests --- .../lib/object_serializer_performance_spec.rb | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/spec/lib/object_serializer_performance_spec.rb b/spec/lib/object_serializer_performance_spec.rb index feeb7bc..e2c4630 100644 --- a/spec/lib/object_serializer_performance_spec.rb +++ b/spec/lib/object_serializer_performance_spec.rb @@ -34,29 +34,48 @@ describe FastJsonapi::ObjectSerializer, performance: true do end end - def print_stats(count, ams_time, our_time) + def print_stats(message, count, ams_time, our_time) format = '%-15s %-10s %s' puts '' + puts message puts format(format, 'Serializer', 'Records', 'Time') puts format(format, 'AMS serializer', count, ams_time.round(2).to_s + ' ms') puts format(format, 'Fast serializer', count, our_time.round(2).to_s + ' ms') end + def run_hash_benchmark(message, movie_count, our_serializer, ams_serializer) + our_time = Benchmark.measure { our_hash = our_serializer.serializable_hash }.real * 1000 + ams_time = Benchmark.measure { ams_hash = ams_serializer.as_json }.real * 1000 + print_stats(message, movie_count, ams_time, our_time) + end + + def run_json_benchmark(message, movie_count, our_serializer, ams_serializer) + our_json = nil + ams_json = nil + our_time = Benchmark.measure { our_json = our_serializer.serialized_json }.real * 1000 + ams_time = Benchmark.measure { ams_json = ams_serializer.to_json }.real * 1000 + print_stats(message, movie_count, ams_time, our_time) + return our_json, ams_json + end + context 'when comparing with AMS 0.10.x' do [1, 25, 250, 1000].each do |movie_count| speed_factor = 25 it "should serialize #{movie_count} records atleast #{speed_factor} times faster than AMS" do ams_movies = build_ams_movies(movie_count) movies = build_movies(movie_count) - our_json = nil - ams_json = nil our_serializer = MovieSerializer.new(movies) ams_serializer = ActiveModelSerializers::SerializableResource.new(ams_movies) - our_time = Benchmark.measure { our_json = our_serializer.serialized_json }.real * 1000 - ams_time = Benchmark.measure { ams_json = ams_serializer.to_json }.real * 1000 - print_stats(movie_count, ams_time, our_time) + + message = "Serialize to JSON string #{movie_count} records" + our_json, ams_json = run_json_benchmark(message, movie_count, our_serializer, ams_serializer) + + message = "Serialize to Ruby Hash #{movie_count} records" + run_hash_benchmark(message, movie_count, our_serializer, ams_serializer) + expect(our_json.length).to eq ams_json.length expect { our_serializer.serialized_json }.to perform_faster_than { ams_serializer.to_json }.at_least(speed_factor).times + expect { our_serializer.serializable_hash }.to perform_faster_than { ams_serializer.as_json }.at_least(speed_factor).times end end end @@ -67,20 +86,21 @@ describe FastJsonapi::ObjectSerializer, performance: true do it "should serialize #{movie_count} records atleast #{speed_factor} times faster than AMS" do ams_movies = build_ams_movies(movie_count) movies = build_movies(movie_count) - our_json = nil - ams_json = nil - options = {} options[:meta] = { total: movie_count } options[:include] = [:actors, :movie_type] - our_serializer = MovieSerializer.new(movies, options) ams_serializer = ActiveModelSerializers::SerializableResource.new(ams_movies, include: options[:include], meta: options[:meta]) - our_time = Benchmark.measure { our_json = our_serializer.serialized_json }.real * 1000 - ams_time = Benchmark.measure { ams_json = ams_serializer.to_json }.real * 1000 - print_stats(movie_count, ams_time, our_time) + + message = "Serialize to JSON string #{movie_count} with includes and meta" + our_json, ams_json = run_json_benchmark(message, movie_count, our_serializer, ams_serializer) + + message = "Serialize to Ruby Hash #{movie_count} with includes and meta" + run_hash_benchmark(message, movie_count, our_serializer, ams_serializer) + expect(our_json.length).to eq ams_json.length expect { our_serializer.serialized_json }.to perform_faster_than { ams_serializer.to_json }.at_least(speed_factor).times + expect { our_serializer.serializable_hash }.to perform_faster_than { ams_serializer.as_json }.at_least(speed_factor).times end end end