Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.2 KiB
73 lines
2.2 KiB
16 years ago
|
|
||
16 years ago
|
#include <time.h>
|
||
|
#include "google_speed.pb.h"
|
||
16 years ago
|
#include "test_util.h"
|
||
16 years ago
|
#include "upb_context.h"
|
||
|
#include "upb_msg.h"
|
||
|
|
||
|
int main ()
|
||
|
{
|
||
|
struct upb_context c;
|
||
|
upb_context_init(&c);
|
||
|
struct upb_string fds;
|
||
16 years ago
|
if(!upb_strreadfile("google_messages.proto.bin", &fds)) {
|
||
16 years ago
|
fprintf(stderr, "Couldn't read google_speed.proto.bin.\n");
|
||
|
return 1;
|
||
|
}
|
||
|
if(!upb_context_parsefds(&c, &fds)) {
|
||
|
fprintf(stderr, "Error parsing or resolving proto.\n");
|
||
|
return 1;
|
||
|
}
|
||
|
upb_strfree(fds);
|
||
16 years ago
|
char class_name[] = "benchmarks.SpeedMessage2";
|
||
|
struct upb_string proto_name;
|
||
|
proto_name.ptr = class_name;
|
||
|
proto_name.byte_len = sizeof(class_name)-1;
|
||
16 years ago
|
struct upb_symtab_entry *e = upb_context_lookup(&c, &proto_name);
|
||
|
if(!e || e->type != UPB_SYM_MESSAGE) {
|
||
|
fprintf(stderr, "Error finding symbol '" UPB_STRFMT "'.\n",
|
||
|
UPB_STRARG(proto_name));
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
struct upb_msg *m = e->ref.msg;
|
||
|
struct upb_msg_parse_state s;
|
||
|
void *data = upb_msgdata_new(m);
|
||
|
upb_msg_parse_init(&s, data, m, false, true);
|
||
|
size_t read;
|
||
|
struct upb_string str;
|
||
|
if(!upb_strreadfile("google_message2.dat", &str)) {
|
||
|
fprintf(stderr, "Error reading google_message2.dat\n");
|
||
|
return 1;
|
||
|
}
|
||
16 years ago
|
size_t total = 0;
|
||
16 years ago
|
clock_t before = clock();
|
||
|
for(int i = 0; i < 2000; i++) {
|
||
16 years ago
|
upb_msg_parse_reset(&s, data, m, false, true);
|
||
|
upb_status_t status = upb_msg_parse(&s, str.ptr, str.byte_len, &read);
|
||
|
if(status != UPB_STATUS_OK && read != str.byte_len) {
|
||
16 years ago
|
fprintf(stderr, "Error. :( error=%d, read=%lu\n", status, read);
|
||
16 years ago
|
return 1;
|
||
|
}
|
||
|
total += str.byte_len;
|
||
|
}
|
||
16 years ago
|
double elapsed = ((double)clock() - before) / CLOCKS_PER_SEC;
|
||
|
fprintf(stderr, "Parsed %sB, ", eng(total, 3, false));
|
||
|
fprintf(stderr, "%sB/s\n", eng(total/elapsed, 3, false));
|
||
16 years ago
|
upb_msg_parse_free(&s);
|
||
|
upb_msgdata_free(data, m, true);
|
||
|
upb_context_free(&c);
|
||
|
upb_strfree(str);
|
||
16 years ago
|
|
||
|
//benchmarks::SpeedMessage2 msg;
|
||
|
//std::string stlstr(str.ptr, str.byte_len);
|
||
|
//before = clock();
|
||
|
//for(int i = 0; i < 2000; i++) {
|
||
|
// msg.ParseFromString(stlstr);
|
||
|
// total += str.byte_len;
|
||
|
//}
|
||
|
//elapsed = ((double)clock() - before) / CLOCKS_PER_SEC;
|
||
|
//fprintf(stderr, "Parsed %sB, ", eng(total, 3, false));
|
||
|
//fprintf(stderr, "%sB/s\n", eng(total/elapsed, 3, false));
|
||
16 years ago
|
}
|