3434 end
3535 end
3636
37- def print_stats ( count , ams_time , our_time )
37+ def print_stats ( message , count , ams_time , our_time )
3838 format = '%-15s %-10s %s'
3939 puts ''
40+ puts message
4041 puts format ( format , 'Serializer' , 'Records' , 'Time' )
4142 puts format ( format , 'AMS serializer' , count , ams_time . round ( 2 ) . to_s + ' ms' )
4243 puts format ( format , 'Fast serializer' , count , our_time . round ( 2 ) . to_s + ' ms' )
4344 end
4445
46+ def run_hash_benchmark ( message , movie_count , our_serializer , ams_serializer )
47+ our_time = Benchmark . measure { our_hash = our_serializer . serializable_hash } . real * 1000
48+ ams_time = Benchmark . measure { ams_hash = ams_serializer . as_json } . real * 1000
49+ print_stats ( message , movie_count , ams_time , our_time )
50+ end
51+
52+ def run_json_benchmark ( message , movie_count , our_serializer , ams_serializer )
53+ our_json = nil
54+ ams_json = nil
55+ our_time = Benchmark . measure { our_json = our_serializer . serialized_json } . real * 1000
56+ ams_time = Benchmark . measure { ams_json = ams_serializer . to_json } . real * 1000
57+ print_stats ( message , movie_count , ams_time , our_time )
58+ return our_json , ams_json
59+ end
60+
4561 context 'when comparing with AMS 0.10.x' do
4662 [ 1 , 25 , 250 , 1000 ] . each do |movie_count |
4763 speed_factor = 25
4864 it "should serialize #{ movie_count } records atleast #{ speed_factor } times faster than AMS" do
4965 ams_movies = build_ams_movies ( movie_count )
5066 movies = build_movies ( movie_count )
51- our_json = nil
52- ams_json = nil
5367 our_serializer = MovieSerializer . new ( movies )
5468 ams_serializer = ActiveModelSerializers ::SerializableResource . new ( ams_movies )
55- our_time = Benchmark . measure { our_json = our_serializer . serialized_json } . real * 1000
56- ams_time = Benchmark . measure { ams_json = ams_serializer . to_json } . real * 1000
57- print_stats ( movie_count , ams_time , our_time )
69+
70+ message = "Serialize to JSON string #{ movie_count } records"
71+ our_json , ams_json = run_json_benchmark ( message , movie_count , our_serializer , ams_serializer )
72+
73+ message = "Serialize to Ruby Hash #{ movie_count } records"
74+ run_hash_benchmark ( message , movie_count , our_serializer , ams_serializer )
75+
5876 expect ( our_json . length ) . to eq ams_json . length
5977 expect { our_serializer . serialized_json } . to perform_faster_than { ams_serializer . to_json } . at_least ( speed_factor ) . times
78+ expect { our_serializer . serializable_hash } . to perform_faster_than { ams_serializer . as_json } . at_least ( speed_factor ) . times
6079 end
6180 end
6281 end
@@ -67,20 +86,21 @@ def print_stats(count, ams_time, our_time)
6786 it "should serialize #{ movie_count } records atleast #{ speed_factor } times faster than AMS" do
6887 ams_movies = build_ams_movies ( movie_count )
6988 movies = build_movies ( movie_count )
70- our_json = nil
71- ams_json = nil
72-
7389 options = { }
7490 options [ :meta ] = { total : movie_count }
7591 options [ :include ] = [ :actors , :movie_type ]
76-
7792 our_serializer = MovieSerializer . new ( movies , options )
7893 ams_serializer = ActiveModelSerializers ::SerializableResource . new ( ams_movies , include : options [ :include ] , meta : options [ :meta ] )
79- our_time = Benchmark . measure { our_json = our_serializer . serialized_json } . real * 1000
80- ams_time = Benchmark . measure { ams_json = ams_serializer . to_json } . real * 1000
81- print_stats ( movie_count , ams_time , our_time )
94+
95+ message = "Serialize to JSON string #{ movie_count } with includes and meta"
96+ our_json , ams_json = run_json_benchmark ( message , movie_count , our_serializer , ams_serializer )
97+
98+ message = "Serialize to Ruby Hash #{ movie_count } with includes and meta"
99+ run_hash_benchmark ( message , movie_count , our_serializer , ams_serializer )
100+
82101 expect ( our_json . length ) . to eq ams_json . length
83102 expect { our_serializer . serialized_json } . to perform_faster_than { ams_serializer . to_json } . at_least ( speed_factor ) . times
103+ expect { our_serializer . serializable_hash } . to perform_faster_than { ams_serializer . as_json } . at_least ( speed_factor ) . times
84104 end
85105 end
86106 end
0 commit comments