-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathstresstest.c
120 lines (111 loc) · 3.64 KB
/
stresstest.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// stresstest ttyplot
//
// License: Apache 2.0
//
// This is needed for musl libc
#if ! defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 500)
#undef _XOPEN_SOURCE // to address warnings about potential re-definition
#define _XOPEN_SOURCE 500
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
const char help[] =
"Usage:\n"
" stresstest [-2] [-c] [-g] [-n] [-r rate]\n"
" stresstest -h\n"
"\n"
" -h print this help message and exit\n"
" -2 output two waves\n"
" -c randomly chunk the output\n"
" -g occasionally output garbage\n"
" -n output negative values\n"
" -r rate sample rate in samples/s (default: 100)\n"
" -s seed set random seed\n";
const char optstring[] = "h2cgnr:s:";
int main(int argc, char *argv[]) {
char buffer[1024];
size_t buffer_pos = 0;
int opt;
bool two_waves = false;
bool chunked = false;
bool add_garbage = false;
bool output_negative = false;
double rate = 100;
unsigned int seed = time(NULL);
// Parse the command line.
while ((opt = getopt(argc, argv, optstring)) != -1) {
switch (opt) {
case 'h':
printf(help);
return EXIT_SUCCESS;
case '2':
two_waves = true;
break;
case 'c':
chunked = true;
break;
case 'g':
add_garbage = true;
break;
case 'n':
output_negative = true;
break;
case 'r':
rate = atof(optarg);
break;
case 's':
seed = atoi(optarg);
break;
default:
fprintf(stderr, help);
return EXIT_FAILURE;
}
}
if (argc > optind) {
fprintf(stderr, help);
return EXIT_FAILURE;
}
const useconds_t delay = 1e6 / rate;
srand(seed);
for (unsigned int n = 0;; n += 5) {
buffer_pos += sprintf(buffer + buffer_pos, "%.1f\n",
(sin(n * M_PI / 180) * 5) + (output_negative ? 0 : 5));
if (add_garbage && rand() <= RAND_MAX / 5)
buffer_pos += sprintf(buffer + buffer_pos, "garbage ");
if (two_waves) {
buffer_pos +=
sprintf(buffer + buffer_pos, "%.1f\n",
(cos(n * M_PI / 180) * 5) + (output_negative ? 0 : 5));
if (add_garbage && rand() <= RAND_MAX / 5)
buffer_pos += sprintf(buffer + buffer_pos, "garbage ");
}
if (chunked) {
size_t send_pos = 0;
while (buffer_pos - send_pos >= 16) {
const size_t bytes_to_send = 1 + rand() % 16; // 1..16
const ssize_t bytes_sent =
write(STDOUT_FILENO, buffer + send_pos, bytes_to_send);
usleep(50); // let ttyplot read this before proceeding
if (bytes_sent > 0)
send_pos += bytes_sent;
}
if (send_pos > 0 && send_pos < buffer_pos)
memmove(buffer, buffer + send_pos, buffer_pos - send_pos);
buffer_pos -= send_pos;
} else {
const ssize_t bytes_sent = write(STDOUT_FILENO, buffer, buffer_pos);
if ((bytes_sent > 0) && ((size_t)bytes_sent < buffer_pos))
memmove(buffer, buffer + bytes_sent, buffer_pos - bytes_sent);
if (bytes_sent > 0)
buffer_pos -= bytes_sent;
}
usleep(delay);
}
return 0;
}