]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_heavy.c
Merge branch 'frr/pull/569'
[mirror_frr.git] / tests / lib / test_heavy.c
1 /*
2 * $Id: heavy.c,v 1.3 2005/04/25 16:42:24 paul Exp $
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /* This programme shows the effects of 'heavy' long-running functions
22 * on the cooperative threading model.
23 *
24 * Run it with a config file containing 'password whatever', telnet to it
25 * (it defaults to port 4000) and enter the 'clear foo string' command.
26 * then type whatever and observe that the vty interface is unresponsive
27 * for quite a period of time, due to the clear_something command
28 * taking a very long time to complete.
29 */
30 #include <zebra.h>
31
32 #include "thread.h"
33 #include "vty.h"
34 #include "command.h"
35 #include "memory.h"
36 #include <math.h>
37
38 #include "tests.h"
39
40 enum
41 {
42 ITERS_FIRST = 0,
43 ITERS_ERR = 100,
44 ITERS_LATER = 400,
45 ITERS_PRINT = 10,
46 ITERS_MAX = 1000,
47 };
48
49 static void
50 slow_func (struct vty *vty, const char *str, const int i)
51 {
52 double x = 1;
53 int j;
54
55 for (j = 0; j < 300; j++)
56 x += sin(x)*j;
57
58 if ((i % ITERS_LATER) == 0)
59 printf ("%s: %d, temporary error, save this somehow and do it later..\n",
60 __func__, i);
61
62 if ((i % ITERS_ERR) == 0)
63 printf ("%s: hard error\n", __func__);
64
65 if ((i % ITERS_PRINT) == 0)
66 printf ("%s did %d, x = %g%s", str, i, x, VTY_NEWLINE);
67 }
68
69 static void
70 clear_something (struct vty *vty, const char *str)
71 {
72 int i;
73
74 /* this could be like iterating through 150k of route_table
75 * or worse, iterating through a list of peers, to bgp_stop them with
76 * each having 150k route tables to process...
77 */
78 for (i = ITERS_FIRST; i < ITERS_MAX; i++)
79 slow_func (vty, str, i);
80 }
81
82 DEFUN (clear_foo,
83 clear_foo_cmd,
84 "clear foo LINE...",
85 "clear command\n"
86 "arbitrary string\n")
87 {
88 char *str;
89 if (!argc)
90 {
91 vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
92 return CMD_WARNING;
93 }
94
95 str = argv_concat (argv, argc, 0);
96
97 clear_something (vty, str);
98 XFREE (MTYPE_TMP, str);
99 return CMD_SUCCESS;
100 }
101
102 static void
103 slow_vty_init()
104 {
105 install_element (VIEW_NODE, &clear_foo_cmd);
106 }
107
108 void
109 test_init()
110 {
111 slow_vty_init();
112 }