1 /*
2  * Copyright (C) 2021 Jakub Kruszona-Zawadzki, Core Technology Sp. z o.o.
3  *
4  * This file is part of MooseFS.
5  *
6  * MooseFS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, version 2 (only).
9  *
10  * MooseFS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with MooseFS; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
18  * or visit http://www.gnu.org/licenses/gpl-2.0.html
19  */
20 
21 #include <sys/types.h>
22 #include <sys/select.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include "clocks.h"
29 #include "portable.h"
30 
31 #include "mfstest.h"
32 
wallclock_utime(void)33 uint64_t wallclock_utime(void) {
34 	struct timeval tv;
35 	uint64_t usec;
36 
37 	gettimeofday(&tv,NULL);
38 	usec = tv.tv_sec;
39 	usec *= 1000000;
40 	usec += tv.tv_usec;
41 	return usec;
42 }
43 
main(void)44 int main(void) {
45 	double st,en,secsum;
46 	uint64_t stusec,enusec,usecsum;
47 	uint64_t stnsec,ennsec,nsecsum;
48 	uint64_t wcstusec,wcenusec,wcusecsum;
49 	int i;
50 
51 	if (strcmp(monotonic_method(),"time")==0) {
52 		printf("testing classic 'time(NULL)' clock doesn't make sense\n");
53 		return 77;
54 	}
55 
56 	mfstest_init();
57 
58 	mfstest_start(monotonic_clocks);
59 
60 	printf("used method: %s\n",monotonic_method());
61 	secsum = 0.0;
62 	usecsum = 0;
63 	nsecsum = 0;
64 	wcusecsum = 0;
65 	for (i=0 ; i<10 ; i++) {
66 		st = monotonic_seconds();
67 		stusec = monotonic_useconds();
68 		stnsec = monotonic_nseconds();
69 		wcstusec = wallclock_utime();
70 		portable_usleep(10000);
71 		en = monotonic_seconds();
72 		enusec = monotonic_useconds();
73 		ennsec = monotonic_nseconds();
74 		wcenusec = wallclock_utime();
75 		secsum += en-st;
76 		usecsum += enusec-stusec;
77 		nsecsum += ennsec-stnsec;
78 		wcusecsum += wcenusec-wcstusec;
79 	}
80 	printf("second: %.6lf ; %"PRIu64" ; %"PRIu64" ; %"PRIu64"\n",secsum,usecsum,nsecsum,wcusecsum);
81 
82 	mfstest_assert_double_ge(secsum,0.1);
83 	mfstest_assert_uint64_ge(usecsum,100000);
84 	mfstest_assert_uint64_ge(nsecsum,100000000);
85 	mfstest_assert_uint64_ge(wcusecsum,100000);
86 	mfstest_assert_double_lt(secsum,0.25);
87 	mfstest_assert_uint64_lt(usecsum,250000);
88 	mfstest_assert_uint64_lt(nsecsum,250000000);
89 	mfstest_assert_uint64_lt(wcusecsum,250000);
90 
91 	mfstest_end();
92 	mfstest_return();
93 }
94