apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dr...@apache.org
Subject svn commit: r416904 - in /apr/apr/trunk/test: ./ Makefile.in echod.c sockperf.c
Date Sat, 24 Jun 2006 12:00:59 GMT
Author: dreid
Date: Sat Jun 24 05:00:58 2006
New Revision: 416904

URL: http://svn.apache.org/viewvc?rev=416904&view=rev
Log:
Add a simple echod and a sockperf test that allows us to get some (very) basic
timings for sockets. The only aim of these is to allow us to compare the
performance of apr's network code if/when we start changing it.


Added:
    apr/apr/trunk/test/echod.c
    apr/apr/trunk/test/sockperf.c
Modified:
    apr/apr/trunk/test/   (props changed)
    apr/apr/trunk/test/Makefile.in

Propchange: apr/apr/trunk/test/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sat Jun 24 05:00:58 2006
@@ -53,3 +53,5 @@
 testshmproducer
 testshmproducer.exe
 lfstests
+sockperf
+echod

Modified: apr/apr/trunk/test/Makefile.in
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/Makefile.in?rev=416904&r1=416903&r2=416904&view=diff
==============================================================================
--- apr/apr/trunk/test/Makefile.in (original)
+++ apr/apr/trunk/test/Makefile.in Sat Jun 24 05:00:58 2006
@@ -17,7 +17,9 @@
 	testmutexscope@EXEEXT@ \
 	testall@EXEEXT@
 
-OTHER_PROGRAMS = sendfile@EXEEXT@ 
+OTHER_PROGRAMS = sendfile@EXEEXT@ \
+                 echod@EXEEXT@ \
+                 sockperf@EXEEXT@
 
 PROGRAMS = $(STDTEST_PORTABLE) $(STDTEST_NONPORTABLE) $(OTHER_PROGRAMS)
 
@@ -95,6 +97,12 @@
 
 testmutexscope@EXEEXT@: testmutexscope.lo $(LOCAL_LIBS)
 	$(LINK_PROG) testmutexscope.lo $(LOCAL_LIBS) $(ALL_LIBS)
+
+echod@EXEEXT@: echod.lo $(LOCAL_LIBS)
+	$(LINK_PROG) echod.lo $(LOCAL_LIBS) $(ALL_LIBS)
+
+sockperf@EXEEXT@: sockperf.lo $(LOCAL_LIBS)
+	$(LINK_PROG) sockperf.lo $(LOCAL_LIBS) $(ALL_LIBS)
 
 TESTS = testutil.lo testtime.lo teststr.lo testvsn.lo testipsub.lo \
 	testmmap.lo testud.lo testtable.lo testsleep.lo testpools.lo \

