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 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 20 | require File.dirname(__FILE__) + "/../spec/spec_helper.rb" |
| 21 | require "lib/thrift/serializer" |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame^] | 22 | require "lib/thrift/protocol/binary_protocol_accelerated" |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 23 | |
| 24 | require "benchmark" |
| 25 | # require "ruby-prof" |
| 26 | |
| 27 | obj = Fixtures::COMPACT_PROTOCOL_TEST_STRUCT |
| 28 | |
| 29 | HOW_MANY = 1_000 |
| 30 | |
| 31 | binser = Thrift::Serializer.new |
| 32 | bin_data = binser.serialize(obj) |
| 33 | bindeser = Thrift::Deserializer.new |
| 34 | accel_bin_ser = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new) |
| 35 | accel_bin_deser = Thrift::Deserializer.new(Thrift::BinaryProtocolAcceleratedFactory.new) |
| 36 | |
| 37 | compact_ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new) |
| 38 | compact_data = compact_ser.serialize(obj) |
| 39 | compact_deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new) |
| 40 | |
| 41 | Benchmark.bm(60) do |reporter| |
| 42 | reporter.report("binary protocol, write") do |
| 43 | HOW_MANY.times do |
| 44 | binser.serialize(obj) |
| 45 | end |
| 46 | end |
| 47 | |
| 48 | reporter.report("accelerated binary protocol, write") do |
| 49 | HOW_MANY.times do |
| 50 | accel_bin_ser.serialize(obj) |
| 51 | end |
| 52 | end |
| 53 | |
| 54 | reporter.report("compact protocol, write") do |
| 55 | # RubyProf.start |
| 56 | HOW_MANY.times do |
| 57 | compact_ser.serialize(obj) |
| 58 | end |
| 59 | # result = RubyProf.stop |
| 60 | # printer = RubyProf::GraphHtmlPrinter.new(result) |
| 61 | # file = File.open("profile.html", "w+") |
| 62 | # printer.print(file, 0) |
| 63 | # file.close |
| 64 | end |
| 65 | |
| 66 | reporter.report("binary protocol, read") do |
| 67 | HOW_MANY.times do |
| 68 | bindeser.deserialize(obj, bin_data) |
| 69 | end |
| 70 | end |
| 71 | |
| 72 | reporter.report("accelerated binary protocol, read") do |
| 73 | HOW_MANY.times do |
| 74 | accel_bin_deser.deserialize(obj, bin_data) |
| 75 | end |
| 76 | end |
| 77 | |
| 78 | reporter.report("compact protocol, read") do |
| 79 | HOW_MANY.times do |
| 80 | compact_deser.deserialize(obj, compact_data) |
| 81 | end |
| 82 | end |
| 83 | |
| 84 | |
| 85 | # f = File.new("/tmp/testfile", "w") |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame^] | 86 | # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f)) |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 87 | # reporter.report("accelerated binary protocol, write (to disk)") do |
| 88 | # HOW_MANY.times do |
| 89 | # obj.write(proto) |
| 90 | # end |
| 91 | # f.flush |
| 92 | # end |
| 93 | # f.close |
| 94 | # |
| 95 | # f = File.new("/tmp/testfile", "r") |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame^] | 96 | # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new)) |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 97 | # reporter.report("accelerated binary protocol, read (from disk)") do |
| 98 | # HOW_MANY.times do |
| 99 | # obj.read(proto) |
| 100 | # end |
| 101 | # end |
| 102 | # f.close |
| 103 | # |
| 104 | # f = File.new("/tmp/testfile", "w") |
| 105 | # reporter.report("compact protocol, write (to disk)") do |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame^] | 106 | # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f)) |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 107 | # HOW_MANY.times do |
| 108 | # obj.write(proto) |
| 109 | # end |
| 110 | # f.flush |
| 111 | # end |
| 112 | # f.close |
| 113 | # |
| 114 | # f = File.new("/tmp/testfile", "r") |
| 115 | # reporter.report("compact protocol, read (from disk)") do |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame^] | 116 | # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new)) |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 117 | # HOW_MANY.times do |
| 118 | # obj.read(proto) |
| 119 | # end |
| 120 | # end |
| 121 | # f.close |
| 122 | |
| 123 | end |