Skip to content

Instantly share code, notes, and snippets.

@wppurking
Last active January 30, 2018 07:54
Show Gist options
  • Save wppurking/b805bc14bd0fe03aa3c346be98c9d9e4 to your computer and use it in GitHub Desktop.
Save wppurking/b805bc14bd0fe03aa3c346be98c9d9e4 to your computer and use it in GitHub Desktop.
roar vs ams vs rabl vs jbuilder Raw. (json_gem, oj)
require 'bundler'
require 'active_model_serializers'
require 'roar/decorator'
require 'roar/json'
require 'rabl'
require 'jbuilder'
require 'benchmark'
require 'ffaker'
require 'multi_json'
require 'oj'
#th 100_000 records
# user system total real
#AMS 9.370000 0.170000 9.540000 ( 9.571351)
#Roar 17.550000 0.230000 17.780000 ( 17.849567)
#RABL 3.330000 0.070000 3.400000 ( 3.409328)
#JB 9.870000 0.130000 10.000000 ( 10.036196)
#with 2000 records
# user system total real
#AMS 0.150000 0.000000 0.150000 ( 0.152790)
#Roar 0.210000 0.000000 0.210000 ( 0.211740)
#RABL 0.040000 0.010000 0.050000 ( 0.034003)
#JB 0.130000 0.000000 0.130000 ( 0.129387)#MultiJson.use :json_gem
#MultiJson.use :json_gem
#with 100_000 records
# user system total real
#AMS 9.040000 0.160000 9.200000 ( 9.236191)
#Roar 11.380000 0.130000 11.510000 ( 11.541029)
#RABL 3.100000 0.070000 3.170000 ( 3.191567)
#JB 2.380000 0.070000 2.450000 ( 2.458665)
#with 2000 records
# user system total real
#AMS 0.130000 0.000000 0.130000 ( 0.135257)
#Roar 0.130000 0.000000 0.130000 ( 0.126143)
#RABL 0.040000 0.000000 0.040000 ( 0.038177)
#JB 0.020000 0.000000 0.020000 ( 0.028446)
MultiJson.use :oj
Post = Struct.new(:id, :author, :body, :draft) do
include ActiveModel::Serializers::JSON
end
posts = []
(1..100_000).each do |id|
posts << Post.new(id: id, author: FFaker::Name.name, body: FFaker::HipsterIpsum.paragraph, draft: [true, false].sample)
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :author, :body, :draft
end
module PostsRepresenter
include Roar::JSON
property :id
property :author
property :body
property :draft
end
def rabl_template
'
collection @posts
attributes :id, :author, :body, :draft
'
end
puts '_' * 88
def ams_test(posts)
ActiveModel::Serializer::CollectionSerializer.new(posts, each_serializer: PostSerializer).to_json
end
def roar_test(posts)
PostsRepresenter.for_collection.prepare(posts).to_json
end
def rabl_test(posts)
Rabl.render(posts, rabl_template)
end
def jbuilder_test(posts)
Jbuilder.encode do |json|
json.array! posts, :id, :author, :body, :draft
end
end
puts 'with 100_000 records'
Benchmark.bm do |x|
x.report('AMS') { ams_test(posts) }
x.report('Roar') { roar_test(posts) }
x.report('RABL') { rabl_test(posts) }
x.report('JB') { jbuilder_test(posts) }
end
puts "with 2000 records"
first2k = posts.take(2000)
Benchmark.bm do |x|
x.report('AMS') { ams_test(first2k) }
x.report('Roar') { roar_test(first2k) }
x.report('RABL') { rabl_test(first2k) }
x.report('JB') { jbuilder_test(first2k) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment