Commit 7c34a502 authored by Jan Včelák's avatar Jan Včelák 🚀

contrib: clean interface for measuring time

parent 999bff45
......@@ -55,6 +55,7 @@ src/contrib/sockaddr.h
src/contrib/string.c
src/contrib/string.h
src/contrib/strtonum.h
src/contrib/time.c
src/contrib/time.h
src/contrib/tolower.h
src/contrib/trim.h
......@@ -547,6 +548,7 @@ tests/contrib/test_net_shortwrite.c
tests/contrib/test_sockaddr.c
tests/contrib/test_string.c
tests/contrib/test_strtonum.c
tests/contrib/test_time.c
tests/contrib/test_wire.c
tests/contrib/test_wire_ctx.c
tests/dthreads.c
......
......@@ -63,6 +63,7 @@ libcontrib_la_SOURCES = \
contrib/string.c \
contrib/string.h \
contrib/strtonum.h \
contrib/time.c \
contrib/time.h \
contrib/tolower.h \
contrib/trim.h \
......
......@@ -59,9 +59,3 @@ void txt_print(const uint8_t *data, unsigned length)
{
array_printf(data, length, &printf, 't');
}
float time_diff(const struct timeval *begin, const struct timeval *end)
{
return (end->tv_sec - begin->tv_sec) * 1000 +
(end->tv_usec - begin->tv_usec) / 1000.0;
}
......@@ -24,7 +24,6 @@
#pragma once
#include <sys/time.h>
#include <stdint.h>
/*!
......@@ -53,14 +52,4 @@ void short_hex_print(const uint8_t *data, unsigned length);
*/
void txt_print(const uint8_t *data, unsigned length);
/*!
* \brief Get time difference in miliseconds.
*
* \param begin
* \param end
*
* \return time diff
*/
float time_diff(const struct timeval *begin, const struct timeval *end);
/*! @} */
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "contrib/time.h"
struct timespec time_now(void)
{
struct timespec result = { 0 };
clock_gettime(CLOCK_MONOTONIC, &result);
return result;
}
struct timespec time_diff(const struct timespec *begin, const struct timespec *end)
{
struct timespec result = { 0 };
if (end->tv_nsec > begin->tv_nsec) {
result.tv_sec = end->tv_sec - begin->tv_sec;
result.tv_nsec = end->tv_nsec - begin->tv_nsec;
} else {
result.tv_sec = end->tv_sec - begin->tv_sec - 1;
result.tv_nsec = 1000000000 - begin->tv_nsec + end->tv_nsec;
}
return result;
}
float time_diff_ms(const struct timespec *begin, const struct timespec *end)
{
struct timespec result = time_diff(begin, end);
return (result.tv_sec * 1000.0) + (result.tv_nsec / 1e6);
}
......@@ -24,16 +24,21 @@
#pragma once
#ifdef HAVE_CLOCK_GETTIME
#include <time.h>
#define time_now(x) clock_gettime(CLOCK_MONOTONIC, (x))
typedef struct timespec timev_t;
#elif HAVE_GETTIMEOFDAY
#include <sys/time.h>
#define time_now(x) gettimeofday((x), NULL)
typedef struct timeval timev_t;
#else
#error Neither clock_gettime() nor gettimeofday() found. At least one is required.
#endif
/*!
* \brief Get current time.
*/
struct timespec time_now(void);
/*!
* \brief Get time elapsed between two events.
*/
struct timespec time_diff(const struct timespec *begin, const struct timespec *end);
/*!
* \brief Get time elapsed between two events in miliseconds.
*/
float time_diff_ms(const struct timespec *begin, const struct timespec *end);
/*! @} */
......@@ -13,6 +13,7 @@
/contrib/test_sockaddr
/contrib/test_string
/contrib/test_strtonum
/contrib/test_time
/contrib/test_wire
/contrib/test_wire_ctx
......
......@@ -22,6 +22,7 @@ check_PROGRAMS = \
contrib/test_sockaddr \
contrib/test_string \
contrib/test_strtonum \
contrib/test_time \
contrib/test_wire \
contrib/test_wire_ctx
......
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <tap/basic.h>
#include "contrib/time.h"
static void test_now(void)
{
struct timespec t = time_now();
ok(t.tv_sec != 0, "time_now() returns something");
}
static void test_diff(void)
{
struct timespec t1 = { 10, 1000 };
struct timespec t2 = { 50, 1500 };
struct timespec t3 = { 70, 500 };
struct timespec res = { 0 };
res = time_diff(&t1, &t2);
ok(res.tv_sec = 40 && res.tv_nsec == 500, "time_diff()");
res = time_diff(&t2, &t3);
ok(res.tv_sec = 19 && res.tv_nsec == 999999000, "time_diff() ns overflow");
res = time_diff(&t3, &t1);
ok(res.tv_sec = -60 && res.tv_nsec == 500, "time_diff() negative");
res = time_diff(&t2, &t1);
ok(res.tv_sec = -41 && res.tv_nsec == 999999500, "time_diff() negative");
}
static void test_diff_ms(void)
{
struct timespec t1 = { 10, 1000 };
struct timespec t2 = { 50, 500 };
float ms = 0.0;
ms = time_diff_ms(&t1, &t2);
ok(39990.0 < ms && ms < 40010.0, "time_diff_ms()");
ms = time_diff_ms(&t2, &t1);
ok(-40010.0 < ms && ms < -39990.0, "time_diff_ms() negative");
}
int main(int argc, char *argv[])
{
plan_lazy();
test_now();
test_diff();
test_diff_ms();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment