From 82b55fbd78b2602f37f96d52371d47fa73eb37c8 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Wed, 12 Nov 2014 19:13:29 +0000 Subject: [PATCH] Ensure Nagle is always off. --- .../map/AbstractChannelReplicator.java | 2 + .../chronicle/map/StatelessChronicleMap.java | 2 + .../openhft/chronicle/map/TcpReplicator.java | 1 - .../chronicle/map/example/ServersMapMain.java | 87 +++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java diff --git a/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java b/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java index f38d9a7ee..a18173bdc 100644 --- a/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java @@ -88,6 +88,8 @@ abstract class AbstractChannelReplicator implements Closeable { try { result = SocketChannel.open(); + result.socket().setTcpNoDelay(true); + } finally { if (result != null) try { diff --git a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java index 108ed1dc5..465aa0b88 100644 --- a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java @@ -135,6 +135,8 @@ class StatelessChronicleMap implements ChronicleMap, Closeable { try { result = AbstractChannelReplicator.openSocketChannel(closeables); result.connect(builder.remoteAddress()); + result.socket().setTcpNoDelay(true); + doHandShaking(result); break; } catch (IOException e) { diff --git a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java index 945ddf50c..76cc3de9a 100644 --- a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java @@ -754,7 +754,6 @@ final class TcpReplicator extends AbstractChannelReplicator implements Closeable socketChannel.socket().setReuseAddress(true); socketChannel.socket().setSoLinger(false, 0); socketChannel.socket().setSoTimeout(0); - socketChannel.socket().setTcpNoDelay(true); try { socketChannel.connect(details.address()); diff --git a/src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java b/src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java new file mode 100644 index 000000000..140da1661 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java @@ -0,0 +1,87 @@ +/* + * 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.chronicle.map.example; + +import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.map.ChronicleMapBuilder; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; + +/** + * Created by peter on 12/11/14. + */ +public class ServersMapMain { + static final int port = Integer.getInteger("port", 8989); + static final int entries = Integer.getInteger("entries", 10000); + static final int runs = Integer.getInteger("runs", 5); + + public static void startServer() throws IOException { + File file = File.createTempFile("testServersMapMain", ".deleteme"); + file.deleteOnExit(); + final ChronicleMap serverMap = ChronicleMapBuilder.of(byte[].class, byte[].class) + .putReturnsNull(true) + .entrySize(50) + .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)) + .createPersistedTo(file); + System.out.println("Server started"); + System.in.read(); + serverMap.close(); + } + + public static void startRemoteClient(String hostname) throws IOException { + final ChronicleMap map = ChronicleMapBuilder.of( + byte[].class, byte[].class) + .putReturnsNull(true) + .statelessClient(new InetSocketAddress(hostname, port)).create(); + ByteBuffer key = ByteBuffer.allocate(8); + ByteBuffer value = ByteBuffer.allocate(32); + double lastAveragePut = Double.POSITIVE_INFINITY, lastAverageGet = Double.POSITIVE_INFINITY; + for (int i = 0; i < runs; i++) { + long puts = 0, gets = 0; + for (int j = 0; j < entries; j++) { + key.putLong(0, j); + value.putLong(0, j); + long t1 = System.nanoTime(); + map.put(key.array(), value.array()); + long t2 = System.nanoTime(); + map.get(key.array()); + long t3 = System.nanoTime(); + puts += t2 - t1; + gets += t3 - t2; + if (t2 - t1 > lastAveragePut * 10 || t3 - t2 > lastAverageGet * 10) + System.out.printf("Took put/get took %.1f/%.1f us%n", (t2 - t1) / 1e3, (t3 - t2) / 1e3); + } + lastAveragePut = puts / entries; + lastAverageGet = gets / entries; + System.out.printf("Average took put/get took %.1f/%.1f us%n", lastAveragePut / 1e3, lastAverageGet / 1e3); + } + } + + public static void main(String[] args) throws IOException { + if (args.length > 0) + startRemoteClient(args[0]); + else + startServer(); + } + +}