]> git.meshlink.io Git - meshlink-tiny/blob - test/metering.c
Add a metering test.
[meshlink-tiny] / test / metering.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <assert.h>
6 #include <inttypes.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "meshlink-tiny.h"
12 #include "devtools.h"
13 #include "netns_utils.h"
14 #include "utils.h"
15
16 #include "full.h"
17
18 static void print_counters(peer_config_t *peers, const char *description) {
19         printf("%s:\n", description);
20         printf("        %9s %9s %9s %9s %9s %9s\n",
21                "in data",
22                "forward",
23                "meta",
24                "out data",
25                "forward",
26                "meta");
27
28         assert(peers[0].full);
29
30         for(int i = 0; i < 3; i++) {
31                 meshlink_node_t *node = full_meshlink_get_node(peers[0].mesh, peers[i].name);
32                 assert(node);
33                 struct devtool_node_status status;
34                 full_devtool_reset_node_counters(peers[0].mesh, node, &status);
35                 printf(" %5s: %9" PRIu64 " %9" PRIu64 " %9" PRIu64 " %9" PRIu64 " %9" PRIu64 " %9" PRIu64 "\n",
36                        node->name,
37                        status.in_data,
38                        status.in_forward,
39                        status.in_meta,
40                        status.out_data,
41                        status.out_forward,
42                        status.out_meta);
43         }
44 }
45
46
47 int main(void) {
48         init_full();
49
50         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
51
52         // Set up relay, peer and NUT
53         peer_config_t *peers = setup_relay_peer_nut("metering");
54
55         // Ensure DEV_CLASS_STATIONARY uses a 5 minute ping time
56         for(int i = 0; i < 3; i++) {
57                 peers[i].full
58                 ? full_meshlink_set_dev_class_timeouts(peers[i].mesh, DEV_CLASS_STATIONARY, 300, 5)
59                 : meshlink_set_dev_class_timeouts(peers[i].mesh, DEV_CLASS_BACKBONE, 300, 5);
60         }
61
62         for(int i = 0; i < 3; i++) {
63                 assert(peers[i].full
64                        ? full_meshlink_start(peers[i].mesh)
65                        : meshlink_start(peers[i].mesh)
66                       );
67         }
68
69         // Measure traffic after 1 minute of PMTU probing
70         sleep(60);
71         print_counters(peers, "PMTU probing (1 min)");
72
73         // Measure traffic after 1 minute of idle
74         for(int i = 0; i < 10; i++) {
75                 sleep(60);
76                 print_counters(peers, "Idle (1 min)");
77         }
78
79         close_relay_peer_nut(peers);
80 }