<commit_before>/*
 * Copyright 2014 Higher Frequency Trading
 *
 * http://www.higherfrequencytrading.com
 *
 * 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.
 */

package net.openhft.performance.tests.vanilla.tcp;

import net.openhft.affinity.AffinitySupport;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

/**
 * @author peter.lawrey
 */
/* Both ends are run with -Xmx64m -verbose:gc

///////// EchoServerMain
On a E5-2650 v2 over loopback with onload
Throughput was 2880.4 MB/s
Loop back echo latency was 5.8/6.2 9.6/19.4 23.2us for 50/90 99/99.9 99.99%tile

On an i7-3970X over loopback
Throughput was 3259.0 MB/s
Loop back echo latency was 4.7/5.2 6/7 11/97 us for 50/90 99/99.9 99.99/worst %tile

Two connections
Throughput was 3925.0 MB/s
Loop back echo latency was 6.5/7.3 9/10 13/49 us for 50/90 99/99.9 99.99/worst %tile

Four connections
Throughput was 4109.9 MB/s
Loop back echo latency was 10.9/12.2 18/21 33/425 us for 50/90 99/99.9 99.99/worst %tile

Ten connections
Throughput was 2806.6 MB/s
Loop back echo latency was 40.0/44.9 48/57 366/11880 us for 50/90 99/99.9 99.99/worst %tile

Between two servers via Solarflare with onload on server & client (no minor GCs)
Throughput was 1156.0 MB/s
Loop back echo latency was 12.2/12.5 21/25 28/465 us for 50/90 99/99.9 99.99/worst %tile

Between two servers via Solarflare with onload on client (no minor GCs)
Throughput was 867.5 MB/s
Loop back echo latency was 15.0/15.7 21/27 30/398 us for 50/90 99/99.9 99.99/worst %tile

//////// NettyEchoServer
Between two servers via Solarflare with onload on server & client (16 minor GCs)
Throughput was 968.7 MB/s
Loop back echo latency was 18.4/19.0 26/31 33/1236 us for 50/90 99/99.9 99.99/worst %tile

Between two servers via Solarflare with onload on client (16 minor GCs)
Throughput was 643.6 MB/s
Loop back echo latency was 20.8/21.8 29/34 38/2286 us for 50/90 99/99.9 99.99/worst %tile


*/


public class EchoClientMain {
    public static final int PORT = 8007;

    public static void main(String... args) throws IOException, InterruptedException {
        AffinitySupport.setAffinity(1L << 3);
        String hostname = (args.length == 0) ? "localhost" : args[0];
        int port = args.length < 2 ? PORT : Integer.parseInt(args[1]);
        int repeats = 2;

        SocketChannel[] sockets = new SocketChannel[repeats];
        openConnections(hostname, port, sockets);
        testThroughput(sockets);
        closeConnections(sockets);
        openConnections(hostname, port, sockets);
        testLatency(sockets);
        closeConnections(sockets);
    }

    private static void openConnections(String hostname, int port, SocketChannel... sockets) throws IOException {
        for (int j = 0; j < sockets.length; j++) {
            sockets[j] = SocketChannel.open(new InetSocketAddress(hostname, port));
            sockets[j].socket().setTcpNoDelay(true);
            sockets[j].configureBlocking(false);
        }
    }

    private static void closeConnections(SocketChannel... sockets) throws IOException {
        for (Closeable socket : sockets)
            socket.close();
    }

