Forked from shilov/redis_json_marshal_eval_benchmarks.rb
Last active
December 19, 2015 17:18
-
-
Save bmalloy/5989784 to your computer and use it in GitHub Desktop.
Oj wins - ruby 2.0
This file contains hidden or 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 'benchmark' | |
require 'json' | |
require 'redis' | |
require 'oj' | |
# ----------------- | |
puts "Initialize variables.." | |
# Initialize connection to Redis via unix socket | |
redis = Redis.new | |
# Generate array containing nested hashes with random integers, ex: { 1931 => { 9159 => 9366 }, 'stringkey' => 'stringvalue' } | |
hashes = [] | |
100000.times do |i| | |
hashes[i] = { Random.rand(1..100) => { Random.rand(1..100) => Random.rand(1..100) }, 'stringkey' => 'stringvalue' } | |
end | |
puts '-----------------' | |
puts "Benchmark converting and inserting to Redis\n\n" | |
# Benchmark converting all the generated hashes into Redis | |
Benchmark.bm(20) do |x| | |
# Convert to json | |
x.report('to_json:') { hashes.each { |h| redis.set 'hash:j', h.to_json } } | |
# "{\"1931\":{\"9159\":9366},\"stringkey\":\"stringvalue\"}" | |
# Convert to bytestream | |
x.report('Marshal.dump:') { hashes.each { |h| redis.set 'hash:m', Marshal.dump(h) } } | |
# "\x04\b{\ai\x02\x8B\a{\x06i\x02\xC7#i\x02\x96$I\"\x0Estringkey\x06:\x06EFI\"\x10stringvalue\x06;\x00F" | |
# Convert to string | |
x.report('hash.to_s:') { hashes.each { |h| redis.set 'hash:s', h } } | |
# "{1931=>{9159=>9366}, \"stringkey\"=>\"stringvalue\"}" | |
# Convert to string OJ | |
x.report('Oj hash dump:') { hashes.each { |h| redis.set('hash:o', Oj.dump(h)) }} | |
# "{1931=>{9159=>9366}, \"stringkey\"=>\"stringvalue\"}" | |
end | |
puts '-----------------' | |
puts "Retrieve from Redis to compare\n\n" | |
# Retrieve from Redis | |
rawjson = redis.get 'hash:j' | |
rawbytestream = redis.get 'hash:m' | |
rawstring = redis.get 'hash:s' | |
rawstringo = redis.get 'hash:o' | |
# Convert back to a hash object | |
json = JSON.parse(rawjson) | |
bytestream = Marshal.restore(rawbytestream) | |
string = eval(rawstring) | |
puts "json == bytestream: #{json == bytestream}" # FALSE | |
puts "string == bytestream: #{string == bytestream}" # TRUE | |
puts '-----------------' | |
puts "Benchmark retrieving from Redis and converting back to a hash\n\n" | |
# Benchmark converting the hash 100000 times | |
Benchmark.bm(20) do |x| | |
x.report('JSON.parse') { 100000.times { JSON.parse(redis.get 'hash:j') } } | |
x.report('Marshal.restore') { 100000.times { Marshal.restore(redis.get 'hash:m') } } | |
x.report('eval(string)') { 100000.times { eval(redis.get 'hash:s') } } | |
x.report('Oj load') { 100000.times { Oj.load(redis.get 'hash:o') } } | |
end | |
# | |
# | |
# | |
# Initialize variables.. | |
# ----------------- | |
# Benchmark converting and inserting to Redis | |
# | |
# user system total real | |
# to_json: 15.110000 2.720000 17.830000 ( 18.295242) | |
# Marshal.dump: 13.120000 2.540000 15.660000 ( 16.096981) | |
# hash.to_s: 13.760000 2.730000 16.490000 ( 17.025083) | |
# hash to Oj: 12.910000 2.620000 15.530000 ( 16.086224) | |
# ----------------- | |
# Retrieve from Redis to compare | |
# | |
# json == bytestream: false | |
# string == bytestream: true | |
# ----------------- | |
# Benchmark retrieving from Redis and converting back to a hash | |
# | |
# | |
# user system total real | |
# JSON.parse 17.590000 2.610000 20.200000 ( 20.887079) | |
# Marshal.restore 17.000000 2.680000 19.680000 ( 20.280520) | |
# eval(string) 19.690000 2.630000 22.320000 ( 22.944768) | |
# oj 15.240000 2.670000 17.910000 ( 18.282523) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment