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 | |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 23 | #include <iostream> |
Jim King | 9de9b1f | 2015-04-30 16:03:34 -0400 | [diff] [blame] | 24 | #define _USE_MATH_DEFINES |
| 25 | #include <math.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 26 | #include "thrift/transport/TBufferTransports.h" |
| 27 | #include "thrift/protocol/TBinaryProtocol.h" |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 28 | #include "gen-cpp/DebugProtoTest_types.h" |
Roger Meier | 6f28c04 | 2014-11-01 20:31:44 +0100 | [diff] [blame] | 29 | |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 30 | #ifdef HAVE_SYS_TIME_H |
David Reiss | 81c7fc0 | 2008-04-28 02:51:44 +0000 | [diff] [blame] | 31 | #include <sys/time.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 32 | #endif |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 33 | |
| 34 | class Timer { |
| 35 | public: |
| 36 | timeval vStart; |
| 37 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 38 | Timer() { THRIFT_GETTIMEOFDAY(&vStart, 0); } |
| 39 | void start() { THRIFT_GETTIMEOFDAY(&vStart, 0); } |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 40 | |
| 41 | double frame() { |
| 42 | timeval vEnd; |
Roger Meier | 22888ce | 2014-02-09 11:31:02 +0100 | [diff] [blame] | 43 | THRIFT_GETTIMEOFDAY(&vEnd, 0); |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 44 | double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0); |
| 45 | double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0); |
| 46 | return dend - dstart; |
| 47 | } |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | int main() { |
| 51 | using namespace std; |
| 52 | using namespace thrift::test::debug; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 53 | using namespace apache::thrift::transport; |
| 54 | using namespace apache::thrift::protocol; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 55 | using namespace boost; |
| 56 | |
| 57 | OneOfEach ooe; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 58 | ooe.im_true = true; |
| 59 | ooe.im_false = false; |
| 60 | ooe.a_bite = 0x7f; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 61 | ooe.integer16 = 27000; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 62 | ooe.integer32 = 1 << 24; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 63 | ooe.integer64 = (uint64_t)6000 * 1000 * 1000; |
| 64 | ooe.double_precision = M_PI; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 65 | ooe.some_characters = "JSON THIS! \"\1"; |
| 66 | ooe.zomg_unicode = "\xd7\n\a\t"; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 67 | ooe.base64 = "\1\2\3\255"; |
| 68 | |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame^] | 69 | int num = 100000; |
| 70 | boost::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer(num*1000)); |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 71 | |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame^] | 72 | uint8_t* data = NULL; |
| 73 | uint32_t datasize = 0; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 74 | |
| 75 | { |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame^] | 76 | buf->resetBuffer(); |
| 77 | TBinaryProtocolT<TMemoryBuffer> prot(buf); |
| 78 | double elapsed = 0.0; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 79 | Timer timer; |
| 80 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 81 | for (int i = 0; i < num; i++) { |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 82 | ooe.write(&prot); |
| 83 | } |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame^] | 84 | elapsed = timer.frame(); |
| 85 | cout << "Write big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 88 | buf->getBuffer(&data, &datasize); |
| 89 | |
| 90 | { |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame^] | 91 | boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
| 92 | TBinaryProtocolT<TMemoryBuffer> prot(buf2); |
| 93 | OneOfEach ooe2; |
| 94 | double elapsed = 0.0; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 95 | Timer timer; |
| 96 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 97 | for (int i = 0; i < num; i++) { |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 98 | ooe2.read(&prot); |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 99 | } |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame^] | 100 | elapsed = timer.frame(); |
| 101 | cout << " Read big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Ben Craig | 384f976 | 2015-07-08 20:33:03 -0500 | [diff] [blame^] | 104 | { |
| 105 | buf->resetBuffer(); |
| 106 | TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf); |
| 107 | double elapsed = 0.0; |
| 108 | Timer timer; |
| 109 | |
| 110 | for (int i = 0; i < num; i++) { |
| 111 | ooe.write(&prot); |
| 112 | } |
| 113 | elapsed = timer.frame(); |
| 114 | cout << "Write little endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 115 | } |
| 116 | |
| 117 | { |
| 118 | OneOfEach ooe2; |
| 119 | boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
| 120 | TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf2); |
| 121 | double elapsed = 0.0; |
| 122 | Timer timer; |
| 123 | |
| 124 | for (int i = 0; i < num; i++) { |
| 125 | ooe2.read(&prot); |
| 126 | } |
| 127 | elapsed = timer.frame(); |
| 128 | cout << " Read little endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 129 | } |
| 130 | |
| 131 | { |
| 132 | buf->resetBuffer(); |
| 133 | TBinaryProtocolT<TMemoryBuffer> prot(buf); |
| 134 | double elapsed = 0.0; |
| 135 | Timer timer; |
| 136 | |
| 137 | for (int i = 0; i < num; i++) { |
| 138 | ooe.write(&prot); |
| 139 | } |
| 140 | elapsed = timer.frame(); |
| 141 | cout << "Write big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 142 | } |
| 143 | |
| 144 | { |
| 145 | boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
| 146 | TBinaryProtocolT<TMemoryBuffer> prot(buf2); |
| 147 | OneOfEach ooe2; |
| 148 | double elapsed = 0.0; |
| 149 | Timer timer; |
| 150 | |
| 151 | for (int i = 0; i < num; i++) { |
| 152 | ooe2.read(&prot); |
| 153 | } |
| 154 | elapsed = timer.frame(); |
| 155 | cout << " Read big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | data = NULL; |
| 160 | datasize = 0; |
| 161 | num = 10000000; |
| 162 | |
| 163 | ListDoublePerf listDoublePerf; |
| 164 | listDoublePerf.field.reserve(num); |
| 165 | for (int x = 0; x < num; ++x) |
| 166 | listDoublePerf.field.push_back(double(x)); |
| 167 | |
| 168 | buf.reset(new TMemoryBuffer(num * 100)); |
| 169 | |
| 170 | { |
| 171 | buf->resetBuffer(); |
| 172 | TBinaryProtocolT<TMemoryBuffer> prot(buf); |
| 173 | double elapsed = 0.0; |
| 174 | Timer timer; |
| 175 | |
| 176 | listDoublePerf.write(&prot); |
| 177 | elapsed = timer.frame(); |
| 178 | cout << "Double write big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 179 | } |
| 180 | |
| 181 | buf->getBuffer(&data, &datasize); |
| 182 | |
| 183 | { |
| 184 | boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
| 185 | TBinaryProtocolT<TMemoryBuffer> prot(buf2); |
| 186 | ListDoublePerf listDoublePerf2; |
| 187 | double elapsed = 0.0; |
| 188 | Timer timer; |
| 189 | |
| 190 | listDoublePerf2.read(&prot); |
| 191 | elapsed = timer.frame(); |
| 192 | cout << " Double read big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 193 | } |
| 194 | |
| 195 | { |
| 196 | buf->resetBuffer(); |
| 197 | TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf); |
| 198 | double elapsed = 0.0; |
| 199 | Timer timer; |
| 200 | |
| 201 | listDoublePerf.write(&prot); |
| 202 | elapsed = timer.frame(); |
| 203 | cout << "Double write little endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 204 | } |
| 205 | |
| 206 | { |
| 207 | ListDoublePerf listDoublePerf2; |
| 208 | boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
| 209 | TBinaryProtocolT<TMemoryBuffer, TNetworkLittleEndian> prot(buf2); |
| 210 | double elapsed = 0.0; |
| 211 | Timer timer; |
| 212 | |
| 213 | listDoublePerf2.read(&prot); |
| 214 | elapsed = timer.frame(); |
| 215 | cout << " Double read little endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 216 | } |
| 217 | |
| 218 | { |
| 219 | buf->resetBuffer(); |
| 220 | TBinaryProtocolT<TMemoryBuffer> prot(buf); |
| 221 | double elapsed = 0.0; |
| 222 | Timer timer; |
| 223 | |
| 224 | listDoublePerf.write(&prot); |
| 225 | elapsed = timer.frame(); |
| 226 | cout << "Double write big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 227 | } |
| 228 | |
| 229 | { |
| 230 | boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
| 231 | TBinaryProtocolT<TMemoryBuffer> prot(buf2); |
| 232 | ListDoublePerf listDoublePerf2; |
| 233 | double elapsed = 0.0; |
| 234 | Timer timer; |
| 235 | |
| 236 | listDoublePerf2.read(&prot); |
| 237 | elapsed = timer.frame(); |
| 238 | cout << " Double read big endian: " << num / (1000 * elapsed) << " kHz" << endl; |
| 239 | } |
| 240 | |
| 241 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |