package com.paritytrading.nassau.soupbintcp.perf;

import static org.jvirtanen.util.Applications.*;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

class PerfTest {

    private static final String USAGE = "nassau-soupbintcp-perf-test <packets>";

    public static void main(String[] args) throws IOException {
        if (args.length != 1)
            usage(USAGE);

        try {
            main(Integer.parseInt(args[0]));
        } catch (NumberFormatException e) {
            usage(USAGE);
        }
    }

    private static void main(int packets) throws IOException {
        final Server server = Server.open(new InetSocketAddress(0));
        final Client client = Client.connect(server.getLocalAddress());

        new Thread(new Runnable() {

            @Override
            public void run() {
                Server.Session session = null;

                try {
                    session = server.accept();

                    server.close();

                    while (session.receive() != -1);
                } catch (IOException e) {
                } finally {
                    try {
                        if (session != null)
                            session.close();
                    } catch (IOException e) {
                    }
                }
            }

        }).start();

        ByteBuffer buffer = ByteBuffer.wrap(new byte[] { 'f', 'o', 'o' });

        System.out.println("Warming up...");

        for (int i = 0; i < packets; i++) {
            client.send(buffer);

            buffer.flip();

            client.receive();
        }

        client.getHistogram().reset();

        System.out.println("Benchmarking...");

        for (int i = 0; i < packets; i++) {
            client.send(buffer);

            buffer.flip();

            client.receive();
        }

        client.close();

        System.out.printf("Results (n = %d):\n", packets);
        System.out.printf("\n");
        System.out.printf( "      Min: %10.2f µs\n", client.getHistogram().getMinValue()                / 1000.0);
        System.out.printf("   50.00%%: %10.2f µs\n", client.getHistogram().getValueAtPercentile(50.00)  / 1000.0);
        System.out.printf("   90.00%%: %10.2f µs\n", client.getHistogram().getValueAtPercentile(90.00)  / 1000.0);
        System.out.printf("   99.00%%: %10.2f µs\n", client.getHistogram().getValueAtPercentile(99.00)  / 1000.0);
        System.out.printf("   99.90%%: %10.2f µs\n", client.getHistogram().getValueAtPercentile(99.90)  / 1000.0);
        System.out.printf("   99.99%%: %10.2f µs\n", client.getHistogram().getValueAtPercentile(99.99)  / 1000.0);
        System.out.printf("  100.00%%: %10.2f µs\n", client.getHistogram().getValueAtPercentile(100.00) / 1000.0);
        System.out.printf("\n");
    }

}

@@ -781,84 +781,16 @@
 ead(
-new Runnable() %7B%0A%0A            @Override%0A            public void run() %7B%0A    
+() -%3E %7B%0A
     
@@ -829,20 +829,16 @@
  null;%0A%0A
-    
         
@@ -859,28 +859,24 @@
             
-    
 session = se
@@ -887,28 +887,24 @@
 .accept();%0A%0A
-    
             
@@ -940,20 +940,16 @@
         
-    
 while (s
@@ -977,36 +977,32 @@
 1);%0A            
-    
 %7D catch (IOExcep
@@ -1023,20 +1023,16 @@
         
- 
-   
 %7D finall
@@ -1043,36 +1043,32 @@
                 
-    
 try %7B%0A          
@@ -1073,28 +1073,24 @@
             
- 
-   
 if (session 
@@ -1118,28 +1118,24 @@
             
-    
 session.clos
@@ -1147,36 +1147,32 @@
                 
-  
-  
 %7D catch (IOExcep
@@ -1185,30 +1185,8 @@
 ) %7B%0A
-                    %7D%0A
     
@@ -1205,33 +1205,32 @@
 %7D%0A            %7D%0A
-%0A
         %7D).start

Clean up SoupBinTCP performance test