From b858ffeb0681f82b0d3a3b3edf57ba38e582999d Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Mon, 19 Oct 2020 13:30:20 +0545 Subject: [PATCH] added pure decryption speed output (#470) Co-authored-by: Logan007 --- tools/benchmark.c | 232 +++++++++++++++++++++------------------------- 1 file changed, 107 insertions(+), 125 deletions(-) diff --git a/tools/benchmark.c b/tools/benchmark.c index 276948d..230b2b0 100644 --- a/tools/benchmark.c +++ b/tools/benchmark.c @@ -18,7 +18,7 @@ #include "n2n.h" -#define DURATION 3 // test duration per algorithm +#define DURATION 2.5 // test duration per algorithm /* heap allocation for compression as per lzo example doc */ @@ -53,27 +53,6 @@ static void run_compression_benchmark(void); static void run_hashing_benchmark(void); -static int perform_decryption = 0; - - -static void usage() { - fprintf(stderr, "Usage: benchmark [-d]\n" - " -d\t\tEnable decryption. Default: only encryption is performed\n"); - exit(1); -} - -static void parseArgs(int argc, char * argv[]) { - if((argc != 1) && (argc != 2)) - usage(); - - if(argc == 2) { - if(strcmp(argv[1], "-d") != 0) - usage(); - - perform_decryption = 1; - } -} - int main(int argc, char * argv[]) { uint8_t pktbuf[N2N_PKT_BUF_SIZE]; n2n_trans_op_t transop_null, transop_tf; @@ -83,7 +62,7 @@ int main(int argc, char * argv[]) { n2n_trans_op_t transop_speck; n2n_edge_conf_t conf; - parseArgs(argc, argv); + print_n2n_version(); /* Init configuration */ edge_init_conf_defaults(&conf); @@ -126,7 +105,6 @@ int main(int argc, char * argv[]) { // --- compression benchmark -------------------------------------------------------------- - static void init_compression_for_benchmark(void) { if(lzo_init() != LZO_E_OK) { @@ -151,10 +129,9 @@ static void deinit_compression_for_benchmark(void) { static void run_compression_benchmark() { - const int target_sec = DURATION; + const float target_sec = DURATION; struct timeval t1; struct timeval t2; - ssize_t nw; ssize_t target_usec = target_sec * 1e6; ssize_t tdiff; // microseconds size_t num_packets; @@ -164,109 +141,103 @@ static void run_compression_benchmark() { uint8_t deflation_buffer[N2N_PKT_BUF_SIZE]; int64_t deflated_len; + // compression + printf("{%s}\t%s\t%.1f sec\t(%u bytes)", + "lzo1x", "compr", target_sec, (unsigned int)sizeof(PKT_CONTENT)); + fflush(stdout); tdiff = 0; num_packets = 0; - - printf("%s\t{%s}\tfor %us\t(%u bytes)", perform_decryption ? "decompr" : "compr", - "lzo1x", target_sec, (unsigned int)sizeof(PKT_CONTENT)); - fflush(stdout); - - // always do compression once to determine compressed size - compression_len = N2N_PKT_BUF_SIZE; - if(!lzo1x_1_compress(PKT_CONTENT, sizeof(PKT_CONTENT), compression_buffer, &compression_len, wrkmem) == LZO_E_OK) { - traceEvent(TRACE_ERROR, "\nLZO compression error"); - exit(1); - } - nw = compression_len; - gettimeofday( &t1, NULL ); - while(tdiff < target_usec) { - if(perform_decryption) { // decompression - deflated_len = N2N_PKT_BUF_SIZE; - - lzo1x_decompress (compression_buffer, compression_len, deflation_buffer, (lzo_uint*)&deflated_len, NULL); - - if(memcmp(deflation_buffer, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) { - traceEvent(TRACE_ERROR, "\nPayload LZO decompression failed!"); - } - } else { // compression - if(!lzo1x_1_compress(PKT_CONTENT, sizeof(PKT_CONTENT), compression_buffer, &compression_len, wrkmem) == LZO_E_OK) { - traceEvent(TRACE_ERROR, "\nLZO compression error"); - exit(1); - } + compression_len = N2N_PKT_BUF_SIZE; + if(!lzo1x_1_compress(PKT_CONTENT, sizeof(PKT_CONTENT), compression_buffer, &compression_len, wrkmem) == LZO_E_OK) { + printf("\n\t compression error\n"); + exit(1); } - gettimeofday( &t2, NULL ); - tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); num_packets++; } - mpps = num_packets / (tdiff / 1e6) / 1e6; + printf(" ---> (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", + (unsigned int)compression_len, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); - printf(" %s (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", perform_decryption ? "<---" : "--->", - (unsigned int)nw, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); - -#ifdef N2N_HAVE_ZSTD + // decompression + printf("\t%s\t%.1f sec\t(%u bytes)", + "decompr", target_sec, (unsigned int)sizeof(PKT_CONTENT)); + fflush(stdout); tdiff = 0; num_packets = 0; + gettimeofday( &t1, NULL ); + while(tdiff < target_usec) { + deflated_len = N2N_PKT_BUF_SIZE; + lzo1x_decompress (compression_buffer, compression_len, deflation_buffer, (lzo_uint*)&deflated_len, NULL); + gettimeofday( &t2, NULL ); + tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); + num_packets++; + } + mpps = num_packets / (tdiff / 1e6) / 1e6; + printf(" <--- (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", + (unsigned int)compression_len, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); + if(memcmp(deflation_buffer, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) + printf("\n\tdecompression error\n"); + printf ("\n"); - printf("%s\t{%s}\tfor %us\t(%u bytes)", perform_decryption ? "decompr" : "compr", - "zstd", target_sec, (unsigned int)sizeof(PKT_CONTENT)); +#ifdef N2N_HAVE_ZSTD + // compression + printf("{%s}\t%s\t%.1f sec\t(%u bytes)", + "zstd", "compr", target_sec, (unsigned int)sizeof(PKT_CONTENT)); fflush(stdout); - - // always do compression once to determine compressed size - compression_len = N2N_PKT_BUF_SIZE; - compression_len = ZSTD_compress(compression_buffer, compression_len, PKT_CONTENT, sizeof(PKT_CONTENT), ZSTD_COMPRESSION_LEVEL) ; - if(ZSTD_isError(compression_len)) { - traceEvent(TRACE_ERROR, "\nZSTD compression error"); - exit(1); + tdiff = 0; + num_packets = 0; + gettimeofday( &t1, NULL ); + while(tdiff < target_usec) { + compression_len = N2N_PKT_BUF_SIZE; + compression_len = ZSTD_compress(compression_buffer, compression_len, PKT_CONTENT, sizeof(PKT_CONTENT), ZSTD_COMPRESSION_LEVEL) ; + if(ZSTD_isError(compression_len)) { + printf("\n\t compression error\n"); + exit(1); + } + gettimeofday( &t2, NULL ); + tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); + num_packets++; } - nw = compression_len; + mpps = num_packets / (tdiff / 1e6) / 1e6; + printf(" ---> (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", + (unsigned int)compression_len, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); + // decompression + printf("\t%s\t%.1f sec\t(%u bytes)", + "decompr", target_sec, (unsigned int)sizeof(PKT_CONTENT)); + fflush(stdout); + tdiff = 0; + num_packets = 0; gettimeofday( &t1, NULL ); - while(tdiff < target_usec) { - if(perform_decryption) { // decompression - deflated_len = N2N_PKT_BUF_SIZE; - - deflated_len = (int32_t)ZSTD_decompress (deflation_buffer, deflated_len, compression_buffer, compression_len); - if(ZSTD_isError(deflated_len)) { - traceEvent (TRACE_ERROR, "\nPayload decompression failed with zstd error '%s'.", - ZSTD_getErrorName(deflated_len)); + deflated_len = N2N_PKT_BUF_SIZE; + deflated_len = (int32_t)ZSTD_decompress (deflation_buffer, deflated_len, compression_buffer, compression_len); + if(ZSTD_isError(deflated_len)) { + printf("\n\tdecompression error '%s'\n", + ZSTD_getErrorName(deflated_len)); exit(1); - } - - if(memcmp(deflation_buffer, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) { - traceEvent(TRACE_ERROR, "\nPayload ZSTD decompression failed!"); - } - } else { // compression - compression_len = N2N_PKT_BUF_SIZE; - compression_len = ZSTD_compress(compression_buffer, compression_len, PKT_CONTENT, sizeof(PKT_CONTENT), ZSTD_COMPRESSION_LEVEL) ; - if(ZSTD_isError(compression_len)) { - traceEvent(TRACE_ERROR, "\nZSTD compression error"); - exit(1); - } } - gettimeofday( &t2, NULL ); - tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); num_packets++; } - mpps = num_packets / (tdiff / 1e6) / 1e6; - - printf(" %s (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", perform_decryption ? "<---" : "--->", - (unsigned int)nw, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); + printf(" <--- (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", + (unsigned int)compression_len, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); + if(memcmp(deflation_buffer, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) + printf("\n\tdecompression error\n"); + printf ("\n"); #endif } // --- hashing benchmark ------------------------------------------------------------------ static void run_hashing_benchmark(void) { - const int target_sec = DURATION; + const float target_sec = DURATION; struct timeval t1; struct timeval t2; ssize_t nw; @@ -276,8 +247,8 @@ static void run_hashing_benchmark(void) { uint32_t hash; - printf("%s\t(%s)\tfor %us\t(%u bytes)", "hash", - "prs32", target_sec, (unsigned int)sizeof(PKT_CONTENT)); + printf("(%s)\t%s\t%.1f sec\t(%u bytes)", + "prs32", "hash", target_sec, (unsigned int)sizeof(PKT_CONTENT)); fflush(stdout); gettimeofday( &t1, NULL ); @@ -297,6 +268,7 @@ static void run_hashing_benchmark(void) { printf(" ---> (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", (unsigned int)nw, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); + printf("\n"); } // --- cipher benchmark ------------------------------------------------------------------- @@ -305,23 +277,26 @@ static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 n2n_common_t cmn; n2n_PACKET_t pkt; n2n_mac_t mac_buf; - const int target_sec = DURATION; + uint8_t decodebuf[N2N_PKT_BUF_SIZE]; + const float target_sec = DURATION; struct timeval t1; struct timeval t2; size_t idx; size_t rem; - ssize_t nw; + size_t nw; ssize_t target_usec = target_sec * 1e6; - ssize_t tdiff = 0; // microseconds - size_t num_packets = 0; + ssize_t tdiff; // microseconds + size_t num_packets; + float mpps; - printf("%s\t[%s]\tfor %us\t(%u bytes)", perform_decryption ? "enc/dec" : "enc", - op_name, target_sec, (unsigned int)sizeof(PKT_CONTENT)); + // encryption + printf("[%s]\t%s\t%.1f sec\t(%u bytes)", + op_name, "encrypt" , target_sec, (unsigned int)sizeof(PKT_CONTENT)); fflush(stdout); - memset(mac_buf, 0, sizeof(mac_buf)); + num_packets = 0; + tdiff = 0; gettimeofday( &t1, NULL ); - while(tdiff < target_usec) { nw = do_encode_packet( pktbuf, N2N_PKT_BUF_SIZE, conf->community_name); @@ -329,30 +304,37 @@ static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 pktbuf+nw, N2N_PKT_BUF_SIZE-nw, PKT_CONTENT, sizeof(PKT_CONTENT), mac_buf); - if(perform_decryption) { - idx=0; - rem=nw; - - decode_common( &cmn, pktbuf, &rem, &idx); - decode_PACKET( &pkt, &cmn, pktbuf, &rem, &idx ); - - uint8_t decodebuf[N2N_PKT_BUF_SIZE]; - - op_fn->rev(op_fn, decodebuf, N2N_PKT_BUF_SIZE, pktbuf+idx, rem, 0); - - if(memcmp(decodebuf, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) - fprintf(stderr, "Payload decryption failed!\n"); - } - gettimeofday( &t2, NULL ); tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); num_packets++; } + mpps = num_packets / (tdiff / 1e6) / 1e6; + printf(" ---> (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", + (unsigned int)nw, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); - float mpps = num_packets / (tdiff / 1e6) / 1e6; - - printf(" %s--> (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", perform_decryption ? "<" : "-", + // decrpytion + printf("\t%s\t%.1f sec\t(%u bytes)", + "decrypt" , target_sec, (unsigned int)sizeof(PKT_CONTENT)); + fflush(stdout); + num_packets = 0; + tdiff = 0; + gettimeofday( &t1, NULL ); + while(tdiff < target_usec) { + idx=0; + rem=nw; + decode_common( &cmn, pktbuf, &rem, &idx); + decode_PACKET( &pkt, &cmn, pktbuf, &rem, &idx ); + op_fn->rev(op_fn, decodebuf, N2N_PKT_BUF_SIZE, pktbuf+idx, rem, 0); + gettimeofday( &t2, NULL ); + tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); + num_packets++; + } + mpps = num_packets / (tdiff / 1e6) / 1e6; + printf(" <--- (%u bytes)\t%12u packets\t%8.1f Kpps\t%8.1f MB/s\n", (unsigned int)nw, (unsigned int)num_packets, mpps * 1e3, mpps * sizeof(PKT_CONTENT)); + if(memcmp(decodebuf, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) + printf("\tpayload decryption failed!\n"); + printf("\n"); }