blob: b9fde17a64ff2fc8edede6970c1316f9ef276445 [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'
24require 'thrift/transport'
25require 'thrift/protocol/binaryprotocol'
26require 'thrift/protocol/binaryprotocolaccelerated'
27
28require 'benchmark'
29require 'rubygems'
30require 'set'
31require 'pp'
32
33# require 'ruby-debug'
34# require 'ruby-prof'
35
36require File.join(File.dirname(__FILE__), '../fixtures/structs')
37
38transport1 = Thrift::MemoryBuffer.new
39ruby_binary_protocol = Thrift::BinaryProtocol.new(transport1)
40
41transport2 = Thrift::MemoryBuffer.new
42c_fast_binary_protocol = Thrift::BinaryProtocolAccelerated.new(transport2)
43
44
45ooe = Fixtures::Structs::OneOfEach.new
46ooe.im_true = true
47ooe.im_false = false
48ooe.a_bite = -42
49ooe.integer16 = 27000
50ooe.integer32 = 1<<24
51ooe.integer64 = 6000 * 1000 * 1000
52ooe.double_precision = Math::PI
53ooe.some_characters = "Debug THIS!"
54ooe.zomg_unicode = "\xd7\n\a\t"
55
56n1 = Fixtures::Structs::Nested1.new
57n1.a_list = []
58n1.a_list << ooe << ooe << ooe << ooe
59n1.i32_map = {}
60n1.i32_map[1234] = ooe
61n1.i32_map[46345] = ooe
62n1.i32_map[-34264] = ooe
63n1.i64_map = {}
64n1.i64_map[43534986783945] = ooe
65n1.i64_map[-32434639875122] = ooe
66n1.dbl_map = {}
67n1.dbl_map[324.65469834] = ooe
68n1.dbl_map[-9458672340.4986798345112] = ooe
69n1.str_map = {}
70n1.str_map['sdoperuix'] = ooe
71n1.str_map['pwoerxclmn'] = ooe
72
73n2 = Fixtures::Structs::Nested2.new
74n2.a_list = []
75n2.a_list << n1 << n1 << n1 << n1 << n1
76n2.i32_map = {}
77n2.i32_map[398345] = n1
78n2.i32_map[-2345] = n1
79n2.i32_map[12312] = n1
80n2.i64_map = {}
81n2.i64_map[2349843765934] = n1
82n2.i64_map[-123234985495] = n1
83n2.i64_map[0] = n1
84n2.dbl_map = {}
85n2.dbl_map[23345345.38927834] = n1
86n2.dbl_map[-1232349.5489345] = n1
87n2.dbl_map[-234984574.23498725] = n1
88n2.str_map = {}
89n2.str_map[''] = n1
90n2.str_map['sdflkertpioux'] = n1
91n2.str_map['sdfwepwdcjpoi'] = n1
92
93n3 = Fixtures::Structs::Nested3.new
94n3.a_list = []
95n3.a_list << n2 << n2 << n2 << n2 << n2
96n3.i32_map = {}
97n3.i32_map[398345] = n2
98n3.i32_map[-2345] = n2
99n3.i32_map[12312] = n2
100n3.i64_map = {}
101n3.i64_map[2349843765934] = n2
102n3.i64_map[-123234985495] = n2
103n3.i64_map[0] = n2
104n3.dbl_map = {}
105n3.dbl_map[23345345.38927834] = n2
106n3.dbl_map[-1232349.5489345] = n2
107n3.dbl_map[-234984574.23498725] = n2
108n3.str_map = {}
109n3.str_map[''] = n2
110n3.str_map['sdflkertpioux'] = n2
111n3.str_map['sdfwepwdcjpoi'] = n2
112
113n4 = Fixtures::Structs::Nested4.new
114n4.a_list = []
115n4.a_list << n3
116n4.i32_map = {}
117n4.i32_map[-2345] = n3
118n4.i64_map = {}
119n4.i64_map[2349843765934] = n3
120n4.dbl_map = {}
121n4.dbl_map[-1232349.5489345] = n3
122n4.str_map = {}
123n4.str_map[''] = n3
124
125
126# prof = RubyProf.profile do
127# n4.write(c_fast_binary_protocol)
128# Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol)
129# end
130#
131# printer = RubyProf::GraphHtmlPrinter.new(prof)
132# printer.print(STDOUT, :min_percent=>0)
133
134Benchmark.bmbm do |x|
135 x.report("ruby write large (1MB) structure once") do
136 n4.write(ruby_binary_protocol)
137 end
138
139 x.report("ruby read large (1MB) structure once") do
140 Fixtures::Structs::Nested4.new.read(ruby_binary_protocol)
141 end
142
143 x.report("c write large (1MB) structure once") do
144 n4.write(c_fast_binary_protocol)
145 end
146
147 x.report("c read large (1MB) structure once") do
148 Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol)
149 end
150
151
152
153 x.report("ruby write 10_000 small structures") do
154 10_000.times do
155 ooe.write(ruby_binary_protocol)
156 end
157 end
158
159 x.report("ruby read 10_000 small structures") do
160 10_000.times do
161 Fixtures::Structs::OneOfEach.new.read(ruby_binary_protocol)
162 end
163 end
164
165 x.report("c write 10_000 small structures") do
166 10_000.times do
167 ooe.write(c_fast_binary_protocol)
168 end
169 end
170
171 x.report("c read 10_000 small structures") do
172 10_000.times do
173 Fixtures::Structs::OneOfEach.new.read(c_fast_binary_protocol)
174 end
175 end
176
177end