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