blob: 7eee095376b8650f6d7b80ab09c35671f191137b [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
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -050041transport3 = Thrift::MemoryBuffer.new
42header_binary_protocol = Thrift::HeaderProtocol.new(transport3)
43
44transport4 = Thrift::MemoryBuffer.new
45header_compact_protocol = Thrift::HeaderProtocol.new(transport4, nil, Thrift::HeaderSubprotocolID::COMPACT)
46
47transport5 = Thrift::MemoryBuffer.new
48header_zlib_protocol = Thrift::HeaderProtocol.new(transport5)
49header_zlib_protocol.add_transform(Thrift::HeaderTransformID::ZLIB)
50
Kevin Clark4bd89162008-07-08 00:47:49 +000051
52ooe = Fixtures::Structs::OneOfEach.new
53ooe.im_true = true
54ooe.im_false = false
55ooe.a_bite = -42
56ooe.integer16 = 27000
57ooe.integer32 = 1<<24
58ooe.integer64 = 6000 * 1000 * 1000
59ooe.double_precision = Math::PI
60ooe.some_characters = "Debug THIS!"
61ooe.zomg_unicode = "\xd7\n\a\t"
62
63n1 = Fixtures::Structs::Nested1.new
64n1.a_list = []
65n1.a_list << ooe << ooe << ooe << ooe
66n1.i32_map = {}
67n1.i32_map[1234] = ooe
68n1.i32_map[46345] = ooe
69n1.i32_map[-34264] = ooe
70n1.i64_map = {}
71n1.i64_map[43534986783945] = ooe
72n1.i64_map[-32434639875122] = ooe
73n1.dbl_map = {}
74n1.dbl_map[324.65469834] = ooe
75n1.dbl_map[-9458672340.4986798345112] = ooe
76n1.str_map = {}
77n1.str_map['sdoperuix'] = ooe
78n1.str_map['pwoerxclmn'] = ooe
79
80n2 = Fixtures::Structs::Nested2.new
81n2.a_list = []
82n2.a_list << n1 << n1 << n1 << n1 << n1
83n2.i32_map = {}
84n2.i32_map[398345] = n1
85n2.i32_map[-2345] = n1
86n2.i32_map[12312] = n1
87n2.i64_map = {}
88n2.i64_map[2349843765934] = n1
89n2.i64_map[-123234985495] = n1
90n2.i64_map[0] = n1
91n2.dbl_map = {}
92n2.dbl_map[23345345.38927834] = n1
93n2.dbl_map[-1232349.5489345] = n1
94n2.dbl_map[-234984574.23498725] = n1
95n2.str_map = {}
96n2.str_map[''] = n1
97n2.str_map['sdflkertpioux'] = n1
98n2.str_map['sdfwepwdcjpoi'] = n1
99
100n3 = Fixtures::Structs::Nested3.new
101n3.a_list = []
102n3.a_list << n2 << n2 << n2 << n2 << n2
103n3.i32_map = {}
104n3.i32_map[398345] = n2
105n3.i32_map[-2345] = n2
106n3.i32_map[12312] = n2
107n3.i64_map = {}
108n3.i64_map[2349843765934] = n2
109n3.i64_map[-123234985495] = n2
110n3.i64_map[0] = n2
111n3.dbl_map = {}
112n3.dbl_map[23345345.38927834] = n2
113n3.dbl_map[-1232349.5489345] = n2
114n3.dbl_map[-234984574.23498725] = n2
115n3.str_map = {}
116n3.str_map[''] = n2
117n3.str_map['sdflkertpioux'] = n2
118n3.str_map['sdfwepwdcjpoi'] = n2
119
120n4 = Fixtures::Structs::Nested4.new
121n4.a_list = []
122n4.a_list << n3
123n4.i32_map = {}
124n4.i32_map[-2345] = n3
125n4.i64_map = {}
126n4.i64_map[2349843765934] = n3
127n4.dbl_map = {}
128n4.dbl_map[-1232349.5489345] = n3
129n4.str_map = {}
130n4.str_map[''] = n3
131
132
133# prof = RubyProf.profile do
134# n4.write(c_fast_binary_protocol)
135# Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol)
136# end
137#
138# printer = RubyProf::GraphHtmlPrinter.new(prof)
139# printer.print(STDOUT, :min_percent=>0)
140
141Benchmark.bmbm do |x|
142 x.report("ruby write large (1MB) structure once") do
143 n4.write(ruby_binary_protocol)
144 end
145
146 x.report("ruby read large (1MB) structure once") do
147 Fixtures::Structs::Nested4.new.read(ruby_binary_protocol)
148 end
149
150 x.report("c write large (1MB) structure once") do
151 n4.write(c_fast_binary_protocol)
152 end
153
154 x.report("c read large (1MB) structure once") do
155 Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol)
156 end
157
158
159
160 x.report("ruby write 10_000 small structures") do
161 10_000.times do
162 ooe.write(ruby_binary_protocol)
163 end
164 end
165
166 x.report("ruby read 10_000 small structures") do
167 10_000.times do
168 Fixtures::Structs::OneOfEach.new.read(ruby_binary_protocol)
169 end
170 end
171
172 x.report("c write 10_000 small structures") do
173 10_000.times do
174 ooe.write(c_fast_binary_protocol)
175 end
176 end
177
178 x.report("c read 10_000 small structures") do
179 10_000.times do
180 Fixtures::Structs::OneOfEach.new.read(c_fast_binary_protocol)
181 end
182 end
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -0500183
184 x.report("header (binary) write 10_000 small structures") do
185 10_000.times do
186 ooe.write(header_binary_protocol)
187 header_binary_protocol.trans.flush
188 end
189 end
190
191 x.report("header (binary) read 10_000 small structures") do
192 10_000.times do
193 Fixtures::Structs::OneOfEach.new.read(header_binary_protocol)
194 end
195 end
196
197 x.report("header (compact) write 10_000 small structures") do
198 10_000.times do
199 ooe.write(header_compact_protocol)
200 header_compact_protocol.trans.flush
201 end
202 end
203
204 x.report("header (compact) read 10_000 small structures") do
205 10_000.times do
206 Fixtures::Structs::OneOfEach.new.read(header_compact_protocol)
207 end
208 end
209
210 x.report("header (zlib) write 10_000 small structures") do
211 10_000.times do
212 ooe.write(header_zlib_protocol)
213 header_zlib_protocol.trans.flush
214 end
215 end
216
217 x.report("header (zlib) read 10_000 small structures") do
218 10_000.times do
219 Fixtures::Structs::OneOfEach.new.read(header_zlib_protocol)
220 end
221 end
222
Kevin Clark4bd89162008-07-08 00:47:49 +0000223end