Skip to content

Instantly share code, notes, and snippets.

@lbrito1
Created March 21, 2019 17:36
Show Gist options
  • Save lbrito1/141e619a4994de2f20ae5fc57a5726be to your computer and use it in GitHub Desktop.
Save lbrito1/141e619a4994de2f20ae5fc57a5726be to your computer and use it in GitHub Desktop.
JSON Serialization Benchmark Ruby/PG
require 'benchmark'
class SerializationBenchmark
def initialize
@temp_events = []
end
def create_events
1000.times do |i|
ev = Event.first.dup
ev.title += i.to_s
ev.save(validate: false) # avoid image-related errors
@temp_events << ev
end
end
def call
# disable logger
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
puts 'Creating events...'
create_events
Event.extend(EventSerializer)
Benchmark.bm(7) do |x|
x.report("ruby") { Event.ruby_to_json }
x.report("postgres") { Event.serialize_to_json }
end
ensure
puts 'Cleaning events...'
@temp_events.each { |ev| ev.delete }
ActiveRecord::Base.logger = old_logger
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment