/* * zfpm_dt_benchmark_protobuf_decode */ int zfpm_dt_benchmark_protobuf_decode(int argc, const char **argv) { int times, i, len; rib_dest_t *dest; struct route_entry *re; uint8_t msg_buf[4096]; QPB_DECLARE_STACK_ALLOCATOR(allocator, 8192); Fpm__Message *fpm_msg; QPB_INIT_STACK_ALLOCATOR(allocator); times = 100000; if (argc > 0) times = atoi(argv[0]); if (!zfpm_dt_find_route(&dest, &re)) return 1; /* * Encode the route into the message buffer once only. */ len = zfpm_protobuf_encode_route(dest, re, msg_buf, sizeof(msg_buf)); if (len <= 0) return 2; // Decode once, and display the decoded message fpm_msg = fpm__message__unpack(&allocator, len, msg_buf); if (fpm_msg) { zfpm_dt_log_fpm_message(fpm_msg); QPB_RESET_STACK_ALLOCATOR(allocator); } /* * Decode encoded message the specified number of times. */ for (i = 0; i < times; i++) { fpm_msg = fpm__message__unpack(&allocator, len, msg_buf); if (!fpm_msg) return 3; // fpm__message__free_unpacked(msg, NULL); QPB_RESET_STACK_ALLOCATOR(allocator); } return 0; }
/* * zfpm_protobuf_encode_route * * Create a protobuf message corresponding to the given route in the * given buffer space. * * Returns the number of bytes written to the buffer. 0 or a negative * value indicates an error. */ int zfpm_protobuf_encode_route(rib_dest_t *dest, struct route_entry *re, uint8_t *in_buf, size_t in_buf_len) { Fpm__Message *msg; QPB_DECLARE_STACK_ALLOCATOR(allocator, 4096); size_t len; QPB_INIT_STACK_ALLOCATOR(allocator); msg = create_route_message(&allocator, dest, re); if (!msg) { assert(0); return 0; } len = fpm__message__pack(msg, (uint8_t *)in_buf); assert(len <= in_buf_len); QPB_RESET_STACK_ALLOCATOR(allocator); return len; }