Added: apr/apr/trunk/test/echod.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/echod.c?rev=416904&view=auto
==============================================================================
--- apr/apr/trunk/test/echod.c (added)
+++ apr/apr/trunk/test/echod.c Sat Jun 24 05:00:58 2006
@@ -0,0 +1,135 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as
+ * applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Simple echo daemon, designed to be used for network throughput
+ * benchmarks. The aim is to allow us to monitor changes in performance
+ * of APR networking code, nothing more.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>  /* for atexit() */
+
+#include "apr.h"
+#include "apr_network_io.h"
+#include "apr_strings.h"
+
+#define BUF_SIZE 4096
+
+static void reportError(const char *msg, apr_status_t rv, 
+                        apr_pool_t *pool)
+{
+    fprintf(stderr, "%s\nError: %d\n'%s'\n", msg, rv,
+            apr_psprintf(pool, "%pm", &rv));
+}
+
+static apr_status_t talkTalk(apr_socket_t *socket, apr_pool_t *parent)
+{
+    apr_pool_t *pool;
+    apr_size_t len;
+    char *buf;
+    apr_status_t rv;
+
+    if (apr_pool_create(&pool, parent) != APR_SUCCESS)
+        return APR_ENOPOOL;
+
+
+    buf = apr_palloc(pool, BUF_SIZE);
+    if (!buf)
+        return ENOMEM;
+
+    do {
+        len = BUF_SIZE;
+        rv = apr_socket_recv(socket, buf, &len);
+        if (APR_STATUS_IS_EOF(rv) || len == 0 || rv != APR_SUCCESS)
+            break;
+        rv = apr_socket_send(socket, buf, &len);
+        if (len == 0 || rv != APR_SUCCESS)
+            break;
+    } while (rv == APR_SUCCESS);
+
+    apr_pool_clear(pool);
+    return APR_SUCCESS;
+}
+
+static apr_status_t glassToWall(apr_int16_t port, apr_pool_t *parent)
+{
+    apr_sockaddr_t *sockAddr;
+    apr_socket_t *listener, *accepted;
+    apr_status_t rv;
+
+    rv = apr_socket_create(&listener, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
+                           parent);
+    if (rv != APR_SUCCESS) {
+        reportError("Unable to create socket", rv, parent);
+        return rv;
+    }
+
+    rv = apr_sockaddr_info_get(&sockAddr, "127.0.0.1", APR_UNSPEC,
+                               port, 0, parent);
+    if (rv != APR_SUCCESS) {
+        reportError("Unable to get socket info", rv, parent);
+        apr_socket_close(listener);
+        return rv;
+    }
+
+    if ((rv = apr_socket_bind(listener, sockAddr)) != APR_SUCCESS ||
+        (rv = apr_socket_listen(listener, 5)) != APR_SUCCESS) {
+        reportError("Unable to bind or listen to socket", rv, parent);
+        apr_socket_close(listener);
+        return rv;
+    }
+
+    for (;;) {
+        rv = apr_socket_accept(&accepted, listener, parent);
+        if (rv != APR_SUCCESS) {
+            reportError("Error accepting on socket", rv, parent);
+            break;
+        }
+        printf("\tAnswering connection\n");
+        rv = talkTalk(accepted, parent);
+        apr_socket_close(accepted);
+        printf("\tConnection closed\n");
+        if (rv != APR_SUCCESS)
+            break;
+    }
+
+    apr_socket_close(listener);
+    return APR_SUCCESS;
+}
+
+int main(int argc, char **argv)
+{
+    apr_pool_t *pool;
+    apr_status_t rv;
+    apr_int16_t theport = 4747;
+
+    printf("APR Test Application: echod\n");
+
+    apr_initialize();
+    atexit(apr_terminate);
+
+    apr_pool_create(&pool, NULL);
+
+    if (argc >= 2) {
+        printf("argc = %d, port = '%s'\n", argc, argv[1]);
+        theport = atoi(argv[1]);
+    }
+
+    fprintf(stdout, "Starting to listen on port %d\n", theport);
+    rv = glassToWall(theport, pool);
+
+    return 0;
+}

