blob: 05a8ee5347dc9b0cf966f5e98cd48e3129d801c6 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Kevin Clark4bd89162008-07-08 00:47:49 +000020$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. .. .. lib rb lib])
21$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. .. .. lib rb ext])
22
23require 'thrift'
Kevin Clark4bd89162008-07-08 00:47:49 +000024
25require 'benchmark'
26require 'rubygems'
27require 'set'
28require 'pp'
29
30# require 'ruby-debug'
31# require 'ruby-prof'
32
33require File.join(File.dirname(__FILE__), '../fixtures/structs')
34
35transport1 = Thrift::MemoryBuffer.new
36ruby_binary_protocol = Thrift::BinaryProtocol.new(transport1)
37
38transport2 = Thrift::MemoryBuffer.new
39c_fast_binary_protocol = Thrift::BinaryProtocolAccelerated.new(transport2)
40
41
42ooe = Fixtures::Structs::OneOfEach.new
43ooe.im_true = true
44ooe.im_false = false
45ooe.a_bite = -42
46ooe.integer16 = 27000
47ooe.integer32 = 1<<24
48ooe.integer64 = 6000 * 1000 * 1000
49ooe.double_precision = Math::PI
50ooe.some_characters = "Debug THIS!"
51ooe.zomg_unicode = "\xd7\n\a\t"
52
53n1 = Fixtures::Structs::Nested1.new
54n1.a_list = []
55n1.a_list << ooe << ooe << ooe << ooe
56n1.i32_map = {}
57n1.i32_map[1234] = ooe
58n1.i32_map[46345] = ooe
59n1.i32_map[-34264] = ooe
60n1.i64_map = {}
61n1.i64_map[43534986783945] = ooe
62n1.i64_map[-32434639875122] = ooe
63n1.dbl_map = {}
64n1.dbl_map[324.65469834] = ooe
65n1.dbl_map[-9458672340.4986798345112] = ooe
66n1.str_map = {}
67n1.str_map['sdoperuix'] = ooe
68n1.str_map['pwoerxclmn'] = ooe
69
70n2 = Fixtures::Structs::Nested2.new
71n2.a_list = []
72n2.a_list << n1 << n1 << n1 << n1 << n1
73n2.i32_map = {}
74n2.i32_map[398345] = n1
75n2.i32_map[-2345] = n1
76n2.i32_map[12312] = n1
77n2.i64_map = {}
78n2.i64_map[2349843765934] = n1
79n2.i64_map[-123234985495] = n1
80n2.i64_map[0] = n1
81n2.dbl_map = {}
82n2.dbl_map[23345345.38927834] = n1
83n2.dbl_map[-1232349.5489345] = n1
84n2.dbl_map[-234984574.23498725] = n1
85n2.str_map = {}
86n2.str_map[''] = n1
87n2.str_map['sdflkertpioux'] = n1
88n2.str_map['sdfwepwdcjpoi'] = n1
89
90n3 = Fixtures::Structs::Nested3.new
91n3.a_list = []
92n3.a_list << n2 << n2 << n2 << n2 << n2
93n3.i32_map = {}
94n3.i32_map[398345] = n2
95n3.i32_map[-2345] = n2
96n3.i32_map[12312] = n2
97n3.i64_map = {}
98n3.i64_map[2349843765934] = n2
99n3.i64_map[-123234985495] = n2
100n3.i64_map[0] = n2
101n3.dbl_map = {}
102n3.dbl_map[23345345.38927834] = n2
103n3.dbl_map[-1232349.5489345] = n2
104n3.dbl_map[-234984574.23498725] = n2
105n3.str_map = {}
106n3.str_map[''] = n2
107n3.str_map['sdflkertpioux'] = n2
108n3.str_map['sdfwepwdcjpoi'] = n2
109
110n4 = Fixtures::Structs::Nested4.new
111n4.a_list = []
112n4.a_list << n3
113n4.i32_map = {}
114n4.i32_map[-2345] = n3
115n4.i64_map = {}
116n4.i64_map[2349843765934] = n3
117n4.dbl_map = {}
118n4.dbl_map[-1232349.5489345] = n3
119n4.str_map = {}
120n4.str_map[''] = n3
121
122
123# prof = RubyProf.profile do
124# n4.write(c_fast_binary_protocol)
125# Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol)
126# end
127#
128# printer = RubyProf::GraphHtmlPrinter.new(prof)
129# printer.print(STDOUT, :min_percent=>0)
130
131Benchmark.bmbm do |x|
132 x.report("ruby write large (1MB) structure once") do
133 n4.write(ruby_binary_protocol)
134 end
135
136 x.report("ruby read large (1MB) structure once") do
137 Fixtures::Structs::Nested4.new.read(ruby_binary_protocol)
138 end
139
140 x.report("c write large (1MB) structure once") do
141 n4.write(c_fast_binary_protocol)
142 end
143
144 x.report("c read large (1MB) structure once") do
145 Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol)
146 end
147
148
149
150 x.report("ruby write 10_000 small structures") do
151 10_000.times do
152 ooe.write(ruby_binary_protocol)
153 end
154 end
155
156 x.report("ruby read 10_000 small structures") do
157 10_000.times do
158 Fixtures::Structs::OneOfEach.new.read(ruby_binary_protocol)
159 end
160 end
161
162 x.report("c write 10_000 small structures") do
163 10_000.times do
164 ooe.write(c_fast_binary_protocol)
165 end
166 end
167
168 x.report("c read 10_000 small structures") do
169 10_000.times do
170 Fixtures::Structs::OneOfEach.new.read(c_fast_binary_protocol)
171 end
172 end
173
174end