Skip to content

Instantly share code, notes, and snippets.

@shishirmk
Created February 14, 2018 06:50
Show Gist options
  • Select an option

  • Save shishirmk/904379f99a64e2f24a055d89e08fd057 to your computer and use it in GitHub Desktop.

Select an option

Save shishirmk/904379f99a64e2f24a055d89e08fd057 to your computer and use it in GitHub Desktop.
Changes to support homogeneous list serialization in https://github.com/NullVoxPopuli/ruby-jsonapi-benchmarks
# require 'benchmark/ips'
# require 'awesome_print'
# require 'pry-byebug'
# require 'kalibera'
# require 'benchmark-memory'
require 'bundler'
require 'bundler/setup'
Bundler.require(:default)
ENV['USE_DEFAULTS'] ||= nil
ENV['TEST_OUTPUT'] ||= nil
require_relative './db/setup'
require_relative './lib/files_under_test'
require_relative './lib/serialization_helper'
require_relative './lib/configurations'
require_relative './db/seeds'
benchmarks = %i[ips]
GC.disable
if ENV['TEST_OUTPUT']
ap 'AMS'
ap SerializationHelper.ams(User.first)
ap 'jsonapi-rb'
ap SerializationHelper.jsonapi_rb(User.first)
ap 'fast_jsonapi'
ap SerializationHelper.fast_jsonapi(User.first)
exit
end
user = User.includes(:posts).first
user_list = []
puts user.first_name
[1, 25, 250, 1000].each do |x|
x.times {user_list << user}
puts ''
puts "Serializing #{x} records"
benchmarks.each do |bench|
Benchmark.send(bench) do |x|
x.config(time: 10, warmup: 5, stats: :bootstrap, confidence: 95) if x.respond_to?(:config)
# x.report('ams ') { SerializationHelper.test_render(:ams) }
# x.report('jsonapi-rb ') { SerializationHelper.test_render(:jsonapi_rb) }
# x.report('fast_jsonapi') { SerializationHelper.test_render(:fast_jsonapi) }
x.report('ams eager') { SerializationHelper.test_manual_eagerload(user_list, :ams) }
x.report('jsonapi-rb eager') { SerializationHelper.test_manual_eagerload(user_list, :jsonapi_rb) }
x.report('fast_jsonapi eager') { SerializationHelper.test_manual_eagerload(user_list, :fast_jsonapi) }
x.compare!
end
end
user_list = []
end
# freaking AMS.
Dir[File.dirname(__FILE__) + '/serializers/ams/*.rb'].each {|file| require file }
module SerializationHelper
module_function
def test_render(render_with)
render_data(
User.first,
render_with
)
end
def test_manual_eagerload(data, render_with)
render_data(
data,
render_with
)
end
def render_data(data, render_with)
send(render_with, data)
end
def ams(data)
ActiveModelSerializers::SerializableResource.new(
data,
include: 'posts',
# fields: params[:fields],
adapter: :json_api,
each_serializer: AMS::UserSerializer
).as_json
end
def jsonapi_rb(data)
JSONAPI::Serializable::Renderer.new.render(
data,
include: 'posts',
class: {
User: JsonapiRb::SerializableUser,
Post: JsonapiRb::SerializablePost,
Comment: JsonapiRb::SerializableComment
}
)
end
# NOTE: at the time of writing this, netflix doesn't support:
# - nested includes.......
# - relationship types aren't the same as the resource type...
# - _not_ rendering every id in a has_many relationship...
# by default, this means that jsonapi-rb is a clear winner...
def fast_jsonapi(data)
NetflixFastJsonapi::UserSerializer.new(data, include: [:posts]).serializable_hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment