[IOMultiplexer] added timer test program
authorpk910 <philipp@zoelle1.de>
Sat, 17 Nov 2012 02:47:52 +0000 (03:47 +0100)
committerpk910 <philipp@zoelle1.de>
Sat, 17 Nov 2012 02:47:52 +0000 (03:47 +0100)
src/test/Makefile [deleted file]
src/test/iotest.c [deleted file]
src/test/socket/Makefile [new file with mode: 0644]
src/test/socket/iotest.c [new file with mode: 0644]
src/test/timer/Makefile [new file with mode: 0644]
src/test/timer/iotest.c [new file with mode: 0644]

diff --git a/src/test/Makefile b/src/test/Makefile
deleted file mode 100644 (file)
index 37fa3d8..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-
-CC      = gcc
-CFLAGS  = -g -O0 -Wall -Wshadow -Werror -DHAVE_PTHREAD_H
-LDFLAGS = -lws2_32 -lpthread
-
-OBJ     = ../IOEngine_epoll.o ../IOEngine_kevent.o ../IOEngine_select.o ../IOEngine_win32.o ../IOHandler.o ../IOHandler_SSL.o iotest.o
-
-all: $(OBJ)
-       $(CC) $(CFLAGS) -oiotest $(OBJ) $(LDFLAGS)
-
-%.o: %.c
-       $(CC) $(CFLAGS) -o$@ -c $<
diff --git a/src/test/iotest.c b/src/test/iotest.c
deleted file mode 100644 (file)
index 15f32f4..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/* main.c - IOMultiplexer
- * Copyright (C) 2012  Philipp Kreil (pk910)
- * 
- * 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 <stdio.h>
-#include "../IOHandler.h"
-
-static IOHANDLER_CALLBACK(io_callback);
-static IOHANDLER_LOG_BACKEND(io_log);
-
-static struct IODescriptor *irc_iofd = NULL;
-
-int main(int argc, char *argv[]) {
-    iolog_backend = io_log;
-    
-    irc_iofd = iohandler_connect("pk910.de", 6667, 0, NULL, io_callback);
-    
-    struct IODescriptor *iofd;
-    iofd = iohandler_add(0, IOTYPE_STDIN, NULL, io_callback);
-    iofd->read_lines = 1;
-    
-    while(1) {
-        iohandler_poll();
-    }
-}
-
-static IOHANDLER_CALLBACK(io_callback) {
-    switch(event->type) {
-        case IOEVENT_CONNECTED:
-            printf("[connect]\n");
-            break;
-        case IOEVENT_CLOSED:
-            printf("[disconnect]\n");
-            break;
-        case IOEVENT_RECV:
-            if(event->iofd->type == IOTYPE_STDIN) {
-                iohandler_printf(irc_iofd, "%s\n", event->data.recv_str);
-                printf("[out] %s\n", event->data.recv_str);
-            } else
-                printf("[in] %s\n", event->data.recv_str);
-            break;
-        
-        default:
-            break;
-    }
-}
-
-static IOHANDLER_LOG_BACKEND(io_log) {
-    //printf("%s", line);
-}
diff --git a/src/test/socket/Makefile b/src/test/socket/Makefile
new file mode 100644 (file)
index 0000000..724e34b
--- /dev/null
@@ -0,0 +1,12 @@
+
+CC      = gcc
+CFLAGS  = -g -O0 -Wall -Wshadow -Werror -DHAVE_PTHREAD_H
+LDFLAGS = -lws2_32 -lpthread
+
+OBJ     = ../../IOEngine_epoll.o ../../IOEngine_kevent.o ../../IOEngine_select.o ../../IOEngine_win32.o ../../IOHandler.o ../../IOHandler_SSL.o iotest.o
+
+all: $(OBJ)
+       $(CC) $(CFLAGS) -oiotest $(OBJ) $(LDFLAGS)
+
+%.o: %.c
+       $(CC) $(CFLAGS) -o$@ -c $<
diff --git a/src/test/socket/iotest.c b/src/test/socket/iotest.c
new file mode 100644 (file)
index 0000000..15f32f4
--- /dev/null
@@ -0,0 +1,63 @@
+/* main.c - IOMultiplexer
+ * Copyright (C) 2012  Philipp Kreil (pk910)
+ * 
+ * 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 <stdio.h>
+#include "../IOHandler.h"
+
+static IOHANDLER_CALLBACK(io_callback);
+static IOHANDLER_LOG_BACKEND(io_log);
+
+static struct IODescriptor *irc_iofd = NULL;
+
+int main(int argc, char *argv[]) {
+    iolog_backend = io_log;
+    
+    irc_iofd = iohandler_connect("pk910.de", 6667, 0, NULL, io_callback);
+    
+    struct IODescriptor *iofd;
+    iofd = iohandler_add(0, IOTYPE_STDIN, NULL, io_callback);
+    iofd->read_lines = 1;
+    
+    while(1) {
+        iohandler_poll();
+    }
+}
+
+static IOHANDLER_CALLBACK(io_callback) {
+    switch(event->type) {
+        case IOEVENT_CONNECTED:
+            printf("[connect]\n");
+            break;
+        case IOEVENT_CLOSED:
+            printf("[disconnect]\n");
+            break;
+        case IOEVENT_RECV:
+            if(event->iofd->type == IOTYPE_STDIN) {
+                iohandler_printf(irc_iofd, "%s\n", event->data.recv_str);
+                printf("[out] %s\n", event->data.recv_str);
+            } else
+                printf("[in] %s\n", event->data.recv_str);
+            break;
+        
+        default:
+            break;
+    }
+}
+
+static IOHANDLER_LOG_BACKEND(io_log) {
+    //printf("%s", line);
+}
diff --git a/src/test/timer/Makefile b/src/test/timer/Makefile
new file mode 100644 (file)
index 0000000..724e34b
--- /dev/null
@@ -0,0 +1,12 @@
+
+CC      = gcc
+CFLAGS  = -g -O0 -Wall -Wshadow -Werror -DHAVE_PTHREAD_H
+LDFLAGS = -lws2_32 -lpthread
+
+OBJ     = ../../IOEngine_epoll.o ../../IOEngine_kevent.o ../../IOEngine_select.o ../../IOEngine_win32.o ../../IOHandler.o ../../IOHandler_SSL.o iotest.o
+
+all: $(OBJ)
+       $(CC) $(CFLAGS) -oiotest $(OBJ) $(LDFLAGS)
+
+%.o: %.c
+       $(CC) $(CFLAGS) -o$@ -c $<
diff --git a/src/test/timer/iotest.c b/src/test/timer/iotest.c
new file mode 100644 (file)
index 0000000..02b8c4b
--- /dev/null
@@ -0,0 +1,78 @@
+/* main.c - IOMultiplexer
+ * Copyright (C) 2012  Philipp Kreil (pk910)
+ * 
+ * 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 <stdio.h>
+#include "../../IOHandler.h"
+
+#define TEST_DURATION 100
+
+static IOHANDLER_CALLBACK(io_callback);
+static IOHANDLER_LOG_BACKEND(io_log);
+
+static struct timeval test_clock1, test_clock2;
+static int timercount;
+
+static void add_timer(int ms) {
+    struct timeval timeout;
+    gettimeofday(&timeout, NULL);
+    timeout.tv_usec += (ms % 1000) * 1000;
+    timeout.tv_sec += (ms / 1000);
+    if(timeout.tv_usec > 1000000) {
+        timeout.tv_usec -= 1000000;
+        timeout.tv_sec++;
+    }
+    iohandler_timer(timeout, io_callback);
+}
+
+int main(int argc, char *argv[]) {
+    iolog_backend = io_log;
+    
+    gettimeofday(&test_clock1, NULL);
+    gettimeofday(&test_clock2, NULL);
+    add_timer(TEST_DURATION);
+    timercount = 0;
+    
+    printf("[timer 0] %ld.%ld\n", test_clock1.tv_sec, test_clock1.tv_usec);
+    
+    while(1) {
+        iohandler_poll();
+    }
+}
+
+static IOHANDLER_CALLBACK(io_callback) {
+    struct timeval curr_time;
+    int diff1;
+    double diff2;
+    switch(event->type) {
+        case IOEVENT_TIMEOUT:
+            add_timer(TEST_DURATION);
+            timercount++;
+            gettimeofday(&curr_time, NULL);
+            diff1 = (curr_time.tv_sec - test_clock1.tv_sec) * 1000 + ((curr_time.tv_usec - test_clock1.tv_usec) / 1000);
+            diff2 = (curr_time.tv_sec - test_clock2.tv_sec) * 1000 + ((curr_time.tv_usec - test_clock2.tv_usec) / 1000.0);
+            diff2 -= (timercount * TEST_DURATION);
+            gettimeofday(&test_clock1, NULL);
+            printf("[timer %03d] %ld.%06ld [%d ms]  accuracy: %f ms\n", timercount, curr_time.tv_sec, curr_time.tv_usec, diff1, diff2);
+            break;
+        default:
+            break;
+    }
+}
+
+static IOHANDLER_LOG_BACKEND(io_log) {
+    //printf("%s", line);
+}