| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 20 | $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 | |
| 23 | require 'thrift' |
| Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 24 | |
| 25 | require 'benchmark' |
| 26 | require 'rubygems' |
| 27 | require 'set' |
| 28 | require 'pp' |
| 29 | |
| 30 | # require 'ruby-debug' |
| 31 | # require 'ruby-prof' |
| 32 | |
| 33 | require File.join(File.dirname(__FILE__), '../fixtures/structs') |
| 34 | |
| 35 | transport1 = Thrift::MemoryBuffer.new |
| 36 | ruby_binary_protocol = Thrift::BinaryProtocol.new(transport1) |
| 37 | |
| 38 | transport2 = Thrift::MemoryBuffer.new |
| 39 | c_fast_binary_protocol = Thrift::BinaryProtocolAccelerated.new(transport2) |
| 40 | |
| Dmytro Shteflyuk | 67bfb29 | 2026-01-28 11:23:50 -0500 | [diff] [blame^] | 41 | transport3 = Thrift::MemoryBuffer.new |
| 42 | header_binary_protocol = Thrift::HeaderProtocol.new(transport3) |
| 43 | |
| 44 | transport4 = Thrift::MemoryBuffer.new |
| 45 | header_compact_protocol = Thrift::HeaderProtocol.new(transport4, nil, Thrift::HeaderSubprotocolID::COMPACT) |
| 46 | |
| 47 | transport5 = Thrift::MemoryBuffer.new |
| 48 | header_zlib_protocol = Thrift::HeaderProtocol.new(transport5) |
| 49 | header_zlib_protocol.add_transform(Thrift::HeaderTransformID::ZLIB) |
| 50 | |
| Kevin Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 51 | |
| 52 | ooe = Fixtures::Structs::OneOfEach.new |
| 53 | ooe.im_true = true |
| 54 | ooe.im_false = false |
| 55 | ooe.a_bite = -42 |
| 56 | ooe.integer16 = 27000 |
| 57 | ooe.integer32 = 1<<24 |
| 58 | ooe.integer64 = 6000 * 1000 * 1000 |
| 59 | ooe.double_precision = Math::PI |
| 60 | ooe.some_characters = "Debug THIS!" |
| 61 | ooe.zomg_unicode = "\xd7\n\a\t" |
| 62 | |
| 63 | n1 = Fixtures::Structs::Nested1.new |
| 64 | n1.a_list = [] |
| 65 | n1.a_list << ooe << ooe << ooe << ooe |
| 66 | n1.i32_map = {} |
| 67 | n1.i32_map[1234] = ooe |
| 68 | n1.i32_map[46345] = ooe |
| 69 | n1.i32_map[-34264] = ooe |
| 70 | n1.i64_map = {} |
| 71 | n1.i64_map[43534986783945] = ooe |
| 72 | n1.i64_map[-32434639875122] = ooe |
| 73 | n1.dbl_map = {} |
| 74 | n1.dbl_map[324.65469834] = ooe |
| 75 | n1.dbl_map[-9458672340.4986798345112] = ooe |
| 76 | n1.str_map = {} |
| 77 | n1.str_map['sdoperuix'] = ooe |
| 78 | n1.str_map['pwoerxclmn'] = ooe |
| 79 | |
| 80 | n2 = Fixtures::Structs::Nested2.new |
| 81 | n2.a_list = [] |
| 82 | n2.a_list << n1 << n1 << n1 << n1 << n1 |
| 83 | n2.i32_map = {} |
| 84 | n2.i32_map[398345] = n1 |
| 85 | n2.i32_map[-2345] = n1 |
| 86 | n2.i32_map[12312] = n1 |
| 87 | n2.i64_map = {} |
| 88 | n2.i64_map[2349843765934] = n1 |
| 89 | n2.i64_map[-123234985495] = n1 |
| 90 | n2.i64_map[0] = n1 |
| 91 | n2.dbl_map = {} |
| 92 | n2.dbl_map[23345345.38927834] = n1 |
| 93 | n2.dbl_map[-1232349.5489345] = n1 |
| 94 | n2.dbl_map[-234984574.23498725] = n1 |
| 95 | n2.str_map = {} |
| 96 | n2.str_map[''] = n1 |
| 97 | n2.str_map['sdflkertpioux'] = n1 |
| 98 | n2.str_map['sdfwepwdcjpoi'] = n1 |
| 99 | |
| 100 | n3 = Fixtures::Structs::Nested3.new |
| 101 | n3.a_list = [] |
| 102 | n3.a_list << n2 << n2 << n2 << n2 << n2 |
| 103 | n3.i32_map = {} |
| 104 | n3.i32_map[398345] = n2 |
| 105 | n3.i32_map[-2345] = n2 |
| 106 | n3.i32_map[12312] = n2 |
| 107 | n3.i64_map = {} |
| 108 | n3.i64_map[2349843765934] = n2 |
| 109 | n3.i64_map[-123234985495] = n2 |
| 110 | n3.i64_map[0] = n2 |
| 111 | n3.dbl_map = {} |
| 112 | n3.dbl_map[23345345.38927834] = n2 |
| 113 | n3.dbl_map[-1232349.5489345] = n2 |
| 114 | n3.dbl_map[-234984574.23498725] = n2 |
| 115 | n3.str_map = {} |
| 116 | n3.str_map[''] = n2 |
| 117 | n3.str_map['sdflkertpioux'] = n2 |
| 118 | n3.str_map['sdfwepwdcjpoi'] = n2 |
| 119 | |
| 120 | n4 = Fixtures::Structs::Nested4.new |
| 121 | n4.a_list = [] |
| 122 | n4.a_list << n3 |
| 123 | n4.i32_map = {} |
| 124 | n4.i32_map[-2345] = n3 |
| 125 | n4.i64_map = {} |
| 126 | n4.i64_map[2349843765934] = n3 |
| 127 | n4.dbl_map = {} |
| 128 | n4.dbl_map[-1232349.5489345] = n3 |
| 129 | n4.str_map = {} |
| 130 | n4.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 | |
| 141 | Benchmark.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 Shteflyuk | 67bfb29 | 2026-01-28 11:23:50 -0500 | [diff] [blame^] | 183 | |
| 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 Clark | 4bd8916 | 2008-07-08 00:47:49 +0000 | [diff] [blame] | 223 | end |