summaryrefslogtreecommitdiff
path: root/bench/runner.c
blob: 94c355d44d1b94641047ded826a4aedb52490091 (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
/* written in C instead of Myrddin because I don't have FP in Myrddin yet... */
#include <stdlib.h>
#include <stdio.h>

#include <unistd.h>
#include <err.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>

#define Nsamp 10

double run(char *prog)
{
    struct rusage ru;
    double sec, usec;
    char *cmd[2];
    int pid;
    int status;

    sec = 0;
    usec = 0;
    pid = fork();
    if (pid < 0) {
        err(1, "Could not fork\n");
    } else if (pid == 0) {
        cmd[0] = prog;
        cmd[1] = NULL;
        execv(prog, cmd);
        err(1, "Failed to exec\n");
    } else {
        wait4(pid, &status, 0, &ru);
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
            err(1, "Subprogram failed to execute\n");
        sec = ru.ru_utime.tv_sec;
        usec = ru.ru_utime.tv_usec / (1000.0 * 1000.0);
    }
    return sec + usec;
}

void timed_run(char *prog)
{
    double avg, m, d, x;
    int i, n;

    avg = 0;
    m = 0;
    n = 0;
    for (i = 0; i < Nsamp; i++) {
        n++;
        x = run(prog);
        d = (x - avg);
        avg += d/n;
        m = m + d*(x - avg);
    }
    printf("%s:\t%fs (σ^2: %f)\n", prog, avg, m/(n-1));
}

int main(int argc, char **argv)
{
    int i;

    printf("Running benchmarks: %d samples per binary\n", Nsamp);
    for (i = 1; i < argc; i++)
        timed_run(argv[i]);
    return 0;
}