|
| 1 | +require_relative './benchmarking_support' |
| 2 | +require_relative './app' |
| 3 | + |
| 4 | +time = 10 |
| 5 | +disable_gc = true |
| 6 | + |
| 7 | +# This is to disable any key transform effects that may impact performance |
| 8 | +ActiveModelSerializers.config.key_transform = :unaltered |
| 9 | + |
| 10 | +########################################### |
| 11 | +# Setup active record models |
| 12 | +########################################## |
| 13 | +require 'active_record' |
| 14 | +require 'sqlite3' |
| 15 | + |
| 16 | +# For debugging SQL output |
| 17 | +# ActiveRecord::Base.logger = Logger.new(STDERR) |
| 18 | + |
| 19 | +# Change the following to reflect your database settings |
| 20 | +ActiveRecord::Base.establish_connection( |
| 21 | + adapter: 'sqlite3', |
| 22 | + database: ':memory:' |
| 23 | +) |
| 24 | + |
| 25 | +# Don't show migration output when constructing fake db |
| 26 | +ActiveRecord::Migration.verbose = false |
| 27 | + |
| 28 | +ActiveRecord::Schema.define do |
| 29 | + create_table :authors, force: true do |t| |
| 30 | + t.string :name |
| 31 | + end |
| 32 | + |
| 33 | + create_table :posts, force: true do |t| |
| 34 | + t.text :body |
| 35 | + t.string :title |
| 36 | + t.references :author |
| 37 | + end |
| 38 | + |
| 39 | + create_table :profiles, force: true do |t| |
| 40 | + t.text :project_url |
| 41 | + t.text :bio |
| 42 | + t.date :birthday |
| 43 | + t.references :author |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +class Author < ActiveRecord::Base |
| 48 | + has_one :profile |
| 49 | + has_many :posts |
| 50 | +end |
| 51 | + |
| 52 | +class Post < ActiveRecord::Base |
| 53 | + belongs_to :author |
| 54 | +end |
| 55 | + |
| 56 | +class Profile < ActiveRecord::Base |
| 57 | + belongs_to :author |
| 58 | +end |
| 59 | + |
| 60 | +# Build out the data to serialize |
| 61 | +author = Author.create(name: 'Preston Sego') |
| 62 | +Profile.create(project_url: 'https://github.com/NullVoxPopuli', author: author) |
| 63 | +50.times do |
| 64 | + Post.create( |
| 65 | + body: 'something about how password restrictions are evil, and less secure, and with the math to prove it.', |
| 66 | + title: 'Your bank is does not know how to do security', |
| 67 | + author: author |
| 68 | + ) |
| 69 | +end |
| 70 | + |
| 71 | +Benchmark.ams('AR: attributes', time: time, disable_gc: disable_gc) do |
| 72 | + ActiveModelSerializers::SerializableResource.new(author, adapter: :attributes, include: 'profile,posts').serializable_hash |
| 73 | +end |
| 74 | + |
| 75 | +Benchmark.ams('AR: json', time: time, disable_gc: disable_gc) do |
| 76 | + ActiveModelSerializers::SerializableResource.new(author, adapter: :json, include: 'profile,posts').serializable_hash |
| 77 | +end |
| 78 | + |
| 79 | +Benchmark.ams('AR: JSON API', time: time, disable_gc: disable_gc) do |
| 80 | + ActiveModelSerializers::SerializableResource.new(author, adapter: :json_api, include: 'profile,posts').serializable_hash |
| 81 | +end |
0 commit comments