Added: apr/apr/trunk/test/sockperf.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/sockperf.c?rev=416904&view=auto
==============================================================================
--- apr/apr/trunk/test/sockperf.c (added)
+++ apr/apr/trunk/test/sockperf.c Sat Jun 24 05:00:58 2006
@@ -0,0 +1,241 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as
+ * applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* sockperf.c
+ * This simple network client tries to connect to an echo daemon (echod)
+ * listening on a port it supplies, then time how long it takes to
+ * reply with packets of varying sizes.
+ * It prints results once completed.
+ *
+ * To run,
+ *
+ *   ./echod &
+ *   ./sockperf
+ */
+
+#include <stdio.h>
+#include <stdlib.h>  /* for atexit() */
+
+#include "apr.h"
+#include "apr_network_io.h"
+#include "apr_strings.h"
+
+#define MAX_ITERS    10
+#define TEST_SIZE  1024
+
+struct testSet {
+    char c;
+    int size;
+    int iters;
+} testRuns[] = {
+    { 'a', 1, 3 },
+    { 'b', 4, 3 },
+    { 'c', 16, 5 },
+    { 'd', 64, 5 },
+    { 'e', 256, 10 },
+};
+
+struct testResult {
+    int size;
+    int iters;
+    apr_time_t msecs[MAX_ITERS];
+    apr_time_t avg;
+};
+
+static apr_int16_t testPort = 4747;
+static apr_sockaddr_t *sockAddr = NULL;
+
+static void reportError(const char *msg, apr_status_t rv, 
+                        apr_pool_t *pool)
+{
+    fprintf(stderr, "%s\n", msg);
+    if (rv != APR_SUCCESS)
+        fprintf(stderr, "Error: %d\n'%s'\n", rv,
+                apr_psprintf(pool, "%pm", &rv));
+    
+}
+
+static closeConnection(apr_socket_t *sock)
+{
+    apr_size_t len = 0;
+    apr_socket_send(sock, NULL, &len);
+}
+
+static apr_status_t sendRecvBuffer(apr_time_t *t, const char *buf, 
+                                   int size, apr_pool_t *pool)
+{
+    apr_socket_t *sock;
+    apr_status_t rv;
+    apr_size_t len = size, thistime = size;
+    char *recvBuf;
+    apr_time_t testStart = apr_time_now(), testEnd;
+    int i;
+
+    if (! sockAddr) {
+        rv = apr_sockaddr_info_get(&sockAddr, "127.0.0.1", APR_UNSPEC,
+                                   testPort, 0, pool);
+        if (rv != APR_SUCCESS) {
+            reportError("Unable to get socket info", rv, pool);
+            return rv;
+        }
+
+        /* make sure we can connect to daemon before we try tests */
+
+        rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
+                           pool);
+        if (rv != APR_SUCCESS)
+            return rv;
+
+        rv = apr_socket_connect(sock, sockAddr);
+        if (rv != APR_SUCCESS) {
+            reportError("Unable to connect to echod!", rv, pool);
+            apr_socket_close(sock);
+            return rv;
+        }
+        apr_socket_close(sock);
+
+    }
+
+    recvBuf = apr_palloc(pool, size);
+    if (! recvBuf)
+        return ENOMEM;
+    *t = 0;
+
+    /* START! */
+    testStart = apr_time_now();
+    rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
+                           pool);
+    if (rv != APR_SUCCESS)
+        return rv;
+
+    rv = apr_socket_connect(sock, sockAddr);
+    if (rv != APR_SUCCESS) {
+        reportError("Unable to connect to echod!", rv, pool);
+        apr_socket_close(sock);
+        return rv;
+    }
+
+    for (i = 0; i < 3; i++) {
+
+        len = size;
+        thistime = size;
+
+        rv = apr_socket_send(sock, buf, &len);
+        if (rv != APR_SUCCESS || len != size) {
+            reportError(apr_psprintf(pool, 
+                         "Unable to send data correctly (iteration %d of 3)",
+                         i) , rv, pool);
+            closeConnection(sock);
+            apr_socket_close(sock);
+            return rv;
+        }
+    
+        do {
+            len = thistime;
+            rv = apr_socket_recv(sock, &recvBuf[size - thistime], &len);
+            if (rv != APR_SUCCESS)
+                break;
+            thistime -= len;
+        } while (thistime);
+    }
+
+    closeConnection(sock);
+    apr_socket_close(sock);
+    testEnd = apr_time_now();
+    /* STOP! */
+
+    if (thistime) {
+        reportError("Received less than we sent :-(", rv, pool);
+        return rv;
+    }        
+    if (strncmp(recvBuf, buf, size) != 0) {
+        reportError("Received corrupt data :-(", 0, pool);
+        printf("We sent:\n%s\nWe received:\n%s\n", buf, recvBuf);
+        return EINVAL;
+    }
+    *t = testEnd - testStart;
+    return APR_SUCCESS;
+}
+
+static apr_status_t runTest(struct testSet *ts, struct testResult *res,
+                            apr_pool_t *pool)
+{
+    char *buffer;
+    apr_status_t rv;
+    int i, sz = ts->size * TEST_SIZE;
+    
+    buffer = apr_palloc(pool, sz);
+    if (!buffer) {
+        reportError("Unable to allocate buffer", ENOMEM, pool);
+        return ENOMEM;
+    }
+    memset(buffer, ts->c, sz);
+
+    res->iters = ts->iters > MAX_ITERS ? MAX_ITERS : ts->iters;
+
+    for (i = 0; i < res->iters; i++) {
+        apr_time_t iterTime;
+        rv = sendRecvBuffer(&iterTime, buffer, sz, pool);
+        if (rv != APR_SUCCESS) {
+            res->iters = i;
+            break;
+        }
+        res->msecs[i] = iterTime;
+    }
+
+    return rv;
+}
+
+int main(int argc, char **argv)
+{
+    apr_pool_t *pool;
+    apr_status_t rv;
+    int i;
+    int nTests = sizeof(testRuns) / sizeof(testRuns[0]);
+    struct testResult *results;
+
+    printf("APR Test Application: sockperf\n");
+
+    apr_initialize();
+    atexit(apr_terminate);
+
+    apr_pool_create(&pool, NULL);
+
+    results = (struct testResult *)apr_pcalloc(pool, 
+                                        sizeof(*results) * nTests);
+
+    for(i = 0; i < nTests; i++) {
+        printf("Test -> %c\n", testRuns[i].c);
+        results[i].size = testRuns[i].size * TEST_SIZE;
+        rv = runTest(&testRuns[i], &results[i], pool);
+    }
+
+    printf("Tests Complete!\n");
+    for(i = 0; i < nTests; i++) {
+        int j;
+        apr_time_t totTime = 0;
+        printf("%10d byte block:\n", results[i].size);
+        printf("\t%2d iterations : ", results[i].iters);
+        for (j = 0; j < results[i].iters; j++) {
+            printf("%6Ld ", results[i].msecs[j]);
+            totTime += results[i].msecs[j];
+        }
+        printf("<\n");
+        printf("\t  Average: %6Ld\n", totTime / results[i].iters);
+    }
+
+    return 0;
+}



Mime
View raw message