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