summaryrefslogtreecommitdiffstats
path: root/tests/lib/test_heavy.c
blob: 1e56940831b07e017b10d355189636fc0b8ce80b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 */

/* This programme shows the effects of 'heavy' long-running functions
 * on the cooperative threading model.
 *
 * Run it with a config file containing 'password whatever', telnet to it
 * (it defaults to port 4000) and enter the 'clear foo string' command.
 * then type whatever and observe that the vty interface is unresponsive
 * for quite a period of time, due to the clear_something command
 * taking a very long time to complete.
 */
#include <zebra.h>

#include "frrevent.h"
#include "vty.h"
#include "command.h"
#include "memory.h"
#include <math.h>

#include "tests.h"

enum { ITERS_FIRST = 0,
       ITERS_ERR = 100,
       ITERS_LATER = 400,
       ITERS_PRINT = 10,
       ITERS_MAX = 1000,
};

static void slow_func(struct vty *vty, const char *str, const int i)
{
	double x = 1;
	int j;

	for (j = 0; j < 300; j++)
		x += sin(x) * j;

	if ((i % ITERS_LATER) == 0)
		printf("%s: %d, temporary error, save this somehow and do it later..\n",
		       __func__, i);

	if ((i % ITERS_ERR) == 0)
		printf("%s: hard error\n", __func__);

	if ((i % ITERS_PRINT) == 0)
		printf("%s did %d, x = %g\n", str, i, x);
}

static void clear_something(struct vty *vty, const char *str)
{
	int i;

	/* this could be like iterating through 150k of route_table
	 * or worse, iterating through a list of peers, to bgp_stop them with
	 * each having 150k route tables to process...
	 */
	for (i = ITERS_FIRST; i < ITERS_MAX; i++)
		slow_func(vty, str, i);
}

DEFUN (clear_foo,
       clear_foo_cmd,
       "clear foo LINE...",
       "clear command\n"
       "arbitrary string\n")
{
	char *str;
	if (!argc) {
		vty_out(vty, "%% string argument required\n");
		return CMD_WARNING;
	}

	str = argv_concat(argv, argc, 0);

	clear_something(vty, str);
	XFREE(MTYPE_TMP, str);
	return CMD_SUCCESS;
}

static void slow_vty_init(void)
{
	install_element(VIEW_NODE, &clear_foo_cmd);
}

void test_init(void)
{
	slow_vty_init();
}