File: shootout_bm.rb

package info (click to toggle)
ruby-msgpack 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 972 kB
  • sloc: ruby: 4,789; ansic: 4,309; java: 1,809; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# encoding: utf-8

if RUBY_PLATFORM.include?('java')
  # JRuby should use this library, MRI should use the standard gem
  $: << File.expand_path('../../../lib', __FILE__)
end

require 'viiite'
require 'msgpack'
require 'json'
require 'bson'

if RUBY_PLATFORM.include?('java')
  BSON_IMPL = BSON::BSON_JAVA
else
  BSON_IMPL = BSON::BSON_C
end

OBJECT_STRUCTURE = {'x' => ['y', 34, 2**30 + 3, 2.1223423423356, {'hello' => 'world', '5' => [63, 'asjdl']}]}
ENCODED_MSGPACK = "\x81\xA1x\x95\xA1y\"\xCE@\x00\x00\x03\xCB@\x00\xFA\x8E\x9F9\xFA\xC1\x82\xA5hello\xA5world\xA15\x92?\xA5asjdl"
ENCODED_BSON = "d\x00\x00\x00\x04x\x00\\\x00\x00\x00\x020\x00\x02\x00\x00\x00y\x00\x101\x00\"\x00\x00\x00\x102\x00\x03\x00\x00@\x013\x00\xC1\xFA9\x9F\x8E\xFA\x00@\x034\x002\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x045\x00\x19\x00\x00\x00\x100\x00?\x00\x00\x00\x021\x00\x06\x00\x00\x00asjdl\x00\x00\x00\x00\x00"
ENCODED_JSON = '{"x":["y",34,1073741827,2.1223423423356,{"hello":"world","5":[63,"asjdl"]}]}'
ITERATIONS = 1_00_000
IMPLEMENTATIONS = ENV.fetch('IMPLEMENTATIONS', 'json,bson,msgpack').split(',').map(&:to_sym)

Viiite.bm do |b|
  b.variation_point :ruby, Viiite.which_ruby
  
  IMPLEMENTATIONS.each do |lib|
    b.variation_point :lib, lib
    
  
    b.report(:pack) do
      ITERATIONS.times do
        case lib
        when :msgpack then MessagePack.pack(OBJECT_STRUCTURE)
        when :bson then BSON_IMPL.serialize(OBJECT_STRUCTURE).to_s
        when :json then OBJECT_STRUCTURE.to_json
        end
      end
    end
  
    b.report(:unpack) do
      ITERATIONS.times do
        case lib
        when :msgpack then MessagePack.unpack(ENCODED_MSGPACK)
        when :bson then BSON_IMPL.deserialize(ENCODED_BSON)
        when :json then JSON.parse(ENCODED_JSON)
        end
      end
    end
    
    b.report(:pack_unpack) do
      ITERATIONS.times do
        case lib
        when :msgpack then MessagePack.unpack(MessagePack.pack(OBJECT_STRUCTURE))
        when :bson then BSON_IMPL.deserialize(BSON_IMPL.serialize(OBJECT_STRUCTURE).to_s)
        when :json then JSON.parse(OBJECT_STRUCTURE.to_json)
        end
      end
    end

    b.report(:unpack_pack) do
      ITERATIONS.times do
        case lib
        when :msgpack then MessagePack.pack(MessagePack.unpack(ENCODED_MSGPACK))
        when :bson then BSON_IMPL.serialize(BSON_IMPL.deserialize(ENCODED_BSON)).to_s
        when :json then OBJECT_STRUCTURE.to_json(JSON.parse(ENCODED_JSON))
        end
      end
    end
  end
end