Skip to content

Add benchmarks for adapters #1511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ end

group :development, :test do
gem 'rubocop', '~> 0.34.0', require: false
gem 'benchmark-ips'
end
52 changes: 52 additions & 0 deletions bench/adapters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'bundler'
Bundler.setup

require 'active_model_serializers'
require 'active_support/all'
require 'benchmark/ips'
require_relative './../test/fixtures/poro'

def bench_adapters(resource)
serializer = PostWithCustomKeysSerializer.new(resource)

Benchmark.ips do |x|
ActiveModel::Serializer::Adapter.adapter_map.each do |adapter_name, adapter_class|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh fun

x.report(adapter_name) { adapter_class.new(serializer).to_json } unless adapter_name == 'null'
end

x.compare!
end
end

def bench_with_associations
post = Post.new(id: 1, title: 'New Post', body: 'Body')
author = Author.new(id: 1, name: 'Ian K')
first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
post.comments = [first_comment, second_comment]
first_comment.post = post
second_comment.post = post
post.author = author
blog = Blog.new(id: 1, name: 'My Blog!!')
post.blog = blog

bench_adapters(post)
end

def bench_without_associations
post = Post.new(id: 1, title: 'New Post', body: 'Body')
bench_adapters(post)
end

puts '=== Benchmarking resource with associations ======'
puts
bench_with_associations
puts

puts '=== Benchmarking resource without associations ==='
puts
bench_without_associations
puts