|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2011-2012 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GnuTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is free software: you can redistribute it and/or modify
|
|
Packit Service |
4684c1 |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
4684c1 |
* the Free Software Foundation, either version 3 of the License, or
|
|
Packit Service |
4684c1 |
* (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is distributed in the hope that it will be useful,
|
|
Packit Service |
4684c1 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
4684c1 |
* GNU General Public License for more details.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
4684c1 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
#include <signal.h>
|
|
Packit Service |
4684c1 |
#include <sys/time.h>
|
|
Packit Service |
4684c1 |
#include <time.h>
|
|
Packit Service |
4684c1 |
#include <unistd.h>
|
|
Packit Service |
4684c1 |
#include "benchmark.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define BSECS 5
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
volatile int benchmark_must_finish = 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#if defined(_WIN32)
|
|
Packit Service |
4684c1 |
#include <windows.h>
|
|
Packit Service |
4684c1 |
static DWORD WINAPI alarm_handler(LPVOID lpParameter)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
HANDLE wtimer = *((HANDLE *) lpParameter);
|
|
Packit Service |
4684c1 |
WaitForSingleObject(wtimer, INFINITE);
|
|
Packit Service |
4684c1 |
benchmark_must_finish = 1;
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
#else
|
|
Packit Service |
4684c1 |
static void alarm_handler(int signo)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
benchmark_must_finish = 1;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void
|
|
Packit Service |
4684c1 |
value2human(uint64_t bytes, double time, double *data, double *speed,
|
|
Packit Service |
4684c1 |
char *metric)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
if (bytes > 1000 && bytes < 1000 * 1000) {
|
|
Packit Service |
4684c1 |
*data = ((double) bytes) / 1000;
|
|
Packit Service |
4684c1 |
*speed = *data / time;
|
|
Packit Service |
4684c1 |
strcpy(metric, "KB");
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
} else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000) {
|
|
Packit Service |
4684c1 |
*data = ((double) bytes) / (1000 * 1000);
|
|
Packit Service |
4684c1 |
*speed = *data / time;
|
|
Packit Service |
4684c1 |
strcpy(metric, "MB");
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
} else if (bytes >= 1000 * 1000 * 1000) {
|
|
Packit Service |
4684c1 |
*data = ((double) bytes) / (1000 * 1000 * 1000);
|
|
Packit Service |
4684c1 |
*speed = *data / time;
|
|
Packit Service |
4684c1 |
strcpy(metric, "GB");
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
*data = (double) bytes;
|
|
Packit Service |
4684c1 |
*speed = *data / time;
|
|
Packit Service |
4684c1 |
strcpy(metric, "bytes");
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void start_benchmark(struct benchmark_st *st)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
memset(st, 0, sizeof(*st));
|
|
Packit Service |
4684c1 |
#ifndef _WIN32
|
|
Packit Service |
4684c1 |
st->old_handler = signal(SIGALRM, alarm_handler);
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
gettime(&st->start);
|
|
Packit Service |
4684c1 |
benchmark_must_finish = 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#if defined(_WIN32)
|
|
Packit Service |
4684c1 |
st->wtimer = CreateWaitableTimer(NULL, TRUE, NULL);
|
|
Packit Service |
4684c1 |
if (st->wtimer == NULL) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, "error: CreateWaitableTimer %u\n",
|
|
Packit Service |
4684c1 |
GetLastError());
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
st->wthread =
|
|
Packit Service |
4684c1 |
CreateThread(NULL, 0, alarm_handler, &st->wtimer, 0, NULL);
|
|
Packit Service |
4684c1 |
if (st->wthread == NULL) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, "error: CreateThread %u\n",
|
|
Packit Service |
4684c1 |
GetLastError());
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
st->alarm_timeout.QuadPart = (BSECS) * 10000000;
|
|
Packit Service |
4684c1 |
if (SetWaitableTimer
|
|
Packit Service |
4684c1 |
(st->wtimer, &st->alarm_timeout, 0, NULL, NULL, FALSE) == 0) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, "error: SetWaitableTimer %u\n",
|
|
Packit Service |
4684c1 |
GetLastError());
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
#else
|
|
Packit Service |
4684c1 |
alarm(BSECS);
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* returns the elapsed time */
|
|
Packit Service |
4684c1 |
double stop_benchmark(struct benchmark_st *st, const char *metric,
|
|
Packit Service |
4684c1 |
int quiet)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
double secs;
|
|
Packit Service |
4684c1 |
unsigned long lsecs;
|
|
Packit Service |
4684c1 |
struct timespec stop;
|
|
Packit Service |
4684c1 |
double dspeed, ddata;
|
|
Packit Service |
4684c1 |
char imetric[16];
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#if defined(_WIN32)
|
|
Packit Service |
4684c1 |
if (st->wtimer != NULL)
|
|
Packit Service |
4684c1 |
CloseHandle(st->wtimer);
|
|
Packit Service |
4684c1 |
if (st->wthread != NULL)
|
|
Packit Service |
4684c1 |
CloseHandle(st->wthread);
|
|
Packit Service |
4684c1 |
#else
|
|
Packit Service |
4684c1 |
signal(SIGALRM, st->old_handler);
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gettime(&stop);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
lsecs = timespec_sub_ms(&stop, &st->start);
|
|
Packit Service |
4684c1 |
secs = lsecs;
|
|
Packit Service |
4684c1 |
secs /= 1000;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (metric == NULL) { /* assume bytes/sec */
|
|
Packit Service |
4684c1 |
value2human(st->size, secs, &ddata, &dspeed, imetric);
|
|
Packit Service |
4684c1 |
if (quiet == 0)
|
|
Packit Service |
4684c1 |
printf(" Processed %.2f %s in %.2f secs: ", ddata,
|
|
Packit Service |
4684c1 |
imetric, secs);
|
|
Packit Service |
4684c1 |
printf("%.2f %s/sec\n", dspeed, imetric);
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
ddata = (double) st->size;
|
|
Packit Service |
4684c1 |
dspeed = ddata / secs;
|
|
Packit Service |
4684c1 |
if (quiet == 0)
|
|
Packit Service |
4684c1 |
printf(" Processed %.2f %s in %.2f secs: ", ddata,
|
|
Packit Service |
4684c1 |
metric, secs);
|
|
Packit Service |
4684c1 |
printf("%.2f %s/sec\n", dspeed, metric);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return secs;
|
|
Packit Service |
4684c1 |
}
|