From 19750050f3471714b43f7f8805d5f63abfc52cc4 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Wed, 11 Apr 2007 07:37:15 +0000 Subject: [PATCH] more brutal testing --- test/Makefile | 7 +++-- test/asynctest.c | 69 ++++++++++++++++++++++++++++++++++++++---------- test/conntest.sh | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 16 deletions(-) create mode 100755 test/conntest.sh diff --git a/test/Makefile b/test/Makefile index f4e6294..f774053 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,12 +2,15 @@ PGINC = -I$(shell pg_config --includedir) PGLIB = -L$(shell pg_config --libdir) -CFLAGS = -O2 -g -Wall $(PGINC) -I$(HOME)/src/libevent -I../src -LDFLAGS = $(PGLIB) -lpq -L$(HOME)/src/libevent/.libs -levent +include ../config.mak + +CPPFLAGS += -I../src $(PGINC) +LDFLAGS += $(PGLIB) -lpq -levent all: asynctest asynctest: asynctest.c + $(CC) -o $@ $< $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) clean: rm -f asynctest diff --git a/test/asynctest.c b/test/asynctest.c index 5d7a074..dadfc7e 100644 --- a/test/asynctest.c +++ b/test/asynctest.c @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include #include @@ -25,6 +27,9 @@ typedef enum { false=0, true=1 } bool; #include "list.h" +static LIST(idle_list); +static LIST(active_list); + typedef struct DbConn { List head; const char *connstr; @@ -32,11 +37,22 @@ typedef struct DbConn { //time_t connect_time; //unsigned query_count; PGconn *con; - const char *query; + //const char *query; } DbConn; -static LIST(idle_list); -static LIST(active_list); +static char *bulk_data; +static int bulk_data_max = 128*1024; /* power of 2 */ + +/* fill mem with random junk */ +static void init_bulk_data(void) +{ + int i; + bulk_data = malloc(bulk_data_max + 1); + for (i = 0; i < bulk_data_max; i++) { + bulk_data[i] = 'a' + (i % 26); + } + bulk_data[i] = 0; +} static DbConn *new_db(const char *connstr) { @@ -65,8 +81,8 @@ static void set_active(DbConn *db) static void conn_error(DbConn *db, const char *desc) { if (db->con) { - printf("libpq error in %s: %s\n", - desc, PQerrorMessage(db->con)); + //printf("libpq error in %s: %s\n", + // desc, PQerrorMessage(db->con)); PQfinish(db->con); db->con = NULL; } else { @@ -161,14 +177,29 @@ static void send_cb(int sock, short flags, void *arg) static void send_query(DbConn *db) { int res; + const char *q = "select $1::text"; + const char *values[1]; + int lengths[1]; + int fmts[1]; + int arglen; + + arglen = random() & (bulk_data_max - 1); + values[0] = bulk_data + bulk_data_max - arglen; + lengths[0] = arglen; + fmts[0] = 1; /* send query */ - res = PQsendQueryParams(db->con, db->query, 0, - NULL, /* paramTypes */ - NULL, /* paramValues */ - NULL, /* paramLengths */ - NULL, /* paramFormats */ - 0); /* resultformat, 0-text, 1-bin */ + if ((random() & 63) == 0) { + res = PQsendQueryParams(db->con, "select pg_sleep(0.2)", 0, + NULL, NULL, NULL, NULL, 0); + } else { + res = PQsendQueryParams(db->con, q, 1, + NULL, /* paramTypes */ + values, /* paramValues */ + lengths,/* paramLengths */ + fmts, /* paramFormats */ + 1); /* resultformat, 0-text, 1-bin */ + } if (!res) { conn_error(db, "PQsendQueryParams"); return; @@ -242,10 +273,20 @@ int main(void) int i; DbConn *db; List *item, *tmp; + unsigned seed; - for (i = 0; i < 10; i++) { - db = new_db("dbname=marko port=6000 host=/tmp"); - db->query = "select 1"; + seed = time(NULL) ^ getpid(); + printf("using seed: %u\n", seed); + srandom(seed); + + init_bulk_data(); + + for (i = 0; i < 50; i++) { + db = new_db("dbname=marko port=6000 host=127.0.0.1 password=kama"); + list_append(&db->head, &idle_list); + } + for (i = 0; i < 50; i++) { + db = new_db("dbname=marko port=7000 host=127.0.0.1 password=kama"); list_append(&db->head, &idle_list); } diff --git a/test/conntest.sh b/test/conntest.sh new file mode 100755 index 0000000..3a7024f --- /dev/null +++ b/test/conntest.sh @@ -0,0 +1,54 @@ +#!/bin/sh + +fw_drop_port() { + echo "fw_drop_port" + case `uname` in + Linux) + sudo iptables -A OUTPUT -p tcp --dport $1 -j DROP;; + Darwin) + sudo ipfw add 100 drop tcp from any to 127.0.0.1 dst-port $1;; + *) + echo "Unknown OS";; + esac +} +fw_reject_port() { + echo "fw_reject_port" + case `uname` in + Linux) + sudo iptables -A OUTPUT -p tcp --dport $1 -j REJECT --reject-with tcp-reset;; + Darwin) + sudo ipfw add 100 reset tcp from any to 127.0.0.1 dst-port $1;; + *) + echo "Unknown OS";; + esac +} + +fw_reset() { + echo "fw_reset" + case `uname` in + Linux) + sudo iptables -F;; + Darwin) + sudo ipfw del 100;; + *) + echo "Unknown OS"; exit 1;; + esac +} + +port=5432 +port=7000 + +fw_reset + +while true; do + #fw_drop_port $port + #sleep 20 + #fw_reset + #sleep 20 + fw_reject_port $port + sleep 3 + fw_reset + sleep 6 +done + + -- 2.40.0