1/*
2 * from ftp://ftp.netbsd.org/pub/NetBSD/misc/gmcgarry/bench/execbench.tar.gz
3 */
4#define errx(x,y...) { fprintf(stderr, y); fprintf(stderr, "\n"); exit(x); }
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <sys/time.h>
9#include <unistd.h>
10
11int
12main(int argc, char *argv[])
13{
14	struct timeval before, after;
15	unsigned long time, elapsed;
16	char timestr[12], iterstr[12];
17	char *timeptr, *countptr;
18        int iter, count;
19
20	if (argc < 2)
21		errx(1, "Usage: %s iterations", argv[0]);
22
23	iter = atoi(argv[1]);
24	if (iter > 0) {
25		gettimeofday(&before, NULL);
26		time = 1000000 * before.tv_sec + before.tv_usec;
27		sprintf(timestr,"%lu", time);
28		timeptr = timestr;
29		countptr = argv[1];
30	} else {
31		iter = atoi(argv[2]);
32		timeptr = argv[3];
33		countptr = argv[4];
34	}
35
36	if (iter != 0) {
37		iter--;
38		sprintf(iterstr, "%d", iter);
39		execl(argv[0], argv[0], "0", iterstr, timeptr, countptr, NULL);
40		errx(1, "execl failed");
41	}
42
43	gettimeofday(&after, NULL);
44	sscanf(argv[3],"%lu", &time);
45	count = atoi(argv[4]);
46	elapsed = 1000000 * after.tv_sec + after.tv_usec;
47	elapsed -= time;
48
49	printf("time: %lu microseconds\n", elapsed / count);
50
51	return (1);
52}
53