blob: 4ff6a550b8841dd6b8d536d56b072a787860a9cc [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Duxburyd815c212009-03-19 18:57:43 +000020require File.dirname(__FILE__) + "/../spec/spec_helper.rb"
21require "lib/thrift/serializer"
Bryan Duxburyd1d15422009-04-04 00:58:03 +000022require "lib/thrift/protocol/binary_protocol_accelerated"
Bryan Duxburyd815c212009-03-19 18:57:43 +000023
24require "benchmark"
25# require "ruby-prof"
26
27obj = Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
28
29HOW_MANY = 1_000
30
31binser = Thrift::Serializer.new
32bin_data = binser.serialize(obj)
33bindeser = Thrift::Deserializer.new
34accel_bin_ser = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
35accel_bin_deser = Thrift::Deserializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
36
37compact_ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new)
38compact_data = compact_ser.serialize(obj)
39compact_deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new)
40
41Benchmark.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 Duxburyd1d15422009-04-04 00:58:03 +000086 # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f))
Bryan Duxburyd815c212009-03-19 18:57:43 +000087 # 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 Duxburyd1d15422009-04-04 00:58:03 +000096 # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new))
Bryan Duxburyd815c212009-03-19 18:57:43 +000097 # 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 Duxburyd1d15422009-04-04 00:58:03 +0000106 # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f))
Bryan Duxburyd815c212009-03-19 18:57:43 +0000107 # 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 Duxburyd1d15422009-04-04 00:58:03 +0000116 # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new))
Bryan Duxburyd815c212009-03-19 18:57:43 +0000117 # HOW_MANY.times do
118 # obj.read(proto)
119 # end
120 # end
121 # f.close
122
123end