    private static void testThroughput(SocketChannel... sockets) throws IOException, InterruptedException {
        System.out.println("Starting throughput test");
        int bufferSize = 32 * 1024;
        ByteBuffer bb = ByteBuffer.allocateDirect(bufferSize);
        int count = 0, window = 2;
        long start = System.nanoTime();
        while (System.nanoTime() - start < 10e9) {
            for (SocketChannel socket : sockets) {
                bb.clear();
                if (socket.write(bb) < 0)
                    throw new AssertionError("Socket " + socket + " unable to write in one go.");
            }
            if (count >= window)
                for (SocketChannel socket : sockets) {
                    bb.clear();<commit_msg>EchoClientMain to support multiple hosts<commit_after>/*
 * Copyright 2014 Higher Frequency Trading
 *
 * http://www.higherfrequencytrading.com
 *
 * 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.
 */

package net.openhft.performance.tests.vanilla.tcp;

import net.openhft.affinity.AffinitySupport;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

/**
 * @author peter.lawrey
 */
/* Both ends are run with -Xmx64m -verbose:gc

///////// EchoServerMain
On a E5-2650 v2 over loopback with onload
Throughput was 2880.4 MB/s
Loop back echo latency was 5.8/6.2 9.6/19.4 23.2us for 50/90 99/99.9 99.99%tile

On an i7-3970X over loopback
Throughput was 3259.0 MB/s
Loop back echo latency was 4.7/5.2 6/7 11/97 us for 50/90 99/99.9 99.99/worst %tile

Two connections
Throughput was 3925.0 MB/s
Loop back echo latency was 6.5/7.3 9/10 13/49 us for 50/90 99/99.9 99.99/worst %tile

Four connections
Throughput was 4109.9 MB/s
Loop back echo latency was 10.9/12.2 18/21 33/425 us for 50/90 99/99.9 99.99/worst %tile

Ten connections
Throughput was 2806.6 MB/s
Loop back echo latency was 40.0/44.9 48/57 366/11880 us for 50/90 99/99.9 99.99/worst %tile

Between two servers via Solarflare with onload on server & client (no minor GCs)
Throughput was 1156.0 MB/s
Loop back echo latency was 12.2/12.5 21/25 28/465 us for 50/90 99/99.9 99.99/worst %tile

Between two servers via Solarflare with onload on client (no minor GCs)
Throughput was 867.5 MB/s
Loop back echo latency was 15.0/15.7 21/27 30/398 us for 50/90 99/99.9 99.99/worst %tile

//////// NettyEchoServer
Between two servers via Solarflare with onload on server & client (16 minor GCs)
Throughput was 968.7 MB/s
Loop back echo latency was 18.4/19.0 26/31 33/1236 us for 50/90 99/99.9 99.99/worst %tile

Between two servers via Solarflare with onload on client (16 minor GCs)
Throughput was 643.6 MB/s
Loop back echo latency was 20.8/21.8 29/34 38/2286 us for 50/90 99/99.9 99.99/worst %tile


*/


public class EchoClientMain {
    public static final int PORT = Integer.getInteger("port", 8007);
    public static final int CLIENTS = Integer.getInteger("clients", 2);

    public static void main(String... args) throws IOException, InterruptedException {
        AffinitySupport.setAffinity(1L << 3);
        String[] hostnames = args.length > 0 ? args : "localhost".split(",");

        SocketChannel[] sockets = new SocketChannel[CLIENTS];
        openConnections(hostnames, PORT, sockets);
        testThroughput(sockets);
        closeConnections(sockets);
        openConnections(hostnames, PORT, sockets);
        testLatency(sockets);
        closeConnections(sockets);
    }

    private static void openConnections(String[] hostname, int port, SocketChannel... sockets) throws IOException {
        for (int j = 0; j < sockets.length; j++) {
            sockets[j] = SocketChannel.open(new InetSocketAddress(hostname[j % hostname.length], port));
            sockets[j].socket().setTcpNoDelay(true);
            sockets[j].configureBlocking(false);
        }
    }

    private static void closeConnections(SocketChannel... sockets) throws IOException {
        for (Closeable socket : sockets)
            socket.close();
    }

    private static void testThroughput(SocketChannel... sockets) throws IOException, InterruptedException {
        System.out.println("Starting throughput test");
        int bufferSize = 32 * 1024;
        ByteBuffer bb = ByteBuffer.allocateDirect(bufferSize);
        int count = 0, window = 2;
        long start = System.nanoTime();
        while (System.nanoTime() - start < 10e9) {
            for (SocketChannel socket : sockets) {
                bb.clear();
                if (socket.write(bb) < 0)
                    throw new AssertionError("Socket " + socket + " unable to write in one go.");
            }
            if (count >= window)
                for (SocketChannel socket : sockets) {
                    bb.clear();
{'fraction_non_alphanumeric': 0.09971634300676413, 'fraction_numerical': 0.07298712633646083, 'mean_word_length': 2.9444922547332184, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6543575', 'n_tokens_mistral': 3310, 'n_tokens_neox': 2748, 'n_words': 969}