]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_heavy.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / lib / test_heavy.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 */
4
5 /* This programme shows the effects of 'heavy' long-running functions
6 * on the cooperative threading model.
7 *
8 * Run it with a config file containing 'password whatever', telnet to it
9 * (it defaults to port 4000) and enter the 'clear foo string' command.
10 * then type whatever and observe that the vty interface is unresponsive
11 * for quite a period of time, due to the clear_something command
12 * taking a very long time to complete.
13 */
14 #include <zebra.h>
15
16 #include "thread.h"
17 #include "vty.h"
18 #include "command.h"
19 #include "memory.h"
20 #include <math.h>
21
22 #include "tests.h"
23
24 enum { ITERS_FIRST = 0,
25 ITERS_ERR = 100,
26 ITERS_LATER = 400,
27 ITERS_PRINT = 10,
28 ITERS_MAX = 1000,
29 };
30
31 static void slow_func(struct vty *vty, const char *str, const int i)
32 {
33 double x = 1;
34 int j;
35
36 for (j = 0; j < 300; j++)
37 x += sin(x) * j;
38
39 if ((i % ITERS_LATER) == 0)
40 printf("%s: %d, temporary error, save this somehow and do it later..\n",
41 __func__, i);
42
43 if ((i % ITERS_ERR) == 0)
44 printf("%s: hard error\n", __func__);
45
46 if ((i % ITERS_PRINT) == 0)
47 printf("%s did %d, x = %g\n", str, i, x);
48 }
49
50 static void clear_something(struct vty *vty, const char *str)
51 {
52 int i;
53
54 /* this could be like iterating through 150k of route_table
55 * or worse, iterating through a list of peers, to bgp_stop them with
56 * each having 150k route tables to process...
57 */
58 for (i = ITERS_FIRST; i < ITERS_MAX; i++)
59 slow_func(vty, str, i);
60 }
61
62 DEFUN (clear_foo,
63 clear_foo_cmd,
64 "clear foo LINE...",
65 "clear command\n"
66 "arbitrary string\n")
67 {
68 char *str;
69 if (!argc) {
70 vty_out(vty, "%% string argument required\n");
71 return CMD_WARNING;
72 }
73
74 str = argv_concat(argv, argc, 0);
75
76 clear_something(vty, str);
77 XFREE(MTYPE_TMP, str);
78 return CMD_SUCCESS;
79 }
80
81 static void slow_vty_init(void)
82 {
83 install_element(VIEW_NODE, &clear_foo_cmd);
84 }
85
86 void test_init(void)
87 {
88 slow_vty_init();
89 }