Last active
November 9, 2021 16:15
-
-
Save yuki24/2f884d9e27c6e8998ad4 to your computer and use it in GitHub Desktop.
jbuilder vs ActiveModel::Serializers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "setup" | |
require 'benchmark/ips' | |
class MoviesController < ApplicationController | |
append_view_path "." | |
def controller_path | |
"" | |
end | |
movie = Movie.new( | |
title: "Dawn of the Planet of the Apes", | |
year: 2014, | |
released: 1405062000, | |
url: "http://trakt.tv/movie/dawn-of-the-planet-of-the-apes-2014", | |
trailer: "http://youtube.com/watch?v=3wMnftb_RI4", | |
runtime: 130, | |
tagline: "One last chance for peace.", | |
overview: "A group of scientists in San Francisco struggle to stay alive in the aftermath of a plague that is wiping out humanity, while Caesar tries to maintain dominance over his community of intelligent apes.", | |
certification: "PG-13", | |
imdb_id: "tt2103281", | |
tmdb_id: "119450", | |
poster: "http://slurm.trakt.us/images/posters_movies/211624.4.jpg", | |
images: { | |
poster: "http://slurm.trakt.us/images/posters_movies/211624.4.jpg", | |
fanart: "http://slurm.trakt.us/images/fanart_movies/211624.4.jpg" | |
}, | |
watchers: 33, | |
ratings: { | |
percentage: 80, | |
votes: 1528, | |
loved: 1456, | |
hated: 72 | |
}, | |
genres: ["Action", "Drama", "Science Fiction", "Thriller"] | |
) | |
MOVIES = Array.new(15, movie) | |
def jbuilder | |
@movies = MOVIES | |
end | |
def asm | |
render text: ActiveModel::ArraySerializer.new(MOVIES, each_serializer: MovieSerializer).to_json | |
end | |
end | |
Benchmark.ips do |x| | |
env = { | |
"REQUEST_METHOD" => "GET", | |
"HTTP_ACCEPT" => "application/json", | |
"rack.input" => StringIO.new, | |
} | |
x.report 'jbuilder' do | |
MoviesController.action(:jbuilder).call(env) | |
end | |
x.report 'AM::Serializers' do | |
MoviesController.action(:asm).call(env) | |
end | |
x.compare! | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'sqlite3' | |
gem 'railties', '~> 4.2.0' | |
gem 'actionpack', '~> 4.2.0' | |
gem 'activerecord', '~> 4.2.0' | |
gem 'active_model_serializers' | |
gem 'jbuilder' | |
gem 'benchmark-ips' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: https://rubygems.org/ | |
specs: | |
actionpack (4.2.0) | |
actionview (= 4.2.0) | |
activesupport (= 4.2.0) | |
rack (~> 1.6.0) | |
rack-test (~> 0.6.2) | |
rails-dom-testing (~> 1.0, >= 1.0.5) | |
rails-html-sanitizer (~> 1.0, >= 1.0.1) | |
actionview (4.2.0) | |
activesupport (= 4.2.0) | |
builder (~> 3.1) | |
erubis (~> 2.7.0) | |
rails-dom-testing (~> 1.0, >= 1.0.5) | |
rails-html-sanitizer (~> 1.0, >= 1.0.1) | |
active_model_serializers (0.9.3) | |
activemodel (>= 3.2) | |
activemodel (4.2.0) | |
activesupport (= 4.2.0) | |
builder (~> 3.1) | |
activerecord (4.2.0) | |
activemodel (= 4.2.0) | |
activesupport (= 4.2.0) | |
arel (~> 6.0) | |
activesupport (4.2.0) | |
i18n (~> 0.7) | |
json (~> 1.7, >= 1.7.7) | |
minitest (~> 5.1) | |
thread_safe (~> 0.3, >= 0.3.4) | |
tzinfo (~> 1.1) | |
arel (6.0.0) | |
benchmark-ips (2.1.1) | |
builder (3.2.2) | |
erubis (2.7.0) | |
i18n (0.7.0) | |
jbuilder (2.2.6) | |
activesupport (>= 3.0.0, < 5) | |
multi_json (~> 1.2) | |
json (1.8.2) | |
loofah (2.0.1) | |
nokogiri (>= 1.5.9) | |
mini_portile (0.6.2) | |
minitest (5.5.1) | |
multi_json (1.10.1) | |
nokogiri (1.6.6.2) | |
mini_portile (~> 0.6.0) | |
rack (1.6.0) | |
rack-test (0.6.3) | |
rack (>= 1.0) | |
rails-deprecated_sanitizer (1.0.3) | |
activesupport (>= 4.2.0.alpha) | |
rails-dom-testing (1.0.5) | |
activesupport (>= 4.2.0.beta, < 5.0) | |
nokogiri (~> 1.6.0) | |
rails-deprecated_sanitizer (>= 1.0.1) | |
rails-html-sanitizer (1.0.1) | |
loofah (~> 2.0) | |
railties (4.2.0) | |
actionpack (= 4.2.0) | |
activesupport (= 4.2.0) | |
rake (>= 0.8.7) | |
thor (>= 0.18.1, < 2.0) | |
rake (10.4.2) | |
sqlite3 (1.3.10) | |
thor (0.19.1) | |
thread_safe (0.3.4) | |
tzinfo (1.2.2) | |
thread_safe (~> 0.1) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
actionpack (~> 4.2.0) | |
active_model_serializers | |
activerecord (~> 4.2.0) | |
benchmark-ips | |
jbuilder | |
railties (~> 4.2.0) | |
sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json.array! @movies do |movie| | |
json.extract! movie, :title, :year, :released, :url, :trailer, | |
:runtime, :tagline, :overview, :certification, | |
:imdb_id, :tmdb_id, :poster, :images, :watchers, | |
:ratings, :genres | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/setup' | |
Bundler.require | |
require 'sqlite3' | |
require 'rails' | |
require 'active_record' | |
require 'active_model_serializers' | |
require 'action_controller' | |
require 'action_view' | |
require 'jbuilder' | |
require 'jbuilder/railtie' | |
# config | |
app = Class.new(Rails::Application) | |
app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4' | |
app.config.session_store :cookie_store, :key => '_myapp_session' | |
app.config.active_support.deprecation = :log | |
app.config.eager_load = false | |
# Rais.root | |
app.config.root = File.dirname(__FILE__) | |
Rails.backtrace_cleaner.remove_silencers! | |
app.initialize! | |
ActiveRecord::Base.configurations = {'test' => {adapter: 'sqlite3', database: ':memory:'}} | |
ActiveRecord::Base.establish_connection :test | |
class Movie < ActiveRecord::Base | |
serialize :images, Hash | |
serialize :ratings, Hash | |
serialize :genres, Array | |
end | |
class CreateMovies < ActiveRecord::Migration | |
def self.up | |
create_table :movies do |t| | |
t.string :title | |
t.integer :year | |
t.integer :released | |
t.string :url | |
t.string :trailer | |
t.integer :runtime | |
t.string :tagline | |
t.text :overview | |
t.string :certification | |
t.string :imdb_id | |
t.string :tmdb_id | |
t.string :poster | |
t.text :images | |
t.integer :watchers | |
t.text :ratings | |
t.text :genres | |
t.timestamps | |
end | |
end | |
end | |
ActiveRecord::Migration.verbose = false | |
CreateMovies.up | |
class MovieSerializer < ActiveModel::Serializer | |
attributes :title, :year, :released, :url, :trailer, | |
:runtime, :tagline, :overview, :certification, | |
:imdb_id, :tmdb_id, :poster, :images, :watchers, | |
:ratings, :genres | |
end | |
class ApplicationController < ActionController::Base | |
end |
Upgraded Rails to 4.2 and tried again:
$ mkdir movies && mv jbuilder.json.jbuilder movies/
$ bundle exec ruby benchmark.rb
Calculating -------------------------------------
jbuilder 24.000 i/100ms
AM::Serializers 23.000 i/100ms
-------------------------------------------------
jbuilder 240.349 (±10.4%) i/s - 1.200k
AM::Serializers 285.601 (±14.0%) i/s - 1.403k
Comparison:
AM::Serializers: 285.6 i/s
jbuilder: 240.3 i/s - 1.19x slower
They seem no much difference on perfermance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: