acl/lib_rpc/samples/fstream/main.cpp

172 lines
3.9 KiB
C++
Raw Normal View History

// protobuf_client.cpp : Defines the entry point for the console application.
2014-11-19 00:25:21 +08:00
//
#include "stdafx.h"
#include "acl_cpp/stream/ifstream.hpp"
#include "acl_cpp/stream/ofstream.hpp"
#include "acl_cpp/stdlib/string.hpp"
#include "google/protobuf/io/acl_fstream.h"
#include "../util.h"
#include "../test.pb.h"
using namespace google::protobuf::io;
static void usage(const char* procname)
{
printf("usage: %s -h [help] -n max\r\n", procname);
}
int main(int argc, char* argv[])
{
int total = 10000;
int ch;
while ((ch = getopt(argc, argv, "hn:")) > 0)
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 'n':
total = atoi(optarg);
if (total <= 0)
total = 10000;
break;
default:
break;
}
}
const char* path = "test.txt";
// <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
2014-11-19 00:25:21 +08:00
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
2014-11-19 00:25:21 +08:00
acl::ofstream out_fp;
if (out_fp.open_write(path) == false)
{
printf("open file %s error\r\n", path);
return 1;
}
// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD>
2014-11-19 00:25:21 +08:00
acl_ofstream output(&out_fp);
tutorial::AddressBook address;
size_t person_count = 5;
acl::string buf;
// <20><><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>б<EFBFBD>
2014-11-19 00:25:21 +08:00
for (size_t i = 0; i < person_count; i++)
{
tutorial::Person* person = address.add_person();
buf.format("zsxxsz-%d", (int) i);
2014-11-19 00:25:21 +08:00
person->set_name(buf.c_str());
buf.format("zsxxsz-%d@test.com", (int) i);
2014-11-19 00:25:21 +08:00
person->set_email(buf.c_str());
person->set_id(i);
// <20><>һ<EFBFBD><D2BB><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><E7BBB0><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
for (size_t j = 0; j < tutorial::Person::WORK; j++)
{
tutorial::Person::PhoneNumber* phone = person->add_phone();
buf.format("11111111-%d-%d", (int) i, (int) j);
2014-11-19 00:25:21 +08:00
phone->set_number(buf.c_str());
}
}
// <20><><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
address.SerializeToZeroCopyStream(&output);
if (output.Flush() == false)
{
printf("flush failed!\r\n");
return 1;
}
out_fp.close();
/////////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB>ļ<EFBFBD><C4BC>ж<EFBFBD>ȡ<EFBFBD>û<EFBFBD><C3BB><EFBFBD>Ϣ
2014-11-19 00:25:21 +08:00
// <20>򿪱<EFBFBD><F2BFAAB1><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
acl::ifstream in_fp;
if (in_fp.open_read(path) == false)
{
printf("open file %s error\r\n", path);
return 1;
}
// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD>
2014-11-19 00:25:21 +08:00
acl_ifstream input(&in_fp);
address.Clear();
// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ϣ
2014-11-19 00:25:21 +08:00
if (!address.ParseFromZeroCopyStream(&input))
{
printf("parse file %s failed\r\n", path);
return 1;
}
// <20>г<EFBFBD><D0B3><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>Ϣ
2014-11-19 00:25:21 +08:00
for (int i = 0; i < address.person_size(); i++)
{
const tutorial::Person& person = address.person(i);
printf("person->name: %s\r\n", person.name().c_str());
printf("person->id: %d\r\n", person.id());
printf("person->email: %s\r\n", person.has_email() ?
person.email().c_str() : "null");
// <20>г<EFBFBD><D0B3><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е
2014-11-19 00:25:21 +08:00
for (int j = 0; j < person.phone_size(); j++)
{
const tutorial::Person::PhoneNumber& phone = person.phone(j);
printf("\tphone number: %s\r\n", phone.number().c_str());
}
printf("------------------------------------------\r\n");
}
acl::string buf1;
acl::ifstream::load(path, &buf1);
struct timeval begin;
gettimeofday(&begin, NULL);
ACL_METER_TIME("begin run");
for (int i = 0; i < total; i++)
{
address.Clear();
std::string data(buf1.c_str(), buf1.length());
if (address.ParseFromString(data) == false)
{
printf("parse failed\r\n");
break;
}
if (i % 1000 == 0)
{
char tmp[64];
snprintf(tmp, sizeof(tmp), "total: %d, curr: %d", total, i);
ACL_METER_TIME(tmp);
}
}
struct timeval end;
gettimeofday(&end, NULL);
double n = util::stamp_sub(&end, &begin);
printf("total check: %d, spent: %0.2f ms, speed: %0.2f\r\n",
total, n, (total * 1000) /(n > 0 ? n : 1));
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
#ifdef WIN32
printf("Enter any key to exit\r\n");
getchar();
#endif
return 0;
}