Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 1 | /* Test routines for the CoDel implementation |
| 2 | * |
| 3 | * (C) 2015 by sysmocom s.f.m.c. GmbH |
| 4 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
| 5 | */ |
| 6 | |
| 7 | #undef _GNU_SOURCE |
| 8 | #define _GNU_SOURCE |
| 9 | |
| 10 | #if 0 |
| 11 | #include <osmocom/core/talloc.h> |
| 12 | #include <osmocom/core/prim.h> |
| 13 | #endif |
| 14 | #include <osmocom/core/application.h> |
| 15 | #include <osmocom/core/utils.h> |
| 16 | #include <osmocom/core/logging.h> |
| 17 | |
| 18 | #include "gprs_codel.h" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/time.h> |
| 23 | |
| 24 | static int do_codel_control(struct gprs_codel *state, const struct timeval *recv, |
| 25 | struct timeval *now, const struct timeval *delta_now, int count) |
| 26 | { |
| 27 | int drop; |
| 28 | |
| 29 | drop = gprs_codel_control(state, recv, now, -1); |
| 30 | if (drop) { |
| 31 | printf("Dropping packet %d, " |
| 32 | "recv = %d.%03d, now = %d.%03d, " |
| 33 | "codel.count = %d\n", |
| 34 | count, |
| 35 | (int)recv->tv_sec, (int)recv->tv_usec/1000, |
| 36 | (int)now->tv_sec, (int)now->tv_usec/1000, |
| 37 | state->count); |
| 38 | } else { |
| 39 | timeradd(now, delta_now, now); |
| 40 | } |
| 41 | |
| 42 | return drop == 0 ? 0 : 1; |
| 43 | } |
| 44 | |
| 45 | static void test_codel(void) |
| 46 | { |
| 47 | struct gprs_codel codel; |
| 48 | struct timeval now; |
| 49 | struct timeval recv; |
| 50 | const struct timeval delta_now = {0, 10000}; |
| 51 | const struct timeval init_delta_recv = {0, 5000}; |
| 52 | struct timeval delta_recv; |
| 53 | unsigned count; |
| 54 | unsigned sum = 0; |
| 55 | unsigned dropped = 0; |
| 56 | int drop; |
| 57 | |
| 58 | printf("----- %s START\n", __func__); |
| 59 | gprs_codel_init(&codel); |
| 60 | gprs_codel_set_interval(&codel, 100); |
| 61 | |
| 62 | timerclear(&now); |
| 63 | timerclear(&recv); |
| 64 | delta_recv = init_delta_recv; |
| 65 | |
| 66 | for (count = 0; count < 20; count++, sum++) { |
| 67 | drop = do_codel_control(&codel, &recv, &now, &delta_now, sum); |
| 68 | timeradd(&recv, &delta_recv, &recv); |
| 69 | dropped += drop; |
| 70 | } |
| 71 | |
| 72 | printf("Dropped %d packets\n", dropped); |
| 73 | OSMO_ASSERT(dropped == 0); |
| 74 | OSMO_ASSERT(!codel.dropping); |
| 75 | |
| 76 | for (count = 0; count < 20; count++, sum++) { |
| 77 | drop = do_codel_control(&codel, &recv, &now, &delta_now, sum); |
| 78 | timeradd(&recv, &delta_recv, &recv); |
| 79 | dropped += drop; |
| 80 | } |
| 81 | |
| 82 | OSMO_ASSERT(dropped == 2); |
| 83 | OSMO_ASSERT(codel.dropping); |
| 84 | |
| 85 | /* slow down recv rate */ |
| 86 | delta_recv.tv_usec = delta_now.tv_usec; |
| 87 | |
| 88 | for (count = 0; count < 75; count++, sum++) { |
| 89 | drop = do_codel_control(&codel, &recv, &now, &delta_now, sum); |
| 90 | timeradd(&recv, &delta_recv, &recv); |
| 91 | dropped += drop; |
| 92 | } |
| 93 | |
| 94 | OSMO_ASSERT(dropped == 20); |
| 95 | OSMO_ASSERT(codel.dropping); |
| 96 | |
| 97 | for (count = 0; count < 50; count++, sum++) { |
| 98 | drop = do_codel_control(&codel, &recv, &now, &delta_now, sum); |
| 99 | timeradd(&recv, &delta_recv, &recv); |
| 100 | dropped += drop; |
| 101 | } |
| 102 | |
| 103 | OSMO_ASSERT(dropped == 20); |
| 104 | OSMO_ASSERT(!codel.dropping); |
| 105 | OSMO_ASSERT(codel.count >= 20); |
| 106 | |
| 107 | /* go back to old data rate */ |
| 108 | delta_recv = init_delta_recv; |
| 109 | |
| 110 | for (count = 0; count < 20; count++, sum++) { |
| 111 | drop = do_codel_control(&codel, &recv, &now, &delta_now, sum); |
| 112 | timeradd(&recv, &delta_recv, &recv); |
| 113 | dropped += drop; |
| 114 | } |
| 115 | |
| 116 | OSMO_ASSERT(dropped == 20); |
| 117 | OSMO_ASSERT(!codel.dropping); |
| 118 | |
| 119 | for (count = 0; count < 20; count++, sum++) { |
| 120 | drop = do_codel_control(&codel, &recv, &now, &delta_now, sum); |
| 121 | timeradd(&recv, &delta_recv, &recv); |
| 122 | dropped += drop; |
| 123 | } |
| 124 | |
| 125 | OSMO_ASSERT(dropped == 22); |
| 126 | OSMO_ASSERT(codel.count >= 2); |
| 127 | |
| 128 | printf("Dropped %d packets\n", dropped); |
| 129 | |
| 130 | printf("----- %s END\n", __func__); |
| 131 | } |
| 132 | |
| 133 | static struct log_info info = {}; |
| 134 | |
| 135 | int main(int argc, char **argv) |
| 136 | { |
Neels Hofmeyr | 42f2d61 | 2018-04-01 16:54:40 +0200 | [diff] [blame] | 137 | osmo_init_logging2(NULL, &info); |
Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 138 | log_set_use_color(osmo_stderr_target, 0); |
| 139 | log_set_print_filename(osmo_stderr_target, 0); |
| 140 | log_set_log_level(osmo_stderr_target, LOGL_INFO); |
| 141 | |
| 142 | printf("===== CoDel test START\n"); |
| 143 | test_codel(); |
| 144 | printf("===== CoDel test END\n\n"); |
| 145 | |
| 146 | exit(EXIT_SUCCESS); |
| 147 | } |