92 lines
1.4 KiB
Ruby
92 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'bundler/inline'
|
|
|
|
gemfile ENV.key?('INSTALL_GEMS') do
|
|
source 'https://rubygems.org'
|
|
gem 'benchmark-ips'
|
|
gem 'bson'
|
|
gem 'msgpack'
|
|
gem 'cbor'
|
|
end
|
|
|
|
require 'json'
|
|
require 'yaml'
|
|
require 'benchmark/ips'
|
|
|
|
data = {
|
|
account_id: 1,
|
|
type: 'artist_update',
|
|
subject: {
|
|
id: 1,
|
|
type: 'Artist'
|
|
},
|
|
author: {
|
|
id: 1,
|
|
type: 'Account'
|
|
}
|
|
}
|
|
|
|
json = JSON.generate(data)
|
|
bson = data.to_bson
|
|
msgpack = MessagePack.pack(data)
|
|
cbor = data.to_cbor
|
|
yaml = YAML.dump data
|
|
|
|
puts "JSON (#{json.bytesize} bytes): #{json}"
|
|
puts "MessagePack (#{msgpack.bytesize} bytes): #{msgpack}"
|
|
puts "BSON (#{bson.to_s.bytesize} bytes): #{bson}"
|
|
puts "CBOR (#{cbor.to_s.bytesize} byes): #{cbor}"
|
|
puts "YAML (#{yaml.bytesize} bytes): #{yaml}"
|
|
|
|
puts "\nBenchmarks"
|
|
|
|
Benchmark.ips do |x|
|
|
x.report('json ->') do
|
|
JSON.generate(data)
|
|
end
|
|
|
|
x.report('msgpack ->') do
|
|
MessagePack.pack(data)
|
|
end
|
|
|
|
x.report('bson ->') do
|
|
data.to_bson
|
|
end
|
|
|
|
x.report('cbor ->') do
|
|
data.to_cbor
|
|
end
|
|
|
|
x.report('yaml ->') do
|
|
YAML.dump(data)
|
|
end
|
|
|
|
x.compare!
|
|
end
|
|
|
|
Benchmark.ips do |x|
|
|
x.report('<- json') do
|
|
JSON.parse(json)
|
|
end
|
|
|
|
x.report('<- msgpack') do
|
|
MessagePack.unpack(msgpack)
|
|
end
|
|
|
|
x.report('<- bson') do
|
|
bson.rewind!
|
|
Hash.from_bson(bson)
|
|
end
|
|
|
|
x.report('<- cbor') do
|
|
CBOR.decode(cbor)
|
|
end
|
|
|
|
x.report('<-- yaml') do
|
|
YAML.load(yaml)
|
|
end
|
|
|
|
x.compare!
|
|
end
|