cq/tests/json-vs-cbor.cr

38 lines
715 B
Crystal
Raw Normal View History

require "benchmark"
require "json"
require "cbor"
if ARGV.size == 0 || ARGV[0] == "-h"
puts "usage: #{PROGRAM_NAME} json-file"
exit 0
end
json_file = ARGV[0]
cbor_file = ARGV[0].gsub("json", "cbor")
json_content = File.read(json_file)
cbor_content = File.read(cbor_file).to_slice
json_value = JSON.parse json_content
cbor_value = CBOR::Decoder.new(cbor_content).read_value
Benchmark.ips do |bm|
bm.report("JSON.parse") do
_ = JSON.parse json_content
end
bm.report("CBOR::Decoder.new(input).read_value") do
_ = CBOR::Decoder.new(cbor_content).read_value
end
end
Benchmark.ips do |bm|
bm.report("to_json") do
_ = json_value.to_json
end
bm.report("to_cbor") do
_ = cbor_value.to_cbor
end
end