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
20require File.dirname(__FILE__) + "/../spec/spec_helper.rb"
21
22require "benchmark"
23# require "ruby-prof"
24
25obj = Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
26
27HOW_MANY = 1_000
28
29binser = Thrift::Serializer.new
30bin_data = binser.serialize(obj)
31bindeser = Thrift::Deserializer.new
32accel_bin_ser = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
33accel_bin_deser = Thrift::Deserializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
34
35compact_ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new)
36compact_data = compact_ser.serialize(obj)
37compact_deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new)
38
39Benchmark.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")
84  # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f))
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")
94  # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new))
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
104  #   proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f))
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
114  #   proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new))
115  #   HOW_MANY.times do
116  #     obj.read(proto)
117  #   end
118  # end
119  # f.close
120
121end
122