7ce2d44e6acc872b4ef0137b53b0840b06741274
[NeonServV5.git] / src / 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 "../../IOHandler.h"
20
21 #define TEST_DURATION 100
22
23 static IOHANDLER_CALLBACK(io_callback);
24 static IOHANDLER_LOG_BACKEND(io_log);
25
26 static struct timeval test_clock1, test_clock2;
27 static int timercount;
28
29 static void add_timer(int ms) {
30     struct timeval timeout;
31     gettimeofday(&timeout, NULL);
32     timeout.tv_usec += (ms % 1000) * 1000;
33     timeout.tv_sec += (ms / 1000);
34     if(timeout.tv_usec > 1000000) {
35         timeout.tv_usec -= 1000000;
36         timeout.tv_sec++;
37     }
38     iohandler_timer(timeout, io_callback);
39 }
40
41 int main(int argc, char *argv[]) {
42     iolog_backend = io_log;
43     
44     iohandler_set(IOHANDLER_SETTING_HIGH_PRECISION_TIMER, 1);
45     
46     gettimeofday(&test_clock1, NULL);
47     gettimeofday(&test_clock2, NULL);
48     add_timer(TEST_DURATION);
49     timercount = 0;
50     
51     printf("[timer 0] %ld.%ld\n", test_clock1.tv_sec, test_clock1.tv_usec);
52     
53     while(1) {
54         iohandler_poll();
55     }
56 }
57
58 static IOHANDLER_CALLBACK(io_callback) {
59     struct timeval curr_time;
60     int diff1;
61     double diff2;
62     switch(event->type) {
63         case IOEVENT_TIMEOUT:
64             add_timer(TEST_DURATION);
65             timercount++;
66             gettimeofday(&curr_time, NULL);
67             diff1 = (curr_time.tv_sec - test_clock1.tv_sec) * 1000 + ((curr_time.tv_usec - test_clock1.tv_usec) / 1000);
68             diff2 = (curr_time.tv_sec - test_clock2.tv_sec) * 1000 + ((curr_time.tv_usec - test_clock2.tv_usec) / 1000.0);
69             diff2 -= (timercount * TEST_DURATION);
70             gettimeofday(&test_clock1, NULL);
71             printf("[timer %03d] %ld.%06ld [%d ms]  accuracy: %f ms\n", timercount, curr_time.tv_sec, curr_time.tv_usec, diff1, diff2);
72             break;
73         default:
74             break;
75     }
76 }
77
78 static IOHANDLER_LOG_BACKEND(io_log) {
79     //printf("%s", line);
80 }