Skip to content

Instantly share code, notes, and snippets.

@FaviusTy
Created February 5, 2013 15:25
Show Gist options
  • Save FaviusTy/4715118 to your computer and use it in GitHub Desktop.
Save FaviusTy/4715118 to your computer and use it in GitHub Desktop.
msgpack<->pstore でベンチしてみたらJSONよりも遅いという結果に
# encoding: utf-8
require "pstore"
require "msgpack"
require "json"
require "benchmark"
_data = {test: "testdatatestdatatestdata",
data: 18358374659302536,
_float: 0.1332542572647585,
arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,],
nested: {key: "key", arr: [1, 1, 1, 000, 32435, true, false]},
bool: true
}
data = {}
read_obj = nil
(1..30000).each do |cycle|
data["#{cycle}"] = _data
end
p "msgpack+pstore"
Benchmark.bm(13) do |x|
packed = MessagePack.pack(data)
x.report("write: ") {
db = PStore.new("./backup")
db.transaction do
db["root"] = MessagePack.unpack(packed)
end
}
x.report("read: ") {
db = PStore.new("./backup")
db.transaction(true) do
read_obj = db["root"]
MessagePack.pack(read_obj)
end
}
end
p "json"
Benchmark.bm(13) do |x|
x.report("write: ") {
File.open('json', 'w') do |f|
f.flock(File::LOCK_EX)
f.write JSON.generate(data)
f.flock(File::LOCK_UN)
end
}
x.report("read: ") {
File.open('json', 'r') do |f|
f.flock(File::LOCK_EX)
read_obj = JSON.load(f)
f.flock(File::LOCK_UN)
end
}
end
@FaviusTy
Copy link
Author

FaviusTy commented Feb 5, 2013

計測結果

msgpack+pstore
user system total real
write: 0.827000 0.000000 0.827000 ( 0.836048)
read: 0.936000 0.015000 0.951000 ( 0.943054)

json
user system total real
write: 0.609000 0.016000 0.625000 ( 0.668038)
read: 0.873000 0.000000 0.873000 ( 0.878050)

Process finished with exit code 0

@FaviusTy
Copy link
Author

FaviusTy commented Feb 5, 2013

多分、msgpackを通すとHash-key値のSymbolがStringになってしまうのが負荷要因と推測する

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment