add a test for -limit functionality
[rofl0r-jobflow.git] / tests / cpuwaster.c
blobb3e04a84020f206104a27796f5513ff61ca8f140
1 #undef _DEFAULT_SOURCE
2 #define _DEFAULT_SOURCE /* for timersub */
3 #include <sys/time.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
8 static void gettimestamp(struct timeval* t) {
9 gettimeofday(t, NULL);
12 static long mspassed(struct timeval* t) {
13 struct timeval now, diff;
14 gettimeofday(&now, NULL);
15 timersub(&now, t, &diff);
16 return (diff.tv_sec * 1000) + (diff.tv_usec / 1000);
19 static int syntax() {
20 puts("prog seconds_of_cpu_time_to_burn");
21 return 1;
24 long parse_human_number(char* num) {
25 long ret = 0;
26 char buf[64];
27 if(!num) return 0;
28 size_t l = strlen(num);
30 if(l && l < sizeof(buf)) {
31 if(num[l -1] == 'G')
32 ret = 1024 * 1024 * 1024;
33 else if(num[l -1] == 'M')
34 ret = 1024 * 1024;
35 else if(num[l -1] == 'K')
36 ret = 1024;
37 if(ret) {
38 memcpy(buf, num, l);
39 buf[l] = 0;
40 return atol(buf) * ret;
42 return atol(num);
44 return ret;
47 int main(int argc, char** argv) {
48 void* buf;
49 long waste_secs;
50 struct timeval start;
51 if (argc != 2) return syntax();
52 waste_secs = parse_human_number(argv[1]);
53 gettimestamp(&start);
55 while(mspassed(&start) < waste_secs * 1000) {}
57 printf("successfully wasted %ld seconds of your cpu time\n", waste_secs);
58 return 0;