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