Search in sources :

Example 6 with FutureOperation

use of org.webpieces.nio.api.handlers.FutureOperation in project webpieces by deanhiller.

the class ZPerformanceSuper method testLargeReadWrite.

/**
	 * This is the difference in performance of writing/reading secure data vs.
	 * writing/reading non-secure data for a very small payload.  
	 * Basic seems to be 75% of secure's time. This is a slowdown of 133% 
	 * for echoing 'hello'.
	 * 
     * Basic....
     * total write time         = 1402 ms
     * total write/response time= 1433 ms
     * time per write/response  = 35   ms

     * Secure....
     * total write time         = 6119 ms
     * total write/response time= 6159 ms
     * time per write/response  = 153 ms
     *
	 * @throws Exception
	 */
public void testLargeReadWrite() throws Exception {
    ByteBuffer b = ByteBuffer.allocate(4000);
    log.info("getting all proper connections");
    int size = 40;
    String[] methodNames = new String[size];
    for (int i = 0; i < size; i++) {
        methodNames[i] = "connected";
    }
    TCPChannel[] clients = new TCPChannel[size];
    for (int i = 0; i < size; i++) {
        clients[i] = chanMgr.createTCPChannel("Client[" + i + "]", getClientFactoryHolder());
        clients[i].oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    }
    mockConnect.expect(methodNames);
    log.info("done getting all connections");
    for (TCPChannel client : clients) {
        client.registerForReads((DataListener) mockHandler);
    }
    int numWrites = 100;
    String payload = "hello";
    for (int i = 0; i < 3000; i++) {
        payload += "i";
    }
    helper.putString(b, payload);
    helper.doneFillingBuffer(b);
    int numBytes = b.remaining();
    log.info("size=" + b.remaining());
    methodNames = new String[size * numWrites];
    for (int i = 0; i < size * numWrites; i++) {
        methodNames[i] = "incomingData";
    }
    PerfTimer timer = new PerfTimer();
    PerfTimer timer2 = new PerfTimer();
    timer.start();
    timer2.start();
    for (TCPChannel client : clients) {
        for (int i = 0; i < numWrites; i++) {
            FutureOperation future = client.write(b);
            future.waitForOperation(5000);
            b.rewind();
        }
    }
    long result2 = timer2.stop();
    CalledMethod[] methods = mockHandler.expect(methodNames);
    long result = timer.stop();
    ByteBuffer actualBuf = (ByteBuffer) methods[6].getAllParams()[1];
    String actual = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals(payload, actual);
    log.info("payload=" + actual);
    long readWriteTime = result / size;
    long byteTime = 100 * result / (numWrites * numBytes);
    log.info("total write time         =" + result2);
    log.info("total write/read time    =" + result);
    log.info("--time per 100 bytes     =" + byteTime);
    log.info("test result info:");
    log.info("--time per write/read    =" + readWriteTime);
    log.info("--time to beat           =" + getLargerReadWriteTimeLimit());
    assertTrue(readWriteTime < getLargerReadWriteTimeLimit());
}
Also used : PerfTimer(org.webpieces.nio.test.PerfTimer) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) FutureOperation(org.webpieces.nio.api.handlers.FutureOperation) CalledMethod(biz.xsoftware.mock.CalledMethod)

Aggregations

FutureOperation (org.webpieces.nio.api.handlers.FutureOperation)6 CalledMethod (biz.xsoftware.mock.CalledMethod)4 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)4 ByteBuffer (java.nio.ByteBuffer)3 PerfTimer (org.webpieces.nio.test.PerfTimer)3 CloneByteBuffer (org.webpieces.nio.api.testutil.CloneByteBuffer)2 InetSocketAddress (java.net.InetSocketAddress)1 DataListener (org.webpieces.nio.api.handlers.DataListener)1