blob: 56adac0b2032eb083f940e0c1d045eaec606d9b0 [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
Roger Meier2fa9c312011-09-05 19:15:53 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
David Reiss2a4bfd62008-04-07 23:45:00 +000023#include <iostream>
Jim King9de9b1f2015-04-30 16:03:34 -040024#define _USE_MATH_DEFINES
25#include <math.h>
cyy316723a2019-01-05 16:35:14 +080026#include <memory>
Roger Meier49ff8b12012-04-13 09:12:31 +000027#include "thrift/protocol/TBinaryProtocol.h"
James E. King, III82ae9572017-08-05 12:23:54 -040028#include "thrift/transport/TBufferTransports.h"
David Reiss2a4bfd62008-04-07 23:45:00 +000029#include "gen-cpp/DebugProtoTest_types.h"
Roger Meier6f28c042014-11-01 20:31:44 +010030
Roger Meier2fa9c312011-09-05 19:15:53 +000031#ifdef HAVE_SYS_TIME_H
David Reiss81c7fc02008-04-28 02:51:44 +000032#include <sys/time.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000033#endif
David Reiss2a4bfd62008-04-07 23:45:00 +000034
35class Timer {
36public:
37 timeval vStart;
38
Sebastian Zenker042580f2019-01-29 15:48:12 +010039 Timer() { THRIFT_GETTIMEOFDAY(&vStart, nullptr); }
40 void start() { THRIFT_GETTIMEOFDAY(&vStart, nullptr); }
David Reiss2a4bfd62008-04-07 23:45:00 +000041
42 double frame() {
43 timeval vEnd;
Sebastian Zenker042580f2019-01-29 15:48:12 +010044 THRIFT_GETTIMEOFDAY(&vEnd, nullptr);
David Reiss2a4bfd62008-04-07 23:45:00 +000045 double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0);
46 double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0);
47 return dend - dstart;
48 }
David Reiss2a4bfd62008-04-07 23:45:00 +000049};
50
51int main() {
David Reiss2a4bfd62008-04-07 23:45:00 +000052 using namespace thrift::test::debug;
T Jake Lucianib5e62212009-01-31 22:36:20 +000053 using namespace apache::thrift::transport;
54 using namespace apache::thrift::protocol;
James E. King, III82ae9572017-08-05 12:23:54 -040055 using std::cout;
56 using std::endl;
David Reiss2a4bfd62008-04-07 23:45:00 +000057
58 OneOfEach ooe;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059 ooe.im_true = true;
60 ooe.im_false = false;
61 ooe.a_bite = 0x7f;
David Reiss2a4bfd62008-04-07 23:45:00 +000062 ooe.integer16 = 27000;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010063 ooe.integer32 = 1 << 24;
David Reiss2a4bfd62008-04-07 23:45:00 +000064 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
65 ooe.double_precision = M_PI;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066 ooe.some_characters = "JSON THIS! \"\1";
67 ooe.zomg_unicode = "\xd7\n\a\t";
David Reiss2a4bfd62008-04-07 23:45:00 +000068 ooe.base64 = "\1\2\3\255";
69
Ben Craig384f9762015-07-08 20:33:03 -050070 int num = 100000;
cyy316723a2019-01-05 16:35:14 +080071 std::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer(num*1000));
David Reiss2a4bfd62008-04-07 23:45:00 +000072
Sebastian Zenker042580f2019-01-29 15:48:12 +010073 uint8_t* data = nullptr;
Ben Craig384f9762015-07-08 20:33:03 -050074 uint32_t datasize = 0;
David Reiss2a4bfd62008-04-07 23:45:00 +000075
76 {
Ben Craig384f9762015-07-08 20:33:03 -050077 buf->resetBuffer();
78 TBinaryProtocolT<TMemoryBuffer> prot(buf);
79 double elapsed = 0.0;
David Reiss2a4bfd62008-04-07 23:45:00 +000080 Timer timer;
81
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082 for (int i = 0; i < num; i++) {
David Reiss2a4bfd62008-04-07 23:45:00 +000083 ooe.write(&prot);
84 }
Ben Craig384f9762015-07-08 20:33:03 -050085 elapsed = timer.frame();
86 cout << "Write big endian: " << num / (1000 * elapsed) << " kHz" << endl;
David Reiss2a4bfd62008-04-07 23:45:00 +000087 }
88
David Reiss2a4bfd62008-04-07 23:45:00 +000089 buf->getBuffer(&data, &datasize);
90
91 {
cyy316723a2019-01-05 16:35:14 +080092 std::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
Ben Craig384f9762015-07-08 20:33:03 -050093 TBinaryProtocolT<TMemoryBuffer> prot(buf2);
94 OneOfEach ooe2;
95 double elapsed = 0.0;
David Reiss2a4bfd62008-04-07 23:45:00 +000096 Timer timer;
97
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098 for (int i = 0; i < num; i++) {
David Reiss2a4bfd62008-04-07 23:45:00 +000099 ooe2.read(&prot);
David Reiss2a4bfd62008-04-07 23:45:00 +0000100 }
Ben Craig384f9762015-07-08 20:33:03 -0500101 elapsed = timer.frame();
102 cout << " Read big endian: " << num / (1000 * elapsed) << " kHz" << endl;
David Reiss2a4bfd62008-04-07 23:45:00 +0000103 }
104
Ben Craig384f9762015-07-08 20:33:03 -0500105 {
106 buf->resetBuffer();
107 TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf);
108 double elapsed = 0.0;
109 Timer timer;
110
111 for (int i = 0; i < num; i++) {
112 ooe.write(&prot);
113 }
114 elapsed = timer.frame();
115 cout << "Write little endian: " << num / (1000 * elapsed) << " kHz" << endl;
116 }
117
118 {
119 OneOfEach ooe2;
cyy316723a2019-01-05 16:35:14 +0800120 std::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
Ben Craig384f9762015-07-08 20:33:03 -0500121 TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf2);
122 double elapsed = 0.0;
123 Timer timer;
124
125 for (int i = 0; i < num; i++) {
126 ooe2.read(&prot);
127 }
128 elapsed = timer.frame();
129 cout << " Read little endian: " << num / (1000 * elapsed) << " kHz" << endl;
130 }
131
132 {
133 buf->resetBuffer();
134 TBinaryProtocolT<TMemoryBuffer> prot(buf);
135 double elapsed = 0.0;
136 Timer timer;
137
138 for (int i = 0; i < num; i++) {
139 ooe.write(&prot);
140 }
141 elapsed = timer.frame();
142 cout << "Write big endian: " << num / (1000 * elapsed) << " kHz" << endl;
143 }
144
145 {
cyy316723a2019-01-05 16:35:14 +0800146 std::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
Ben Craig384f9762015-07-08 20:33:03 -0500147 TBinaryProtocolT<TMemoryBuffer> prot(buf2);
148 OneOfEach ooe2;
149 double elapsed = 0.0;
150 Timer timer;
151
152 for (int i = 0; i < num; i++) {
153 ooe2.read(&prot);
154 }
155 elapsed = timer.frame();
156 cout << " Read big endian: " << num / (1000 * elapsed) << " kHz" << endl;
157 }
158
159
Sebastian Zenker042580f2019-01-29 15:48:12 +0100160 data = nullptr;
Ben Craig384f9762015-07-08 20:33:03 -0500161 datasize = 0;
162 num = 10000000;
163
164 ListDoublePerf listDoublePerf;
165 listDoublePerf.field.reserve(num);
166 for (int x = 0; x < num; ++x)
167 listDoublePerf.field.push_back(double(x));
168
169 buf.reset(new TMemoryBuffer(num * 100));
170
171 {
172 buf->resetBuffer();
173 TBinaryProtocolT<TMemoryBuffer> prot(buf);
174 double elapsed = 0.0;
175 Timer timer;
176
177 listDoublePerf.write(&prot);
178 elapsed = timer.frame();
179 cout << "Double write big endian: " << num / (1000 * elapsed) << " kHz" << endl;
180 }
181
182 buf->getBuffer(&data, &datasize);
183
184 {
cyy316723a2019-01-05 16:35:14 +0800185 std::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
Ben Craig384f9762015-07-08 20:33:03 -0500186 TBinaryProtocolT<TMemoryBuffer> prot(buf2);
187 ListDoublePerf listDoublePerf2;
188 double elapsed = 0.0;
189 Timer timer;
190
191 listDoublePerf2.read(&prot);
192 elapsed = timer.frame();
193 cout << " Double read big endian: " << num / (1000 * elapsed) << " kHz" << endl;
194 }
195
196 {
197 buf->resetBuffer();
198 TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf);
199 double elapsed = 0.0;
200 Timer timer;
201
202 listDoublePerf.write(&prot);
203 elapsed = timer.frame();
204 cout << "Double write little endian: " << num / (1000 * elapsed) << " kHz" << endl;
205 }
206
207 {
208 ListDoublePerf listDoublePerf2;
cyy316723a2019-01-05 16:35:14 +0800209 std::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
Ben Craig384f9762015-07-08 20:33:03 -0500210 TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf2);
211 double elapsed = 0.0;
212 Timer timer;
213
214 listDoublePerf2.read(&prot);
215 elapsed = timer.frame();
216 cout << " Double read little endian: " << num / (1000 * elapsed) << " kHz" << endl;
217 }
218
219 {
220 buf->resetBuffer();
221 TBinaryProtocolT<TMemoryBuffer> prot(buf);
222 double elapsed = 0.0;
223 Timer timer;
224
225 listDoublePerf.write(&prot);
226 elapsed = timer.frame();
227 cout << "Double write big endian: " << num / (1000 * elapsed) << " kHz" << endl;
228 }
229
230 {
cyy316723a2019-01-05 16:35:14 +0800231 std::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
Ben Craig384f9762015-07-08 20:33:03 -0500232 TBinaryProtocolT<TMemoryBuffer> prot(buf2);
233 ListDoublePerf listDoublePerf2;
234 double elapsed = 0.0;
235 Timer timer;
236
237 listDoublePerf2.read(&prot);
238 elapsed = timer.frame();
239 cout << " Double read big endian: " << num / (1000 * elapsed) << " kHz" << endl;
240 }
241
242
David Reiss2a4bfd62008-04-07 23:45:00 +0000243 return 0;
244}