|
| 1 | +JSON.zero_copy_parsing = true |
| 2 | +json = File.read('twitter.json') |
| 3 | +data = JSON.parse json |
| 4 | +cbor = CBOR.encode(data) |
| 5 | +cbor_size = cbor.bytesize |
| 6 | +msgpack = MessagePack.pack(data) |
| 7 | +msgpack_size = msgpack.bytesize |
| 8 | +json_size = json.bytesize |
| 9 | + |
| 10 | +pointer = "/statuses/50/retweeted_status/user/screen_name" |
| 11 | + |
| 12 | +puts "=" * 100 |
| 13 | +puts "EAGER DECODE BENCHMARK (twitter.json)" |
| 14 | +puts "=" * 100 |
| 15 | +GC.start |
| 16 | +# CBOR |
| 17 | +puts "\nCBOR.decode" |
| 18 | +timer = Chrono::Timer.new |
| 19 | +result = CBOR.decode(cbor) |
| 20 | +elapsed = timer.elapsed |
| 21 | + |
| 22 | +puts " Time: #{elapsed.round(9)} sec" |
| 23 | +puts " Throughput: #{(cbor_size.to_f / elapsed / 1_000_000_000).round(2)} GBps" |
| 24 | + |
| 25 | +# MessagePack |
| 26 | +puts "\nMessagePack.unpack" |
| 27 | +timer = Chrono::Timer.new |
| 28 | +result = MessagePack.unpack(msgpack) |
| 29 | +elapsed = timer.elapsed |
| 30 | + |
| 31 | +puts " Time: #{elapsed.round(9)} sec" |
| 32 | +puts " Throughput: #{(msgpack_size.to_f / elapsed / 1_000_000_000).round(2)} GBps" |
| 33 | + |
| 34 | +# JSON |
| 35 | +puts "\nJSON.parse" |
| 36 | +timer = Chrono::Timer.new |
| 37 | +result = JSON.parse(json) |
| 38 | +elapsed = timer.elapsed |
| 39 | + |
| 40 | +puts " Time: #{elapsed.round(9)} sec" |
| 41 | +puts " Throughput: #{(json_size.to_f / elapsed / 1_000_000_000).round(2)} GBps" |
| 42 | + |
| 43 | +puts "\n" + "=" * 100 |
| 44 | +puts "LAZY DECODE BENCHMARK — #{pointer}" |
| 45 | +puts "=" * 100 |
| 46 | +GC.start |
| 47 | +# CBOR Lazy |
| 48 | +puts "\nCBOR.decode_lazy" |
| 49 | +timer = Chrono::Timer.new |
| 50 | +result = CBOR.decode_lazy(cbor)["statuses"][50]["retweeted_status"]["user"]["screen_name"].value |
| 51 | +elapsed = timer.elapsed |
| 52 | + |
| 53 | +puts " Result: #{result.inspect}" |
| 54 | +puts " Time: #{elapsed.round(9)} sec" |
| 55 | +puts " Throughput: #{(cbor_size.to_f / elapsed / 1_000_000_000).round(2)} GBps" |
| 56 | + |
| 57 | +# MessagePack Lazy |
| 58 | +puts "\nMessagePack.unpack_lazy" |
| 59 | +timer = Chrono::Timer.new |
| 60 | +result = MessagePack.unpack_lazy(msgpack).at_pointer(pointer) |
| 61 | +elapsed = timer.elapsed |
| 62 | + |
| 63 | +puts " Result: #{result.inspect}" |
| 64 | +puts " Time: #{elapsed.round(9)} sec" |
| 65 | +puts " Throughput: #{(msgpack_size.to_f / elapsed / 1_000_000_000).round(2)} GBps" |
| 66 | + |
| 67 | +# JSON Lazy |
| 68 | +puts "\nJSON.parse_lazy" |
| 69 | +timer = Chrono::Timer.new |
| 70 | +result = JSON.parse_lazy(json).at_pointer(pointer) |
| 71 | +elapsed = timer.elapsed |
| 72 | + |
| 73 | +puts " Result: #{result.inspect}" |
| 74 | +puts " Time: #{elapsed.round(9)} sec" |
| 75 | +puts " Throughput: #{(json_size.to_f / elapsed / 1_000_000_000).round(2)} GBps" |
| 76 | + |
| 77 | +puts "\n" + "=" * 100 |
| 78 | +puts "Wire Sizes:" |
| 79 | +puts " CBOR: #{cbor_size} bytes" |
| 80 | +puts " MessagePack: #{msgpack_size} bytes" |
| 81 | +puts " JSON: #{json_size} bytes" |
| 82 | +puts "=" * 100 |
0 commit comments