blob: f4cd3925698fc1550d2f60a9a1755c4d7a53a0fb [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
David Reiss2a4bfd62008-04-07 23:45:00 +000020#include <iostream>
21#include <cmath>
David Reisse71115b2010-10-06 17:09:56 +000022#include "transport/TBufferTransports.h"
23#include "protocol/TBinaryProtocol.h"
David Reiss2a4bfd62008-04-07 23:45:00 +000024#include "gen-cpp/DebugProtoTest_types.h"
25#include <time.h>
David Reiss81c7fc02008-04-28 02:51:44 +000026#include <sys/time.h>
David Reiss2a4bfd62008-04-07 23:45:00 +000027
28class Timer {
29public:
30 timeval vStart;
31
32 Timer() {
33 gettimeofday(&vStart, 0);
34 }
35 void start() {
36 gettimeofday(&vStart, 0);
37 }
38
39 double frame() {
40 timeval vEnd;
41 gettimeofday(&vEnd, 0);
42 double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0);
43 double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0);
44 return dend - dstart;
45 }
46
47};
48
49int main() {
50 using namespace std;
51 using namespace thrift::test::debug;
T Jake Lucianib5e62212009-01-31 22:36:20 +000052 using namespace apache::thrift::transport;
53 using namespace apache::thrift::protocol;
David Reiss2a4bfd62008-04-07 23:45:00 +000054 using namespace boost;
55
56 OneOfEach ooe;
57 ooe.im_true = true;
58 ooe.im_false = false;
Christian Lavoiecbf87cb2010-11-28 14:34:26 +000059 ooe.a_bite = 0x7f;
David Reiss2a4bfd62008-04-07 23:45:00 +000060 ooe.integer16 = 27000;
61 ooe.integer32 = 1<<24;
62 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
63 ooe.double_precision = M_PI;
64 ooe.some_characters = "JSON THIS! \"\1";
65 ooe.zomg_unicode = "\xd7\n\a\t";
66 ooe.base64 = "\1\2\3\255";
67
68 shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
69
70 int num = 1000000;
71
72 {
73 Timer timer;
74
75 for (int i = 0; i < num; i ++) {
76 buf->resetBuffer();
David Reisse71115b2010-10-06 17:09:56 +000077 TBinaryProtocolT<TBufferBase> prot(buf);
David Reiss2a4bfd62008-04-07 23:45:00 +000078 ooe.write(&prot);
79 }
80 cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl;
81 }
82
83 uint8_t* data;
84 uint32_t datasize;
85
86 buf->getBuffer(&data, &datasize);
87
88 {
89
90 Timer timer;
91
92 for (int i = 0; i < num; i ++) {
93 OneOfEach ooe2;
94 shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize));
95 //buf2->resetBuffer(data, datasize);
David Reisse71115b2010-10-06 17:09:56 +000096 TBinaryProtocolT<TBufferBase> prot(buf2);
David Reiss2a4bfd62008-04-07 23:45:00 +000097 ooe2.read(&prot);
98
T Jake Lucianib5e62212009-01-31 22:36:20 +000099 //cout << apache::thrift::ThriftDebugString(ooe2) << endl << endl;
David Reiss2a4bfd62008-04-07 23:45:00 +0000100 }
101 cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl;
102 }
103
104
105 return 0;
106}