blob: 9d96d0813f4b87330d675784d5fdd182737e3f0a [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>
Roger Meier49ff8b12012-04-13 09:12:31 +000026#include "thrift/transport/TBufferTransports.h"
27#include "thrift/protocol/TBinaryProtocol.h"
David Reiss2a4bfd62008-04-07 23:45:00 +000028#include "gen-cpp/DebugProtoTest_types.h"
Roger Meier6f28c042014-11-01 20:31:44 +010029
Roger Meier2fa9c312011-09-05 19:15:53 +000030#ifdef HAVE_SYS_TIME_H
David Reiss81c7fc02008-04-28 02:51:44 +000031#include <sys/time.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000032#endif
David Reiss2a4bfd62008-04-07 23:45:00 +000033
34class Timer {
35public:
36 timeval vStart;
37
Konrad Grochowski16a23a62014-11-13 15:33:38 +010038 Timer() { THRIFT_GETTIMEOFDAY(&vStart, 0); }
39 void start() { THRIFT_GETTIMEOFDAY(&vStart, 0); }
David Reiss2a4bfd62008-04-07 23:45:00 +000040
41 double frame() {
42 timeval vEnd;
Roger Meier22888ce2014-02-09 11:31:02 +010043 THRIFT_GETTIMEOFDAY(&vEnd, 0);
David Reiss2a4bfd62008-04-07 23:45:00 +000044 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 Reiss2a4bfd62008-04-07 23:45:00 +000048};
49
50int main() {
51 using namespace std;
52 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;
David Reiss2a4bfd62008-04-07 23:45:00 +000055 using namespace boost;
56
57 OneOfEach ooe;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010058 ooe.im_true = true;
59 ooe.im_false = false;
60 ooe.a_bite = 0x7f;
David Reiss2a4bfd62008-04-07 23:45:00 +000061 ooe.integer16 = 27000;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010062 ooe.integer32 = 1 << 24;
David Reiss2a4bfd62008-04-07 23:45:00 +000063 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
64 ooe.double_precision = M_PI;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 ooe.some_characters = "JSON THIS! \"\1";
66 ooe.zomg_unicode = "\xd7\n\a\t";
David Reiss2a4bfd62008-04-07 23:45:00 +000067 ooe.base64 = "\1\2\3\255";
68
Roger Meier611f90c2011-12-11 22:08:51 +000069 boost::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
David Reiss2a4bfd62008-04-07 23:45:00 +000070
71 int num = 1000000;
72
73 {
74 Timer timer;
75
Konrad Grochowski16a23a62014-11-13 15:33:38 +010076 for (int i = 0; i < num; i++) {
David Reiss2a4bfd62008-04-07 23:45:00 +000077 buf->resetBuffer();
David Reisse71115b2010-10-06 17:09:56 +000078 TBinaryProtocolT<TBufferBase> prot(buf);
David Reiss2a4bfd62008-04-07 23:45:00 +000079 ooe.write(&prot);
80 }
81 cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl;
82 }
83
84 uint8_t* data;
85 uint32_t datasize;
86
87 buf->getBuffer(&data, &datasize);
88
89 {
90
91 Timer timer;
92
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093 for (int i = 0; i < num; i++) {
David Reiss2a4bfd62008-04-07 23:45:00 +000094 OneOfEach ooe2;
Roger Meier611f90c2011-12-11 22:08:51 +000095 boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
Konrad Grochowski16a23a62014-11-13 15:33:38 +010096 // buf2->resetBuffer(data, datasize);
David Reisse71115b2010-10-06 17:09:56 +000097 TBinaryProtocolT<TBufferBase> prot(buf2);
David Reiss2a4bfd62008-04-07 23:45:00 +000098 ooe2.read(&prot);
99
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100100 // cout << apache::thrift::ThriftDebugString(ooe2) << endl << endl;
David Reiss2a4bfd62008-04-07 23:45:00 +0000101 }
102 cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl;
103 }
104
David Reiss2a4bfd62008-04-07 23:45:00 +0000105 return 0;
106}