n2n-benchmark.c (n2n-3.0) | : | n2n-benchmark.c (n2n-3.1.1) | ||
---|---|---|---|---|
/* | /* | |||
* (C) 2007-21 - ntop.org and contributors | * (C) 2007-22 - ntop.org and contributors | |||
* | * | |||
* This program is free software; you can redistribute it and/or modify | * This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 3 of the License, or | * the Free Software Foundation; either version 3 of the License, or | |||
* (at your option) any later version. | * (at your option) any later version. | |||
* | * | |||
* This program is distributed in the hope that it will be useful, | * This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | * GNU General Public License for more details. | |||
skipping to change at line 30 | skipping to change at line 30 | |||
/* MinGW has undefined function gettimeofday() warnings without this header | /* MinGW has undefined function gettimeofday() warnings without this header | |||
* but Visual C++ doesnt even have the header */ | * but Visual C++ doesnt even have the header */ | |||
#include <sys/time.h> | #include <sys/time.h> | |||
#endif | #endif | |||
#include "n2n.h" | #include "n2n.h" | |||
#define DURATION 2.5 // test duration per algorithm | #define DURATION 2.5 // test duration per algorithm | |||
#define PACKETS_BEFORE_GETTIME 2047 // do not check time after every packet bu t after (2 ^ n - 1) | #define PACKETS_BEFORE_GETTIME 2047 // do not check time after every packet bu t after (2 ^ n - 1) | |||
/* heap allocation for compression as per lzo example doc */ | uint8_t PKT_CONTENT[DEFAULT_MTU]; | |||
#define HEAP_ALLOC(var,size) lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lz | ||||
o_align_t) - 1)) / sizeof(lzo_align_t) ] | ||||
static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS); | ||||
uint8_t PKT_CONTENT[]={ | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | ||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 } | ||||
; | ||||
/* Prototypes */ | /* Prototypes */ | |||
static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_com munity_t c ); | static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_com munity_t c ); | |||
static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 n_edge_conf_t *conf, uint8_t *pktbuf); | static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 n_edge_conf_t *conf, uint8_t *pktbuf); | |||
static void init_compression_for_benchmark(void); | ||||
static void deinit_compression_for_benchmark(void); | ||||
static void run_compression_benchmark(void); | ||||
static void run_hashing_benchmark(void); | static void run_hashing_benchmark(void); | |||
static void run_ecc_benchmark(void); | static void run_ecc_benchmark(void); | |||
int main(int argc, char * argv[]) { | int main(int argc, char * argv[]) { | |||
uint8_t pktbuf[N2N_PKT_BUF_SIZE]; | uint8_t pktbuf[N2N_PKT_BUF_SIZE]; | |||
size_t i; | ||||
n2n_trans_op_t transop_null, transop_tf; | n2n_trans_op_t transop_null, transop_tf; | |||
n2n_trans_op_t transop_aes; | n2n_trans_op_t transop_aes; | |||
n2n_trans_op_t transop_cc20; | n2n_trans_op_t transop_cc20; | |||
n2n_trans_op_t transop_lzo; | ||||
#ifdef HAVE_ZSTD | ||||
n2n_trans_op_t transop_zstd; | ||||
#endif | ||||
n2n_trans_op_t transop_speck; | n2n_trans_op_t transop_speck; | |||
n2n_edge_conf_t conf; | n2n_edge_conf_t conf; | |||
print_n2n_version(); | print_n2n_version(); | |||
/* Init configuration */ | /* Init configuration */ | |||
edge_init_conf_defaults(&conf); | edge_init_conf_defaults(&conf); | |||
strncpy((char*)conf.community_name, "abc123def456", sizeof(conf.community_name )); | strncpy((char*)conf.community_name, "abc123def456", sizeof(conf.community_name )); | |||
conf.encrypt_key = "SoMEVer!S$cUREPassWORD"; | conf.encrypt_key = "SoMEVer!S$cUREPassWORD"; | |||
pearson_hash_init(); | pearson_hash_init(); | |||
/* Init transopts */ | /* Init transops */ | |||
n2n_transop_null_init(&conf, &transop_null); | n2n_transop_null_init(&conf, &transop_null); | |||
n2n_transop_tf_init(&conf, &transop_tf); | n2n_transop_tf_init(&conf, &transop_tf); | |||
n2n_transop_aes_init(&conf, &transop_aes); | n2n_transop_aes_init(&conf, &transop_aes); | |||
n2n_transop_cc20_init(&conf, &transop_cc20); | n2n_transop_cc20_init(&conf, &transop_cc20); | |||
n2n_transop_speck_init(&conf, &transop_speck); | n2n_transop_speck_init(&conf, &transop_speck); | |||
n2n_transop_lzo_init(&conf, &transop_lzo); | ||||
#ifdef HAVE_ZSTD | ||||
n2n_transop_zstd_init(&conf, &transop_zstd); | ||||
#endif | ||||
/* Setup packet content */ | ||||
for (i = 0; i < sizeof(PKT_CONTENT) / sizeof(PKT_CONTENT[0]); i++) { | ||||
PKT_CONTENT[i] = i & 0x0f; | ||||
} | ||||
/* Run the tests */ | /* Run the tests */ | |||
run_transop_benchmark("null", &transop_null, &conf, pktbuf); | run_transop_benchmark("null", &transop_null, &conf, pktbuf); | |||
run_transop_benchmark("tf", &transop_tf, &conf, pktbuf); | run_transop_benchmark("tf", &transop_tf, &conf, pktbuf); | |||
run_transop_benchmark("aes", &transop_aes, &conf, pktbuf); | run_transop_benchmark("aes", &transop_aes, &conf, pktbuf); | |||
run_transop_benchmark("cc20", &transop_cc20, &conf, pktbuf); | run_transop_benchmark("cc20", &transop_cc20, &conf, pktbuf); | |||
run_transop_benchmark("speck", &transop_speck, &conf, pktbuf); | run_transop_benchmark("speck", &transop_speck, &conf, pktbuf); | |||
run_transop_benchmark("lzo1x", &transop_lzo, &conf, pktbuf); | ||||
#ifdef HAVE_ZSTD | ||||
run_transop_benchmark("zstd", &transop_zstd, &conf, pktbuf); | ||||
#endif | ||||
run_ecc_benchmark(); | run_ecc_benchmark(); | |||
/* Also for compression (init moved here for ciphers get run before in case of | ||||
lzo init error) */ | ||||
init_compression_for_benchmark(); | ||||
run_compression_benchmark(); | ||||
run_hashing_benchmark(); | run_hashing_benchmark(); | |||
/* Cleanup */ | /* Cleanup */ | |||
transop_null.deinit(&transop_null); | transop_null.deinit(&transop_null); | |||
transop_tf.deinit(&transop_tf); | transop_tf.deinit(&transop_tf); | |||
transop_aes.deinit(&transop_aes); | transop_aes.deinit(&transop_aes); | |||
transop_cc20.deinit(&transop_cc20); | transop_cc20.deinit(&transop_cc20); | |||
transop_speck.deinit(&transop_speck); | transop_speck.deinit(&transop_speck); | |||
transop_lzo.deinit(&transop_lzo); | ||||
deinit_compression_for_benchmark(); | #ifdef HAVE_ZSTD | |||
transop_zstd.deinit(&transop_zstd); | ||||
return 0; | ||||
} | ||||
// --- compression benchmark --------------------------------------------------- | ||||
----------- | ||||
static void init_compression_for_benchmark(void) { | ||||
if(lzo_init() != LZO_E_OK) { | ||||
traceEvent(TRACE_ERROR, "LZO compression init error"); | ||||
exit(1); | ||||
} | ||||
#ifdef N2N_HAVE_ZSTD | ||||
// zstd does not require initialization. if it were required, this would be a | ||||
good place | ||||
#endif | ||||
} | ||||
static void deinit_compression_for_benchmark(void) { | ||||
// lzo1x does not require de-initialization. if it were required, this would b | ||||
e a good place | ||||
#ifdef N2N_HAVE_ZSTD | ||||
// zstd does not require de-initialization. if it were required, this would be | ||||
a good place | ||||
#endif | ||||
} | ||||
static void run_compression_benchmark() { | ||||
const float target_sec = DURATION; | ||||
struct timeval t1; | ||||
struct timeval t2; | ||||
ssize_t target_usec = target_sec * 1e6; | ||||
ssize_t tdiff; // microseconds | ||||
size_t num_packets; | ||||
float mpps; | ||||
uint8_t compression_buffer[N2N_PKT_BUF_SIZE]; // size allows enough of a reser | ||||
ve required for compression | ||||
lzo_uint compression_len = N2N_PKT_BUF_SIZE; | ||||
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; | ||||
gettimeofday( &t1, NULL ); | ||||
while(tdiff < target_usec) { | ||||
compression_len = N2N_PKT_BUF_SIZE; | ||||
if(lzo1x_1_compress(PKT_CONTENT, sizeof(PKT_CONTENT), compression_buffer, &c | ||||
ompression_len, wrkmem) != LZO_E_OK) { | ||||
printf("\n\t compression error\n"); | ||||
exit(1); | ||||
} | ||||
num_packets++; | ||||
if (!(num_packets & PACKETS_BEFORE_GETTIME)) { | ||||
gettimeofday( &t2, NULL ); | ||||
tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); | ||||
} | ||||
} | ||||
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, mp | ||||
ps * 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) { | ||||
deflated_len = N2N_PKT_BUF_SIZE; | ||||
lzo1x_decompress (compression_buffer, compression_len, deflation_buffer, (lz | ||||
o_uint*)&deflated_len, NULL); | ||||
num_packets++; | ||||
if (!(num_packets & PACKETS_BEFORE_GETTIME)) { | ||||
gettimeofday( &t2, NULL ); | ||||
tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); | ||||
} | ||||
} | ||||
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, mp | ||||
ps * sizeof(PKT_CONTENT)); | ||||
if(memcmp(deflation_buffer, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) | ||||
printf("\n\tdecompression error\n"); | ||||
printf ("\n"); | ||||
#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); | ||||
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_CON | ||||
TENT, sizeof(PKT_CONTENT), ZSTD_COMPRESSION_LEVEL) ; | ||||
if(ZSTD_isError(compression_len)) { | ||||
printf("\n\t compression error\n"); | ||||
exit(1); | ||||
} | ||||
num_packets++; | ||||
if (!(num_packets & PACKETS_BEFORE_GETTIME)) { | ||||
gettimeofday( &t2, NULL ); | ||||
tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); | ||||
} | ||||
} | ||||
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, mp | ||||
ps * 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) { | ||||
deflated_len = N2N_PKT_BUF_SIZE; | ||||
deflated_len = (int32_t)ZSTD_decompress (deflation_buffer, deflated_len, com | ||||
pression_buffer, compression_len); | ||||
if(ZSTD_isError(deflated_len)) { | ||||
printf("\n\tdecompression error '%s'\n", | ||||
ZSTD_getErrorName(deflated_len)); | ||||
exit(1); | ||||
} | ||||
num_packets++; | ||||
if (!(num_packets & PACKETS_BEFORE_GETTIME)) { | ||||
gettimeofday( &t2, NULL ); | ||||
tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); | ||||
} | ||||
} | ||||
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, mp | ||||
ps * sizeof(PKT_CONTENT)); | ||||
if(memcmp(deflation_buffer, PKT_CONTENT, sizeof(PKT_CONTENT)) != 0) | ||||
printf("\n\tdecompression error\n"); | ||||
printf ("\n"); | ||||
#endif | #endif | |||
return 0; | ||||
} | } | |||
// --- hashing benchmark ------------------------------------------------------- ----------- | // --- hashing benchmark ------------------------------------------------------- ----------- | |||
static void run_hashing_benchmark(void) { | static void run_hashing_benchmark(void) { | |||
const float target_sec = DURATION; | const float target_sec = DURATION; | |||
struct timeval t1; | struct timeval t1; | |||
struct timeval t2; | struct timeval t2; | |||
ssize_t nw; | ssize_t nw; | |||
ssize_t target_usec = target_sec * 1e6; | ssize_t target_usec = target_sec * 1e6; | |||
skipping to change at line 329 | skipping to change at line 185 | |||
tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); | tdiff = ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec); | |||
} | } | |||
float mpps = num_packets / (tdiff / 1e6) / 1e6; | float mpps = num_packets / (tdiff / 1e6) / 1e6; | |||
printf(" ---> (%u bytes)\t%12u ops\t%8.1f Kops/s\n", | printf(" ---> (%u bytes)\t%12u ops\t%8.1f Kops/s\n", | |||
(unsigned int)nw, (unsigned int)num_packets, mpps * 1e3); | (unsigned int)nw, (unsigned int)num_packets, mpps * 1e3); | |||
printf("\n"); | printf("\n"); | |||
} | } | |||
// --- cipher benchmark -------------------------------------------------------- ----------- | // --- transop benchmark ------------------------------------------------------- ----------- | |||
static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 n_edge_conf_t *conf, uint8_t *pktbuf) { | static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 n_edge_conf_t *conf, uint8_t *pktbuf) { | |||
n2n_common_t cmn; | n2n_common_t cmn; | |||
n2n_PACKET_t pkt; | n2n_PACKET_t pkt; | |||
n2n_mac_t mac_buf; | n2n_mac_t mac_buf; | |||
uint8_t decodebuf[N2N_PKT_BUF_SIZE]; | uint8_t decodebuf[N2N_PKT_BUF_SIZE]; | |||
const float target_sec = DURATION; | const float target_sec = DURATION; | |||
struct timeval t1; | struct timeval t1; | |||
struct timeval t2; | struct timeval t2; | |||
size_t idx; | size_t idx; | |||
End of changes. 14 change blocks. | ||||
189 lines changed or deleted | 29 lines changed or added |