2015-08-23 22:33:32 -06:00
|
|
|
/*
|
|
|
|
* Copyright © 2015 Kazunobu Kuriyama <kazunobu.kuriyama@nifty.com>
|
|
|
|
* Ran Benita <ran234@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-03-15 12:42:21 -06:00
|
|
|
#include <assert.h>
|
2015-08-23 22:33:32 -06:00
|
|
|
#include <stdio.h>
|
2018-07-05 09:13:14 -06:00
|
|
|
#include <sys/time.h>
|
2015-08-23 22:33:32 -06:00
|
|
|
|
|
|
|
#include "bench.h"
|
|
|
|
|
|
|
|
void
|
2018-07-05 09:13:14 -06:00
|
|
|
bench_start(struct bench *bench)
|
2015-08-23 22:33:32 -06:00
|
|
|
{
|
|
|
|
struct timeval val;
|
2016-03-15 12:42:21 -06:00
|
|
|
(void) gettimeofday(&val, NULL);
|
2018-07-05 09:13:14 -06:00
|
|
|
bench->start = (struct bench_time) {
|
|
|
|
.seconds = val.tv_sec,
|
|
|
|
.microseconds = val.tv_usec,
|
|
|
|
};
|
2015-08-23 22:33:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-05 09:13:14 -06:00
|
|
|
bench_stop(struct bench *bench)
|
2015-08-23 22:33:32 -06:00
|
|
|
{
|
|
|
|
struct timeval val;
|
2016-03-15 12:42:21 -06:00
|
|
|
(void) gettimeofday(&val, NULL);
|
2018-07-05 09:13:14 -06:00
|
|
|
bench->stop = (struct bench_time) {
|
|
|
|
.seconds = val.tv_sec,
|
|
|
|
.microseconds = val.tv_usec,
|
|
|
|
};
|
2015-08-23 22:33:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-05 09:13:14 -06:00
|
|
|
bench_elapsed(const struct bench *bench, struct bench_time *result)
|
2015-08-23 22:33:32 -06:00
|
|
|
{
|
2018-07-05 09:13:14 -06:00
|
|
|
result->seconds = bench->stop.seconds - bench->start.seconds;
|
|
|
|
result->microseconds = bench->stop.microseconds - bench->start.microseconds;
|
|
|
|
if (result->microseconds < 0) {
|
|
|
|
result->microseconds += 1000000;
|
|
|
|
result->seconds--;
|
|
|
|
}
|
2015-08-23 22:33:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2018-07-05 09:13:14 -06:00
|
|
|
bench_elapsed_str(const struct bench *bench)
|
2015-08-23 22:33:32 -06:00
|
|
|
{
|
|
|
|
struct bench_time elapsed;
|
|
|
|
char *buf;
|
2016-03-15 12:42:21 -06:00
|
|
|
int ret;
|
2015-08-23 22:33:32 -06:00
|
|
|
|
2018-07-05 09:13:14 -06:00
|
|
|
bench_elapsed(bench, &elapsed);
|
|
|
|
ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.microseconds);
|
2016-03-15 12:42:21 -06:00
|
|
|
assert(ret >= 0);
|
2015-08-23 22:33:32 -06:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|