[IOMultiplexerV2] alpha
[NextIRCd.git] / src / IOHandler_test / timer / iotest.c
1 /* main.c - IOMultiplexer
2  * Copyright (C) 2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include <stdio.h>
19 #include <sys/time.h>
20 #include "../../IOHandler/IOHandler.h"
21 #include "../../IOHandler/IOTimer.h"
22 #include "../../IOHandler/IOLog.h"
23
24 #define TEST_DURATION 100
25
26 static IOTIMER_CALLBACK(io_callback);
27 static IOLOG_CALLBACK(io_log);
28
29 static struct timeval test_clock1, test_clock2;
30 static int timercount;
31
32 int main(int argc, char *argv[]) {
33         iohandler_init();
34         iolog_register_callback(io_log);
35         
36         gettimeofday(&test_clock1, NULL);
37         gettimeofday(&test_clock2, NULL);
38
39         struct timeval tv;
40         tv.tv_sec = TEST_DURATION / 1000;
41         tv.tv_usec = TEST_DURATION % 1000 * 1000;
42         struct IOTimerDescriptor *timer = iotimer_create(NULL);
43         iotimer_set_callback(timer, io_callback);
44         iotimer_set_autoreload(timer, &tv);
45         iotimer_start(timer);
46         
47         timercount = 0;
48         
49         printf("[timer 0] %ld.%ld\n", test_clock1.tv_sec, test_clock1.tv_usec);
50         
51         iohandler_run();
52         return 1;
53 }
54
55 static IOTIMER_CALLBACK(io_callback) {
56         struct timeval curr_time;
57         int diff1;
58         double diff2;
59         
60         timercount++;
61         gettimeofday(&curr_time, NULL);
62         diff1 = (curr_time.tv_sec - test_clock1.tv_sec) * 1000 + ((curr_time.tv_usec - test_clock1.tv_usec) / 1000);
63         diff2 = (curr_time.tv_sec - test_clock2.tv_sec) * 1000 + ((curr_time.tv_usec - test_clock2.tv_usec) / 1000.0);
64         diff2 -= (timercount * TEST_DURATION);
65         gettimeofday(&test_clock1, NULL);
66         printf("[timer %03d] %ld.%06ld [%d ms]  accuracy: %f ms\n", timercount, curr_time.tv_sec, curr_time.tv_usec, diff1, diff2);
67 }
68
69 static IOLOG_CALLBACK(io_log) {
70         //printf("%s", line);
71 }