void WriteOneOfEach(TProtocolFactory& prot_factory, int iters) { OneOfEach ooe; ooe.im_true = true; ooe.im_false = false; ooe.a_bite = 0xd6; ooe.integer16 = 27000; ooe.integer32 = 1<<24; ooe.integer64 = (uint64_t)6000 * 1000 * 1000; ooe.double_precision = M_PI; ooe.some_characters = "JSON THIS! \"\1"; ooe.zomg_unicode = "\xd7\n\a\t"; ooe.base64 = "\1\2\3\255"; ooe.string_string_map["one"] = "two"; ooe.string_string_hash_map["three"] = "four"; ooe.float_precision = (float)12.345; ooe.rank_map[567419810] = (float)0.211184; ooe.rank_map[507959914] = (float)0.080382; std::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); std::shared_ptr<TProtocol> generic_prot(prot_factory.getProtocol(buf)); Protocol_* prot = dynamic_cast<Protocol_*>(generic_prot.get()); if (!prot) { abort(); } for (int i = 0; i < iters; ++i) { buf->resetBuffer(); ooe.write(prot); prot->getTransport()->flush(); } }
int main() { using namespace std; using namespace thrift::test::debug; using namespace apache::thrift::transport; using namespace apache::thrift::protocol; using namespace boost; OneOfEach ooe; ooe.im_true = true; ooe.im_false = false; ooe.a_bite = 0x7f; ooe.integer16 = 27000; ooe.integer32 = 1<<24; ooe.integer64 = (uint64_t)6000 * 1000 * 1000; ooe.double_precision = M_PI; ooe.some_characters = "JSON THIS! \"\1"; ooe.zomg_unicode = "\xd7\n\a\t"; ooe.base64 = "\1\2\3\255"; boost::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); int num = 1000000; { Timer timer; for (int i = 0; i < num; i ++) { buf->resetBuffer(); TBinaryProtocolT<TBufferBase> prot(buf); ooe.write(&prot); } cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl; } uint8_t* data; uint32_t datasize; buf->getBuffer(&data, &datasize); { Timer timer; for (int i = 0; i < num; i ++) { OneOfEach ooe2; boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); //buf2->resetBuffer(data, datasize); TBinaryProtocolT<TBufferBase> prot(buf2); ooe2.read(&prot); //cout << apache::thrift::ThriftDebugString(ooe2) << endl << endl; } cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl; } return 0; }
void runTestWrite(int iters) { TBufferType_ prot; size_t bufSize = ooe.serializedSizeZC(&prot); folly::IOBufQueue queue; for (int i = 0; i < iters; i ++) { queue.clear(); prot.setOutput(&queue, bufSize); ooe.write(&prot); } buf = queue.move(); }
void ReadOneOfEach(TProtocolFactory& prot_factory, int iters) { OneOfEach ooe; ooe.im_true = true; ooe.im_false = false; ooe.a_bite = 0xd6; ooe.integer16 = 27000; ooe.integer32 = 1<<24; ooe.integer64 = (uint64_t)6000 * 1000 * 1000; ooe.double_precision = M_PI; ooe.some_characters = "JSON THIS! \"\1"; ooe.zomg_unicode = "\xd7\n\a\t"; ooe.base64 = "\1\2\3\255"; ooe.string_string_map["one"] = "two"; ooe.string_string_hash_map["three"] = "four"; ooe.float_precision = (float)12.345; ooe.rank_map[567419810] = (float)0.211184; ooe.rank_map[507959914] = (float)0.080382; std::shared_ptr<TMemoryBuffer> rbuf(new TMemoryBuffer()); std::shared_ptr<TMemoryBuffer> wbuf(new TMemoryBuffer()); std::shared_ptr<TProtocol> generic_iprot(prot_factory.getProtocol(rbuf)); std::shared_ptr<TProtocol> generic_oprot(prot_factory.getProtocol(wbuf)); Protocol_* iprot = dynamic_cast<Protocol_*>(generic_iprot.get()); Protocol_* oprot = dynamic_cast<Protocol_*>(generic_oprot.get()); if (!iprot || !oprot) { abort(); } ooe.write(oprot); oprot->getTransport()->flush(); uint8_t* data; uint32_t datasize; wbuf->getBuffer(&data, &datasize); // Don't construct a new OneOfEach object each time around the loop. // The vector insert operations in the constructor take up a significant // fraction of the time. OneOfEach ooe2; for (int i = 0; i < iters; ++i) { rbuf->resetBuffer(data, datasize); ooe2.read(iprot); } }
void runTest() { cpp2::OneOfEach ooe2; Cpp2Writer prot; Cpp2Reader protReader; // Verify writing with cpp2 size_t bufSize = ooe.serializedSize(&prot); IOBufQueue queue(IOBufQueue::cacheChainLength()); prot.setOutput(&queue, bufSize); ooe.write(&prot); bufSize = queue.chainLength(); auto buf = queue.move(); // Try deserialize with cpp2 protReader.setInput(buf.get()); ooe2.read(&protReader); testOoe(ooe2); // Try deserialize with cpp std::shared_ptr<TTransport> buf2( new TMemoryBuffer(buf->writableData(), bufSize)); CppProtocol cppProt(buf2); OneOfEach cppOoe; cppOoe.read(&cppProt); // Try to serialize with cpp buf2.reset(new TMemoryBuffer()); CppProtocol cppProt2(buf2); cppOoe.write(&cppProt2); std::string buffer = dynamic_pointer_cast<TMemoryBuffer>( buf2)->getBufferAsString(); std::unique_ptr<folly::IOBuf> buf3( folly::IOBuf::wrapBuffer(buffer.data(), buffer.size())); protReader.setInput(buf3.get()); ooe2.read(&protReader); testOoe(ooe2); }
int main() { using std::cout; using std::endl; using namespace thrift::test::debug; using apache::thrift::transport::TMemoryBuffer; using apache::thrift::protocol::TJSONProtocol; OneOfEach ooe; ooe.im_true = true; ooe.im_false = false; ooe.a_bite = 0x7f; ooe.integer16 = 27000; ooe.integer32 = 1<<24; ooe.integer64 = (uint64_t)6000 * 1000 * 1000; ooe.double_precision = M_PI; ooe.some_characters = "JSON THIS! \"\1"; ooe.zomg_unicode = "\xd7\n\a\t"; ooe.base64 = "\1\2\3\255"; cout << apache::thrift::ThriftJSONString(ooe) << endl << endl; Nesting n; n.my_ooe = ooe; n.my_ooe.integer16 = 16; n.my_ooe.integer32 = 32; n.my_ooe.integer64 = 64; n.my_ooe.double_precision = (std::sqrt(5.0)+1)/2; n.my_ooe.some_characters = ":R (me going \"rrrr\")"; n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20" "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e" "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc"; n.my_bonk.type = 31337; n.my_bonk.message = "I am a bonk... xor!"; cout << apache::thrift::ThriftJSONString(n) << endl << endl; HolyMoley hm; hm.big.push_back(ooe); hm.big.push_back(n.my_ooe); hm.big[0].a_bite = 0x22; hm.big[1].a_bite = 0x33; std::vector<std::string> stage1; stage1.push_back("and a one"); stage1.push_back("and a two"); hm.contain.insert(stage1); stage1.clear(); stage1.push_back("then a one, two"); stage1.push_back("three!"); stage1.push_back("FOUR!!"); hm.contain.insert(stage1); stage1.clear(); hm.contain.insert(stage1); std::vector<Bonk> stage2; hm.bonks["nothing"] = stage2; stage2.resize(stage2.size()+1); stage2.back().type = 1; stage2.back().message = "Wait."; stage2.resize(stage2.size()+1); stage2.back().type = 2; stage2.back().message = "What?"; hm.bonks["something"] = stage2; stage2.clear(); stage2.resize(stage2.size()+1); stage2.back().type = 3; stage2.back().message = "quoth"; stage2.resize(stage2.size()+1); stage2.back().type = 4; stage2.back().message = "the raven"; stage2.resize(stage2.size()+1); stage2.back().type = 5; stage2.back().message = "nevermore"; hm.bonks["poe"] = stage2; cout << apache::thrift::ThriftJSONString(hm) << endl << endl; boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); cout << "Testing ooe" << endl; ooe.write(proto.get()); OneOfEach ooe2; ooe2.read(proto.get()); assert(ooe == ooe2); cout << "Testing hm" << endl; hm.write(proto.get()); HolyMoley hm2; hm2.read(proto.get()); assert(hm == hm2); hm2.big[0].a_bite = 0x00; assert(hm != hm2); Doubles dub; dub.nan = HUGE_VAL/HUGE_VAL; dub.inf = HUGE_VAL; dub.neginf = -HUGE_VAL; dub.repeating = 10.0/3.0; dub.big = 1E+305; dub.small = 1E-305; dub.zero = 0.0; dub.negzero = -0.0; cout << apache::thrift::ThriftJSONString(dub) << endl << endl; cout << "Testing base" << endl; Base64 base; base.a = 123; base.b1 = "1"; base.b2 = "12"; base.b3 = "123"; base.b4 = "1234"; base.b5 = "12345"; base.b6 = "123456"; base.write(proto.get()); Base64 base2; base2.read(proto.get()); assert(base == base2); return 0; }