| David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 1 | #include <iostream> | 
 | 2 | #include <cmath> | 
 | 3 | #include <transport/TTransportUtils.h> | 
 | 4 | #include <protocol/TBinaryProtocol.h> | 
 | 5 | #include <protocol/TJSONProtocol.h> | 
 | 6 | #include "gen-cpp/DebugProtoTest_types.h" | 
 | 7 | #include <time.h> | 
 | 8 | #include "../lib/cpp/src/protocol/TDebugProtocol.h" | 
| David Reiss | 81c7fc0 | 2008-04-28 02:51:44 +0000 | [diff] [blame] | 9 | #include <sys/time.h> | 
| David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 10 |  | 
 | 11 | class Timer { | 
 | 12 | public: | 
 | 13 |   timeval vStart; | 
 | 14 |  | 
 | 15 |   Timer() { | 
 | 16 |     gettimeofday(&vStart, 0); | 
 | 17 |   } | 
 | 18 |   void start() { | 
 | 19 |     gettimeofday(&vStart, 0); | 
 | 20 |   } | 
 | 21 |  | 
 | 22 |   double frame() { | 
 | 23 |     timeval vEnd; | 
 | 24 |     gettimeofday(&vEnd, 0); | 
 | 25 |     double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0); | 
 | 26 |     double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0); | 
 | 27 |     return dend - dstart; | 
 | 28 |   } | 
 | 29 |  | 
 | 30 | }; | 
 | 31 |  | 
 | 32 | int main() { | 
 | 33 |   using namespace std; | 
 | 34 |   using namespace thrift::test::debug; | 
 | 35 |   using namespace facebook::thrift::transport; | 
 | 36 |   using namespace facebook::thrift::protocol; | 
 | 37 |   using namespace boost; | 
 | 38 |  | 
 | 39 |   OneOfEach ooe; | 
 | 40 |   ooe.im_true   = true; | 
 | 41 |   ooe.im_false  = false; | 
 | 42 |   ooe.a_bite    = 0xd6; | 
 | 43 |   ooe.integer16 = 27000; | 
 | 44 |   ooe.integer32 = 1<<24; | 
 | 45 |   ooe.integer64 = (uint64_t)6000 * 1000 * 1000; | 
 | 46 |   ooe.double_precision = M_PI; | 
 | 47 |   ooe.some_characters  = "JSON THIS! \"\1"; | 
 | 48 |   ooe.zomg_unicode     = "\xd7\n\a\t"; | 
 | 49 |   ooe.base64 = "\1\2\3\255"; | 
 | 50 |  | 
 | 51 |   shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); | 
 | 52 |  | 
 | 53 |   int num = 1000000; | 
 | 54 |  | 
 | 55 |   { | 
 | 56 |     Timer timer; | 
 | 57 |  | 
 | 58 |     for (int i = 0; i < num; i ++) { | 
 | 59 |       buf->resetBuffer(); | 
 | 60 |       TBinaryProtocol prot(buf); | 
 | 61 |       ooe.write(&prot); | 
 | 62 |     } | 
 | 63 |     cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl; | 
 | 64 |   } | 
 | 65 |  | 
 | 66 |   uint8_t* data; | 
 | 67 |   uint32_t datasize; | 
 | 68 |  | 
 | 69 |   buf->getBuffer(&data, &datasize); | 
 | 70 |  | 
 | 71 |   { | 
 | 72 |  | 
 | 73 |     Timer timer; | 
 | 74 |  | 
 | 75 |     for (int i = 0; i < num; i ++) { | 
 | 76 |       OneOfEach ooe2; | 
 | 77 |       shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); | 
 | 78 |       //buf2->resetBuffer(data, datasize); | 
 | 79 |       TBinaryProtocol prot(buf2); | 
 | 80 |       ooe2.read(&prot); | 
 | 81 |  | 
 | 82 |       //cout << facebook::thrift::ThriftDebugString(ooe2) << endl << endl; | 
 | 83 |     } | 
 | 84 |     cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl; | 
 | 85 |   } | 
 | 86 |  | 
 | 87 |  | 
 | 88 |   return 0; | 
 | 89 | } |