summaryrefslogtreecommitdiff
path: root/src/benchmark.c
blob: 134c2a13cb202705729123e11926eaaccb071614 (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "benchmark.h"

int benchmark_must_finish = 0;

#if defined(_WIN32)
#include <windows.h>
DWORD WINAPI
alarm_handler (LPVOID lpParameter)
{
  HANDLE wtimer = *((HANDLE *) lpParameter);
  WaitForSingleObject (wtimer, INFINITE);
  benchmark_must_finish = 1;
  return 0;
}
#else
static void
alarm_handler (int signo)
{
  benchmark_must_finish = 1;
}
#endif

static void
value2human (unsigned long bytes, double time, double *data, double *speed,
             char *metric)
{
  if (bytes > 1000 && bytes < 1000 * 1000)
    {
      *data = ((double) bytes) / 1000;
      *speed = *data / time;
      strcpy (metric, "KB");
      return;
    }
  else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000)
    {
      *data = ((double) bytes) / (1000 * 1000);
      *speed = *data / time;
      strcpy (metric, "MB");
      return;
    }
  else if (bytes >= 1000 * 1000 * 1000)
    {
      *data = ((double) bytes) / (1000 * 1000 * 1000);
      *speed = *data / time;
      strcpy (metric, "GB");
      return;
    }
  else
    {
      *data = (double) bytes;
      *speed = *data / time;
      strcpy (metric, "bytes");
      return;
    }
}

void start_benchmark(struct benchmark_st * st)
{
  memset(st, 0, sizeof(*st));
  st->old_handler = signal (SIGALRM, alarm_handler);
  gettime (&st->start);
  benchmark_must_finish = 0;

#if defined(_WIN32)
  st->wtimer = CreateWaitableTimer (NULL, TRUE, NULL);
  if (st->wtimer == NULL)
    {
      fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ());
      exit(1);
    }
  st->wthread = CreateThread (NULL, 0, alarm_handler, &st->wtimer, 0, NULL);
  if (st->wthread == NULL)
    {
      fprintf (stderr, "error: CreateThread %u\n", GetLastError ());
      exit(1);
    }
  alarm_timeout.QuadPart = (5) * 10000000;
  if (SetWaitableTimer (st->wtimer, &alarm_timeout, 0, NULL, NULL, FALSE) == 0)
    {
      fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ());
      exit(1);
    }
  }
#else
  alarm (5);
#endif
  
}

/* returns the elapsed time */
double stop_benchmark(struct benchmark_st * st, const char* metric)
{
  double secs;
  struct timespec stop;
  double dspeed, ddata;
  char imetric[16];

#if defined(_WIN32)
  if (st->wtimer != NULL)
    CloseHandle (st->wtimer);
  if (st->wthread != NULL)
    CloseHandle (st->wthread);
#else
  signal(SIGALRM, st->old_handler);
#endif

  gettime (&stop);

  secs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) -
          (st->start.tv_sec * 1000 + st->start.tv_nsec / (1000 * 1000)));
  secs /= 1000;

  if (metric == NULL)
    { /* assume bytes/sec */
      value2human (st->size, secs, &ddata, &dspeed, imetric);
      printf ("Processed %.2f %s in %.2f secs: ", ddata, imetric, secs);
      printf ("%.2f %s/sec\n", dspeed, imetric);
    }
  else
    {
      ddata = (double) st->size;
      dspeed = ddata / secs;
      printf ("Processed %.2f %s in %.2f secs: ", ddata, metric, secs);
      printf ("%.2f %s/sec\n", dspeed, metric);
    }

  return secs;
}