From 033edc0e242e964d926c560d2e09506514894d37 Mon Sep 17 00:00:00 2001 From: Andrei Pozolotin Date: Sun, 6 Jan 2013 21:34:28 -0600 Subject: [PATCH] submission cleanup #8 --- .../udt/echo/bytes/ByteEchoClient.java | 8 +- .../udt/echo/bytes/ByteEchoServer.java | 6 +- .../udt/echo/message/MsgEchoClient.java | 8 +- .../udt/echo/message/MsgEchoServer.java | 6 +- .../udt/echo/rendevous/MsgEchoPeerBase.java | 4 +- .../udt/echo/rendevous/MsgEchoPeerTwo.java | 4 +- ...orterUDT.java => UtilConsoleReporter.java} | 20 +- ...FactoryUDT.java => UtilThreadFactory.java} | 7 +- .../udt/nio/NioUdtAcceptorChannel.java | 2 +- .../udt/nio/NioUdtByteConnectorChannel.java | 4 +- .../udt/nio/NioUdtMessageAcceptorChannel.java | 2 +- .../nio/NioUdtMessageConnectorChannel.java | 4 +- .../transport/udt/nio/NioUdtProvider.java | 2 +- .../netty/transport/udt/bench/BenchXfer.java | 50 ++++ .../transport/udt/bench/xfer/TcpNative.java | 66 +++++ .../transport/udt/bench/xfer/UdtNative.java | 270 ++++++++++++++++++ .../xfer/{BenchNetty.java => UdtNetty.java} | 12 +- .../io/netty/transport/udt/util/BootHelp.java | 6 +- .../transport/udt/util/CaliperBench.java | 75 ++++- .../transport/udt/util/CaliperMeasure.java | 73 +++-- .../{CaliperHelp.java => CaliperRunner.java} | 111 ++++--- .../udt/util/EchoMessageHandler.java | 2 + .../transport/udt/util/TrafficControl.java | 18 +- .../io/netty/transport/udt/util/UnitHelp.java | 58 ++-- ...eadFactory.java => UtilThreadFactory.java} | 11 +- 25 files changed, 686 insertions(+), 143 deletions(-) rename barchart-udt-netty4/src/main/java/io/netty/example/udt/util/{ConsoleReporterUDT.java => UtilConsoleReporter.java} (92%) rename barchart-udt-netty4/src/main/java/io/netty/example/udt/util/{ThreadFactoryUDT.java => UtilThreadFactory.java} (83%) create mode 100644 barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/BenchXfer.java create mode 100644 barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/TcpNative.java create mode 100644 barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/UdtNative.java rename barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/{BenchNetty.java => UdtNetty.java} (91%) rename barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/{CaliperHelp.java => CaliperRunner.java} (76%) rename barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/{UdtThreadFactory.java => UtilThreadFactory.java} (80%) diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoClient.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoClient.java index 1ddaacf..2f6a27b 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoClient.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoClient.java @@ -19,8 +19,8 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.nio.NioEventLoopGroup; -import io.netty.example.udt.util.ConsoleReporterUDT; -import io.netty.example.udt.util.ThreadFactoryUDT; +import io.netty.example.udt.util.UtilConsoleReporter; +import io.netty.example.udt.util.UtilThreadFactory; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.transport.udt.UdtChannel; @@ -60,7 +60,7 @@ public ByteEchoClient(final String host, final int port, public void run() throws Exception { // Configure the client. final Bootstrap boot = new Bootstrap(); - final ThreadFactory connectFactory = new ThreadFactoryUDT("connect"); + final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); try { @@ -91,7 +91,7 @@ public static void main(final String[] args) throws Exception { log.info("init"); // client is reporting metrics - ConsoleReporterUDT.enable(3, TimeUnit.SECONDS); + UtilConsoleReporter.enable(3, TimeUnit.SECONDS); final String host = "localhost"; final int port = 1234; diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoServer.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoServer.java index 2f920ab..a20f045 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoServer.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/bytes/ByteEchoServer.java @@ -20,7 +20,7 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.socket.nio.NioEventLoopGroup; -import io.netty.example.udt.util.ThreadFactoryUDT; +import io.netty.example.udt.util.UtilThreadFactory; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.transport.udt.UdtChannel; @@ -48,8 +48,8 @@ public ByteEchoServer(final int port) { } public void run() throws Exception { - final ThreadFactory acceptFactory = new ThreadFactoryUDT("accept"); - final ThreadFactory connectFactory = new ThreadFactoryUDT("connect"); + final ThreadFactory acceptFactory = new UtilThreadFactory("accept"); + final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoClient.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoClient.java index e522fee..d7c80ef 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoClient.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoClient.java @@ -19,8 +19,8 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.nio.NioEventLoopGroup; -import io.netty.example.udt.util.ConsoleReporterUDT; -import io.netty.example.udt.util.ThreadFactoryUDT; +import io.netty.example.udt.util.UtilConsoleReporter; +import io.netty.example.udt.util.UtilThreadFactory; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.transport.udt.UdtChannel; @@ -60,7 +60,7 @@ public MsgEchoClient(final String host, final int port, public void run() throws Exception { // Configure the client. final Bootstrap boot = new Bootstrap(); - final ThreadFactory connectFactory = new ThreadFactoryUDT("connect"); + final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); try { @@ -91,7 +91,7 @@ public static void main(final String[] args) throws Exception { log.info("init"); // client is reporting metrics - ConsoleReporterUDT.enable(3, TimeUnit.SECONDS); + UtilConsoleReporter.enable(3, TimeUnit.SECONDS); final String host = "localhost"; diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoServer.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoServer.java index 9a86a53..38ae00c 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoServer.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/message/MsgEchoServer.java @@ -20,7 +20,7 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.socket.nio.NioEventLoopGroup; -import io.netty.example.udt.util.ThreadFactoryUDT; +import io.netty.example.udt.util.UtilThreadFactory; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.transport.udt.UdtChannel; @@ -48,8 +48,8 @@ public MsgEchoServer(final int port) { } public void run() throws Exception { - final ThreadFactory acceptFactory = new ThreadFactoryUDT("accept"); - final ThreadFactory connectFactory = new ThreadFactoryUDT("connect"); + final ThreadFactory acceptFactory = new UtilThreadFactory("accept"); + final ThreadFactory connectFactory = new UtilThreadFactory("connect"); final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerBase.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerBase.java index feb93ba..744a94d 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerBase.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerBase.java @@ -19,7 +19,7 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.nio.NioEventLoopGroup; -import io.netty.example.udt.util.ThreadFactoryUDT; +import io.netty.example.udt.util.UtilThreadFactory; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.transport.udt.UdtChannel; @@ -56,7 +56,7 @@ public MsgEchoPeerBase(final InetSocketAddress self, public void run() throws Exception { // Configure the peer. final Bootstrap boot = new Bootstrap(); - final ThreadFactory connectFactory = new ThreadFactoryUDT("rendezvous"); + final ThreadFactory connectFactory = new UtilThreadFactory("rendezvous"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); try { diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerTwo.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerTwo.java index 522797a..4dfdcde 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerTwo.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerTwo.java @@ -15,7 +15,7 @@ */ package io.netty.example.udt.echo.rendevous; -import io.netty.example.udt.util.ConsoleReporterUDT; +import io.netty.example.udt.util.UtilConsoleReporter; import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; @@ -37,7 +37,7 @@ public static void main(final String[] args) throws Exception { log.info("init"); // peer two is reporting metrics - ConsoleReporterUDT.enable(3, TimeUnit.SECONDS); + UtilConsoleReporter.enable(3, TimeUnit.SECONDS); final int messageSize = 64 * 1024; diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/ConsoleReporterUDT.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/UtilConsoleReporter.java similarity index 92% rename from barchart-udt-netty4/src/main/java/io/netty/example/udt/util/ConsoleReporterUDT.java rename to barchart-udt-netty4/src/main/java/io/netty/example/udt/util/UtilConsoleReporter.java index b59b6aa..6492041 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/ConsoleReporterUDT.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/UtilConsoleReporter.java @@ -44,7 +44,7 @@ * A simple reporters which prints out application metrics to a * {@link PrintStream} periodically. */ -public class ConsoleReporterUDT extends AbstractPollingReporter implements +public class UtilConsoleReporter extends AbstractPollingReporter implements MetricProcessor { private static final int CONSOLE_WIDTH = 80; @@ -62,7 +62,7 @@ public static void enable(final long period, final TimeUnit unit) { */ public static void enable(final MetricsRegistry metricsRegistry, final long period, final TimeUnit unit) { - final ConsoleReporterUDT reporter = new ConsoleReporterUDT( + final UtilConsoleReporter reporter = new UtilConsoleReporter( metricsRegistry, System.out, MetricPredicate.ALL); reporter.start(period, unit); } @@ -74,26 +74,26 @@ public static void enable(final MetricsRegistry metricsRegistry, private final Locale locale; /** - * Creates a new {@link ConsoleReporterUDT} for the default metrics + * Creates a new {@link UtilConsoleReporter} for the default metrics * registry, with unrestricted output. */ - public ConsoleReporterUDT(final PrintStream out) { + public UtilConsoleReporter(final PrintStream out) { this(Metrics.defaultRegistry(), out, MetricPredicate.ALL); } /** - * Creates a new {@link ConsoleReporterUDT} for a given metrics registry. + * Creates a new {@link UtilConsoleReporter} for a given metrics registry. */ - public ConsoleReporterUDT(final MetricsRegistry metricsRegistry, + public UtilConsoleReporter(final MetricsRegistry metricsRegistry, final PrintStream out, final MetricPredicate predicate) { this(metricsRegistry, out, predicate, Clock.defaultClock(), TimeZone .getDefault()); } /** - * Creates a new {@link ConsoleReporterUDT} for a given metrics registry. + * Creates a new {@link UtilConsoleReporter} for a given metrics registry. */ - public ConsoleReporterUDT(final MetricsRegistry metricsRegistry, + public UtilConsoleReporter(final MetricsRegistry metricsRegistry, final PrintStream out, final MetricPredicate predicate, final Clock clock, final TimeZone timeZone) { this(metricsRegistry, out, predicate, clock, timeZone, Locale @@ -101,9 +101,9 @@ public ConsoleReporterUDT(final MetricsRegistry metricsRegistry, } /** - * Creates a new {@link ConsoleReporterUDT} for a given metrics registry. + * Creates a new {@link UtilConsoleReporter} for a given metrics registry. */ - public ConsoleReporterUDT(final MetricsRegistry metricsRegistry, + public UtilConsoleReporter(final MetricsRegistry metricsRegistry, final PrintStream out, final MetricPredicate predicate, final Clock clock, final TimeZone timeZone, final Locale locale) { super(metricsRegistry, "console-reporter"); diff --git a/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/ThreadFactoryUDT.java b/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/UtilThreadFactory.java similarity index 83% rename from barchart-udt-netty4/src/main/java/io/netty/example/udt/util/ThreadFactoryUDT.java rename to barchart-udt-netty4/src/main/java/io/netty/example/udt/util/UtilThreadFactory.java index 27411f7..c5ea0e1 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/ThreadFactoryUDT.java +++ b/barchart-udt-netty4/src/main/java/io/netty/example/udt/util/UtilThreadFactory.java @@ -22,20 +22,19 @@ /** * Custom thread factory to use with examples. */ -public class ThreadFactoryUDT implements ThreadFactory { +public class UtilThreadFactory implements ThreadFactory { private static final AtomicInteger counter = new AtomicInteger(); private final String name; - public ThreadFactoryUDT(final String name) { + public UtilThreadFactory(final String name) { this.name = name; } @Override public Thread newThread(final Runnable runnable) { - return new Thread(runnable, "udt-" + name + "-" - + counter.getAndIncrement()); + return new Thread(runnable, name + "-" + counter.getAndIncrement()); }; } diff --git a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtAcceptorChannel.java b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtAcceptorChannel.java index a44baf3..1f44a45 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtAcceptorChannel.java +++ b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtAcceptorChannel.java @@ -40,7 +40,7 @@ public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel protected static final InternalLogger logger = InternalLoggerFactory .getInstance(NioUdtAcceptorChannel.class); - protected final UdtChannelConfig config; + private final UdtChannelConfig config; protected NioUdtAcceptorChannel(final ServerSocketChannelUDT channelUDT) { super(null, channelUDT.socketUDT().id(), channelUDT, OP_ACCEPT); diff --git a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtByteConnectorChannel.java b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtByteConnectorChannel.java index 75d7edc..437e390 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtByteConnectorChannel.java +++ b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtByteConnectorChannel.java @@ -78,11 +78,11 @@ public NioUdtByteConnectorChannel(final Channel parent, final Integer id, } } - protected NioUdtByteConnectorChannel(final SocketChannelUDT channelUDT) { + public NioUdtByteConnectorChannel(final SocketChannelUDT channelUDT) { this(null, channelUDT.socketUDT().id(), channelUDT); } - protected NioUdtByteConnectorChannel(final TypeUDT type) { + public NioUdtByteConnectorChannel(final TypeUDT type) { this(NioUdtProvider.newConnectorChannelUDT(type)); } diff --git a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageAcceptorChannel.java b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageAcceptorChannel.java index 44c068c..a8f83ea 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageAcceptorChannel.java +++ b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageAcceptorChannel.java @@ -35,7 +35,7 @@ public class NioUdtMessageAcceptorChannel extends NioUdtAcceptorChannel { private static final ChannelMetadata METADATA = new ChannelMetadata( BufType.MESSAGE, false); - protected NioUdtMessageAcceptorChannel() { + public NioUdtMessageAcceptorChannel() { super(TypeUDT.DATAGRAM); } diff --git a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageConnectorChannel.java b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageConnectorChannel.java index 234abf9..bbac0b9 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageConnectorChannel.java +++ b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtMessageConnectorChannel.java @@ -213,10 +213,10 @@ protected int doWriteMessages(final MessageBuf messageQueue, } } - message.free(); - messageQueue.remove(); + message.free(); + return 1; } diff --git a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtProvider.java b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtProvider.java index f60df0a..55b18ea 100644 --- a/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtProvider.java +++ b/barchart-udt-netty4/src/main/java/io/netty/transport/udt/nio/NioUdtProvider.java @@ -196,7 +196,7 @@ private NioUdtProvider(final TypeUDT type, final KindUDT kind) { */ public KindUDT kind() { return kind; - }; + } /** * Produce new {@link UdtChannel} based on factory {@link #kind()} and diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/BenchXfer.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/BenchXfer.java new file mode 100644 index 0000000..a015f35 --- /dev/null +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/BenchXfer.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you 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 io.netty.transport.udt.bench; + +import io.netty.transport.udt.util.CaliperBench; +import io.netty.transport.udt.util.CaliperRunner; +import io.netty.transport.udt.util.TrafficControl; + +import java.util.List; + +/** + * perform two way native udt socket send/recv + */ +public abstract class BenchXfer extends CaliperBench { + + /** introduce network latency */ + protected static List latencyList() { + if (TrafficControl.isAvailable()) { + return CaliperRunner.valueList("0,10,30"); + } else { + return CaliperRunner.valueList("0"); + } + } + + /** verify different message sizes */ + protected static List messageList() { + return CaliperRunner + .valueList("500,1500,3000,5000,10000,20000,50000,100000"); + } + + /** benchmark run time per each configuration */ + protected static List durationList() { + return CaliperRunner.valueList("30000"); + } + +} diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/TcpNative.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/TcpNative.java new file mode 100644 index 0000000..0885eed --- /dev/null +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/TcpNative.java @@ -0,0 +1,66 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you 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 io.netty.transport.udt.bench.xfer; + +import io.netty.transport.udt.bench.BenchXfer; +import io.netty.transport.udt.util.CaliperRunner; +import io.netty.transport.udt.util.TrafficControl; + +import java.util.List; + +import com.google.caliper.Param; + +/** + * perform two way native TCP socket send/recv + */ +public class TcpNative extends BenchXfer { + + @Param + private volatile int latency; + + protected static List latencyValues() { + return BenchXfer.latencyList(); + } + + @Param + private volatile int message; + + protected static List messageValues() { + return BenchXfer.messageList(); + } + + @Param + private volatile int duration; + + protected static List durationValues() { + return BenchXfer.durationList(); + } + + public void timeRun(final int reps) throws Exception { + log.info("init"); + + TrafficControl.delay(latency); + + TrafficControl.delay(0); + + log.info("done"); + } + + public static void main(final String[] args) throws Exception { + CaliperRunner.execute(TcpNative.class); + } +} diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/UdtNative.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/UdtNative.java new file mode 100644 index 0000000..b1a0466 --- /dev/null +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/UdtNative.java @@ -0,0 +1,270 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you 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 io.netty.transport.udt.bench.xfer; + +import static io.netty.transport.udt.util.UnitHelp.*; +import io.netty.transport.udt.bench.BenchXfer; +import io.netty.transport.udt.util.CaliperRunner; +import io.netty.transport.udt.util.TrafficControl; + +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.barchart.udt.SocketUDT; +import com.barchart.udt.StatusUDT; +import com.barchart.udt.TypeUDT; +import com.google.caliper.Param; + +/** + * perform two way native UDT socket send/recv + */ +public class UdtNative extends BenchXfer { + + @Param + private volatile int latency; + + protected static List latencyValues() { + return BenchXfer.latencyList(); + } + + @Param + private volatile int message; + + protected static List messageValues() { + return BenchXfer.messageList(); + } + + @Param + private volatile int duration; + + protected static List durationValues() { + return BenchXfer.durationList(); + } + + private volatile SocketUDT peer1; + private volatile SocketUDT peer2; + + @Override + protected void setUp() throws Exception { + log.info("init"); + + TrafficControl.delay(latency); + + final InetSocketAddress addr1 = localSocketAddress(); + final InetSocketAddress addr2 = localSocketAddress(); + + peer1 = new SocketUDT(TypeUDT.DATAGRAM); + peer2 = new SocketUDT(TypeUDT.DATAGRAM); + + peer1.setBlocking(false); + peer2.setBlocking(false); + + peer1.setRendezvous(true); + peer2.setRendezvous(true); + + peer1.bind(addr1); + peer2.bind(addr2); + + socketAwait(peer1, StatusUDT.OPENED); + socketAwait(peer2, StatusUDT.OPENED); + + peer1.connect(addr2); + peer2.connect(addr1); + + socketAwait(peer1, StatusUDT.CONNECTED); + socketAwait(peer2, StatusUDT.CONNECTED); + + peer1.setBlocking(true); + peer2.setBlocking(true); + + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + + peer1.setBlocking(false); + peer2.setBlocking(false); + + peer1.close(); + peer2.close(); + + socketAwait(peer1, StatusUDT.CLOSED, StatusUDT.BROKEN); + socketAwait(peer2, StatusUDT.CLOSED, StatusUDT.BROKEN); + + TrafficControl.delay(0); + + log.info("done"); + } + + /** benchmark invocation */ + public void timeMain(final int reps) throws Exception { + + final int threadCount = 4; + + final CountDownLatch completion = new CountDownLatch(threadCount); + + final AtomicBoolean isOn = new AtomicBoolean(true); + + final Runnable sendPeer1 = new Runnable() { + + @Override + public void run() { + try { + while (isOn.get()) { + runCore(); + } + } catch (final Exception e) { + log.error("", e); + } finally { + completion.countDown(); + } + } + + final ByteBuffer buffer = ByteBuffer.allocateDirect(message); + + long sequence; + + void runCore() throws Exception { + buffer.rewind(); + buffer.putLong(0, sequence++); + final int count = peer1.send(buffer); + if (count != message) { + throw new Exception("count"); + } + measure().rate().mark(count); + } + }; + + final Runnable sendPeer2 = new Runnable() { + + @Override + public void run() { + try { + while (isOn.get()) { + runCore(); + } + } catch (final Exception e) { + log.error("", e); + } finally { + completion.countDown(); + } + } + + final ByteBuffer buffer = ByteBuffer.allocateDirect(message); + + long sequence; + + void runCore() throws Exception { + buffer.rewind(); + buffer.putLong(0, sequence++); + final int count = peer2.send(buffer); + if (count != message) { + throw new Exception("count"); + } + } + }; + + final Runnable recvPeer1 = new Runnable() { + + @Override + public void run() { + try { + while (isOn.get()) { + runCore(); + } + } catch (final Exception e) { + log.error("", e); + } finally { + completion.countDown(); + } + } + + final ByteBuffer buffer = ByteBuffer.allocateDirect(message); + + long sequence; + + void runCore() throws Exception { + buffer.rewind(); + final int count = peer1.receive(buffer); + if (count != message) { + throw new Exception("count"); + } + if (this.sequence++ != buffer.getLong(0)) { + throw new Exception("sequence"); + } + } + }; + + final Runnable recvPeer2 = new Runnable() { + + @Override + public void run() { + try { + while (isOn.get()) { + runCore(); + } + } catch (final Exception e) { + log.error("", e); + } finally { + completion.countDown(); + } + } + + final ByteBuffer buffer = ByteBuffer.allocateDirect(message); + + long sequence; + + void runCore() throws Exception { + buffer.rewind(); + final int count = peer2.receive(buffer); + if (count != message) { + throw new Exception("count"); + } + if (this.sequence++ != buffer.getLong(0)) { + throw new Exception("sequence"); + } + } + }; + + final ExecutorService executor = Executors + .newFixedThreadPool(threadCount); + + executor.submit(recvPeer1); + executor.submit(recvPeer2); + executor.submit(sendPeer1); + executor.submit(sendPeer2); + + markWait(duration); + + isOn.set(false); + + completion.await(); + + executor.shutdownNow(); + } + + public static void main(final String[] args) throws Exception { + CaliperRunner.execute(UdtNative.class); + } +} diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/BenchNetty.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/UdtNetty.java similarity index 91% rename from barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/BenchNetty.java rename to barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/UdtNetty.java index c5c5bc8..8323ce2 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/BenchNetty.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/bench/xfer/UdtNetty.java @@ -41,12 +41,12 @@ /** * perform two way netty send/recv */ -public final class BenchNetty { +public final class UdtNetty { - private BenchNetty() { + private UdtNetty() { } - static final Logger log = LoggerFactory.getLogger(BenchNetty.class); + static final Logger log = LoggerFactory.getLogger(UdtNetty.class); /** * use slf4j provider for io.netty.logging.InternalLogger @@ -64,10 +64,10 @@ private BenchNetty() { /** transfer chunk size */ static final int size = 64 * 1024; - static final Counter benchTime = Metrics.newCounter(BenchNetty.class, + static final Counter benchTime = Metrics.newCounter(UdtNetty.class, "bench time"); - static final Counter benchSize = Metrics.newCounter(BenchNetty.class, + static final Counter benchSize = Metrics.newCounter(UdtNetty.class, "bench size"); static { @@ -75,7 +75,7 @@ private BenchNetty() { benchSize.inc(size); } - static final Meter rate = Metrics.newMeter(BenchNetty.class, "rate", + static final Meter rate = Metrics.newMeter(UdtNetty.class, "rate", "bytes", TimeUnit.SECONDS); static { diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/BootHelp.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/BootHelp.java index d238c13..ac8d1fe 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/BootHelp.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/BootHelp.java @@ -25,7 +25,7 @@ import java.util.concurrent.ThreadFactory; /** - * bootstrap utils + * Bootstrap utilities. */ public class BootHelp { @@ -37,7 +37,7 @@ public static Bootstrap bytePeerBoot(final InetSocketAddress self, final Bootstrap boot = new Bootstrap(); - final ThreadFactory connectFactory = new UdtThreadFactory("peer"); + final ThreadFactory connectFactory = new UtilThreadFactory("peer"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER); @@ -57,7 +57,7 @@ public static Bootstrap messagePeerBoot(final InetSocketAddress self, final Bootstrap boot = new Bootstrap(); - final ThreadFactory connectFactory = new UdtThreadFactory("peer"); + final ThreadFactory connectFactory = new UtilThreadFactory("peer"); final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER); diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperBench.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperBench.java index 5764f88..01f52cf 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperBench.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperBench.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you 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 io.netty.transport.udt.util; import io.netty.logging.InternalLoggerFactory; @@ -8,8 +24,14 @@ import com.google.caliper.SimpleBenchmark; -public class CaliperBench extends SimpleBenchmark { +/** + * Base class for caliper/metrics benchmarks. + */ +public abstract class CaliperBench extends SimpleBenchmark { + /** + * Ensure no network latency after JVM shutdown + */ static { Runtime.getRuntime().addShutdownHook(new Thread() { @Override @@ -23,6 +45,9 @@ public void run() { }); } + /** + * Use slf4j logging. + */ static { final InternalLoggerFactory defaultFactory = new Slf4JLoggerFactory(); InternalLoggerFactory.setDefaultFactory(defaultFactory); @@ -30,29 +55,55 @@ public void run() { protected final Logger log = LoggerFactory.getLogger(getClass()); - private final CaliperMeasure measure = new CaliperMeasure(); + private volatile CaliperMeasure measure; + /** + * Caliper metrics wrapper. + */ protected CaliperMeasure measure() { - // CaliperMeasure measure = this.measure; - // if (measure == null) { - // measure = this.measure = new CaliperMeasure(); - // } return measure; } + /** + * Start measurement. + */ + @Override + protected void setUp() throws Exception { + measure = new CaliperMeasure(); + } + + /** + * Finish measurement. + */ + @Override + protected void tearDown() throws Exception { + measure.shutdown(); + } + + /** + * Measure time step and minimum run time. + */ + protected long markStep() { + return 3 * 1000; + } + + /** + * Measure progress while in sleep. + */ protected void markWait(final long time) throws Exception { final long timeStart = System.currentTimeMillis(); while (true) { + Thread.sleep(markStep()); + measure().mark(); final long timeFinish = System.currentTimeMillis(); - final long timeDiff = timeFinish - timeStart; - if (timeDiff >= time) { - break; + if (timeFinish - timeStart >= time) { + System.out.print("+\n"); + return; } else { - log.info("mark"); - measure().mark(); - Thread.sleep(3 * 1000); + System.out.print("-"); + continue; } } diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperMeasure.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperMeasure.java index 2d469e9..2d43c48 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperMeasure.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperMeasure.java @@ -34,13 +34,18 @@ import com.yammer.metrics.core.Timer; /** - * marry metrics with caliper + * Caliper measure with Metrics provider. + *

+ * measure up to 3 values: {@link #rate()}, {@link #time()}, {@link #size()} */ public class CaliperMeasure { private final static Logger log = LoggerFactory .getLogger(CaliperMeasure.class); + /** + * Gauge any double value + */ public static class SizeGuage extends Gauge { private volatile Double size = 0.0; @@ -55,15 +60,21 @@ public void value(final double number) { } } - static final Map RATE_UNIT = new HashMap(); + /** + * Default rate measurement units. + */ + private static final Map RATE_UNIT = new HashMap(); static { - RATE_UNIT.put("Rate B/s", 1); - RATE_UNIT.put("Rate K/s", 1000); - RATE_UNIT.put("Rate M/s", 1000 * 1000); - RATE_UNIT.put("Rate G/s", 1000 * 1000 * 1000); + RATE_UNIT.put("Rate B/s", 1); + RATE_UNIT.put("Rate KB/s", 1024); + RATE_UNIT.put("Rate MB/s", 1024 * 1024); + RATE_UNIT.put("Rate GB/s", 1024 * 1024 * 1024); } - static final Map TIME_UNIT = new HashMap(); + /** + * Default time measurement units. + */ + private static final Map TIME_UNIT = new HashMap(); static { TIME_UNIT.put("Time ns", 1); TIME_UNIT.put("Time us", 1000); @@ -71,12 +82,15 @@ public void value(final double number) { TIME_UNIT.put("Time s ", 1000 * 1000 * 1000); } - static final Map SIZE_UNIT = new HashMap(); + /** + * Default size measurement units. + */ + private static final Map SIZE_UNIT = new HashMap(); static { - SIZE_UNIT.put("Size B", 1); - SIZE_UNIT.put("Size K", 1000); - SIZE_UNIT.put("Size M", 1000 * 1000); - SIZE_UNIT.put("Size B", 1000 * 1000 * 1000); + SIZE_UNIT.put("Size B", 1); + SIZE_UNIT.put("Size KB", 1024); + SIZE_UNIT.put("Size MB", 1024 * 1024); + SIZE_UNIT.put("Size GB", 1024 * 1024 * 1024); } private final Map rateMap = new HashMap(); @@ -96,21 +110,30 @@ public void value(final double number) { metrics.newGauge(getClass(), "", size); } - /** generic rate meter */ + /** + * Rate meter. + */ public Meter rate() { return rate; } - /** generic time timer */ + /** + * Time meter. + */ public Timer time() { return time; } - /** generic size guage */ + /** + * Size meter. + */ public SizeGuage size() { return size; } + /** + * Workaround: zero breaks gwt web app. + */ private double filter(final double value) { if (value <= 0.0) { return 1.0; @@ -119,7 +142,9 @@ private double filter(final double value) { } } - /** from metrics into caliper */ + /** + * Perform measurement; convert from metrics into caliper. + */ public void mark() { final double rateValue = filter(rate.oneMinuteRate()); final double timeValue = filter(time.mean()); @@ -147,6 +172,9 @@ public void mark() { private final Map variables = new HashMap(); + /** + * Caliper scenario variables. + */ public Map variables() { return variables; } @@ -157,9 +185,12 @@ private MeasurementSet measurementSet(final Map map) { return set; } + /** + * Attach this measure to parent caliper run. + */ public void appendTo(final Run run) { - final Scenario scenario = new Scenario(variables); + final Scenario scenario = new Scenario(variables()); /** display rate as caliper durations */ final MeasurementSet timeSet = measurementSet(rateMap); @@ -180,6 +211,14 @@ public void appendTo(final Run run) { .getMeasurements(); measurements.put(scenario, scenarioResult); + } + /** + * Terminate metrics resources. + */ + public void shutdown() { + rate.stop(); + time.stop(); + metrics.shutdown(); } } diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperHelp.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperRunner.java similarity index 76% rename from barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperHelp.java rename to barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperRunner.java index 5aa023a..e74e538 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperHelp.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/CaliperRunner.java @@ -19,6 +19,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Date; @@ -45,48 +46,82 @@ import com.google.caliper.SimpleBenchmark; import com.yammer.metrics.core.TimerContext; -public final class CaliperHelp { +/** + * Custom caliper runner for {@link CaliperBench}. + */ +public final class CaliperRunner { private final static Logger log = LoggerFactory - .getLogger(CaliperHelp.class); + .getLogger(CaliperRunner.class); + + private CaliperRunner() { + } - private CaliperHelp() { + /** + * Parse bench parameters. + */ + public static List valueList(final String valueText) { + return Arrays.asList(valueText.split(",")); } + /** + * Execute full cycle: warm up, execute and publish benchmark. + */ public static void execute(final Class klaz) throws Exception { + Run run; + run = execute("WARMUP", klaz); + run = execute("REPORT", klaz); + publish(newResult(run)); + } - final CaliperBench booter = klaz.newInstance(); + /** + * Execute benchmark for all parameter combinations. + */ + public static Run execute(final String name, + final Class klaz) throws Exception { - final List> paramSet = product(booter); + final CaliperBench booter = klaz.newInstance(); - for (final Map param : paramSet) { - log.info("warming : {}", param); - final ConfiguredBenchmark runner = booter.createBenchmark(param); - runner.run(0); - } + final List> varsSet = product(booter); final Run run = newRun(klaz.getName()); - for (final Map param : paramSet) { - log.info("working : {}", param); - final ConfiguredBenchmark runner = booter.createBenchmark(param); - runner.run(0); + int index = 0; + for (final Map vars : varsSet) { + final int done = 100 * index++ / varsSet.size(); + + log.info("{} {}% {}", name, done, vars); + + /** call setUp() */ + final ConfiguredBenchmark runner = booter.createBenchmark(vars); + final CaliperBench bench = (CaliperBench) runner.getBenchmark(); - bench.measure().variables().putAll(param); - bench.measure().mark(); - bench.measure().appendTo(run); + final CaliperMeasure measure = bench.measure(); + measure.variables().putAll(vars); + + /** call timeXXX() */ + runner.run(0); + + /** call tearDown() */ + runner.close(); + + measure.appendTo(run); } - final Result result = newResult(run); - publish(result); - log.info("\n{}", json(result)); + return run; } + /** + * Convert caliper result into JSON string. + */ public static String json(final Result result) { return Json.getGsonInstance().toJson(result); } + /** + * Map signature based on map values. + */ public static String signature(final Map map) { final StringBuilder text = new StringBuilder(); for (final String item : map.values()) { @@ -95,6 +130,9 @@ public static String signature(final Map map) { return text.toString(); } + /** + * Generate all parameter combinations for {@link SimpleBenchmark}. + */ public static List> product(final SimpleBenchmark bench) { final Set> collect = new HashSet>(); final Map> pending = new TreeMap>(); @@ -114,6 +152,9 @@ public int compare(final Map o1, return list; } + /** + * Calculate ordered Cartesian product of sets. + */ public static Set> product( final Set> collect, final Map> pending) { @@ -147,6 +188,9 @@ public static Set> product( } } + /** + * Publish result on http://microbenchmarks.appspot.com + */ public static void publish(final Result result) throws Exception { final Runner runner = new Runner(); final Method method = runner.getClass().getDeclaredMethod( @@ -155,6 +199,9 @@ public static void publish(final Result result) throws Exception { method.invoke(runner, result); } + /** + * Provide new named run instance. + */ public static Run newRun(final String benchmarkName) { final Map measurements = new HashMap(); final Date executedTimestamp = new Date(); @@ -162,6 +209,9 @@ public static Run newRun(final String benchmarkName) { return run; } + /** + * Make new result from run. + */ public static Result newResult(final Run run) { final Environment env = new EnvironmentGetter() .getEnvironmentSnapshot(); @@ -169,40 +219,27 @@ public static Result newResult(final Run run) { return result; } - /** basic measure tester */ + /** + * Verify measure publication manually. + */ public static void main(final String[] args) throws Exception { - - final Run run = newRun("test-1111"); - + final Run run = newRun("test-main"); for (int param = 0; param < 5; param++) { - final CaliperMeasure measure = new CaliperMeasure(); - measure.variables().put("param", "" + param); - for (int step = 0; step < 5; step++) { - measure.rate().mark(50 + step); - final TimerContext time = measure.time().time(); Thread.sleep(15); time.stop(); - measure.size().value(50 + step); - measure.mark(); } - measure.appendTo(run); - } - final Result result = newResult(run); - publish(result); - System.out.println(json(result)); - } } diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/EchoMessageHandler.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/EchoMessageHandler.java index a6f4847..5bffc80 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/EchoMessageHandler.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/EchoMessageHandler.java @@ -49,6 +49,8 @@ public Meter meter() { public EchoMessageHandler(final Meter meter, final int messageSize) { + super(UdtMessage.class); + this.meter = meter; final ByteBuf byteBuf = Unpooled.buffer(messageSize); diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/TrafficControl.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/TrafficControl.java index cc466fb..5f73c8b 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/TrafficControl.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/TrafficControl.java @@ -16,8 +16,11 @@ package io.netty.transport.udt.util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** - * Introduce traffic control, such as transfer delays. + * Introduce traffic control, such as transfer latency. *

* requires sudo setup for /sbin/tc under current account *

@@ -25,6 +28,9 @@ */ public final class TrafficControl { + private static final Logger log = LoggerFactory + .getLogger(TrafficControl.class.getName()); + private TrafficControl() { } @@ -47,24 +53,24 @@ public static boolean isAvailable() { return time2 >= time1 + millis - margin && time2 >= time3 + millis - margin; } catch (final Throwable e) { - e.printStackTrace(); + log.debug("", e); return false; } } /** - * introduce round-trip delay on local host - * @param time - delay in milliseconds + * Introduce round-trip delay on local host + * @param time - delay in milliseconds; use zero to remove delay. */ public static void delay(final int time) throws Exception { if (time < 0) { - throw new IllegalArgumentException("negative delay"); + throw new IllegalArgumentException("negative latency"); } final int delay = time / 2; if (delay == 0) { UnitHelp.process(String.format(TC_RESET, "lo")); } else { - /** extend packet buffer queue to avoid packet loss due to delay */ + /** extend packet buffer queue to avoid packet loss due to latency */ final int limit = 1024 * 1024; UnitHelp.process(String.format(TC_RESET, "lo")); UnitHelp.process(String.format(TC_DELAY, "lo", delay, limit)); diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UnitHelp.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UnitHelp.java index d3ab8d5..b373457 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UnitHelp.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UnitHelp.java @@ -25,8 +25,6 @@ import java.util.Random; import java.util.Set; import java.util.TreeSet; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,15 +33,15 @@ import com.barchart.udt.StatusUDT; /** - * unit test helper + * Unit test helper. */ public final class UnitHelp { private static final Logger log = LoggerFactory.getLogger(UnitHelp.class); - private static final ConcurrentMap // - socketMap = new ConcurrentHashMap(); - + /** + * Zero out buffer. + */ public static void clear(final IntBuffer buffer) { for (int index = 0; index < buffer.capacity(); index++) { buffer.put(index, 0); @@ -51,7 +49,7 @@ public static void clear(final IntBuffer buffer) { } /** - * measure ping time to host + * Measure ping time to a host. */ public static long ping(final String host) throws Exception { final String name = System.getProperty("os.name").toLowerCase(); @@ -78,8 +76,11 @@ public static long ping(final String host) throws Exception { return timeDiff; } + /** + * Invoke external process and wait for completion. + */ public static void process(final String command) throws Exception { - final ProcessBuilder builder = new ProcessBuilder(command.split("\\s")); + final ProcessBuilder builder = new ProcessBuilder(command.split("\\s+")); final Process process = builder.start(); process.waitFor(); } @@ -95,19 +96,22 @@ public static synchronized InetSocketAddress findLocalAddress( socket = new ServerSocket(0, 3, address); return (InetSocketAddress) socket.getLocalSocketAddress(); } catch (final Exception e) { - log.error("failed to find addess"); + log.error("Failed to find addess."); return null; } finally { if (socket != null) { try { socket.close(); } catch (final Exception e) { - log.error("failed to close socket"); + log.error("Failed to close socket."); } } } } + /** + * Find named address on local host. + */ public static InetSocketAddress hostedSocketAddress(final String host) throws Exception { for (int k = 0; k < 10; k++) { @@ -118,16 +122,19 @@ public static InetSocketAddress hostedSocketAddress(final String host) } return address; } - throw new Exception("failed to allocate address"); + throw new Exception("Failed to allocate address."); } /** - * allocate available local address / port or throw exception + * Allocate available local address / port or throw exception. */ public static InetSocketAddress localSocketAddress() throws Exception { return hostedSocketAddress("localhost"); } + /** + * Display contents of a buffer. + */ public static void logBuffer(final String title, final IntBuffer buffer) { for (int index = 0; index < buffer.capacity(); index++) { final int value = buffer.get(index); @@ -138,6 +145,9 @@ public static void logBuffer(final String title, final IntBuffer buffer) { } } + /** + * Display java.class.path + */ public static void logClassPath() { final String classPath = System.getProperty("java.class.path"); final String[] entries = classPath.split(File.pathSeparator); @@ -149,6 +159,9 @@ public static void logClassPath() { log.info("\n\t[java.class.path]{}", text); } + /** + * Display java.library.path + */ public static void logLibraryPath() { final String classPath = System.getProperty("java.library.path"); final String[] entries = classPath.split(File.pathSeparator); @@ -160,6 +173,9 @@ public static void logLibraryPath() { log.info("\n\t[java.library.path]{}", text); } + /** + * Display current OS/ARCH. + */ public static void logOsArch() { final StringBuilder text = new StringBuilder(1024); text.append("\n\t"); @@ -169,6 +185,9 @@ public static void logOsArch() { log.info("\n\t[os/arch]{}", text); } + /** + * Display contents of a set. + */ public static void logSet(final Set set) { @SuppressWarnings({ "rawtypes", "unchecked" }) final TreeSet treeSet = new TreeSet(set); @@ -203,13 +222,18 @@ public static String randomSuffix(final String name) { return name + "-" + System.currentTimeMillis(); } + /** + * Block till socket reaches given state. + */ public static void socketAwait(final SocketUDT socket, - final StatusUDT status) throws Exception { + final StatusUDT... statusArray) throws Exception { while (true) { - if (socket.status() == status) { - return; - } else { - Thread.sleep(50); + for (final StatusUDT status : statusArray) { + if (socket.status() == status) { + return; + } else { + Thread.sleep(50); + } } } } diff --git a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UdtThreadFactory.java b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UtilThreadFactory.java similarity index 80% rename from barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UdtThreadFactory.java rename to barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UtilThreadFactory.java index a6e48dc..87f05fb 100644 --- a/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UdtThreadFactory.java +++ b/barchart-udt-netty4/src/test/java/io/netty/transport/udt/util/UtilThreadFactory.java @@ -20,22 +20,21 @@ import java.util.concurrent.atomic.AtomicInteger; /** - * testing thread factory + * Thread factory for tests. */ -public class UdtThreadFactory implements ThreadFactory { +public class UtilThreadFactory implements ThreadFactory { private static final AtomicInteger counter = new AtomicInteger(); private final String name; - public UdtThreadFactory(final String name) { + public UtilThreadFactory(final String name) { this.name = name; } @Override public Thread newThread(final Runnable runnable) { - return new Thread(runnable, "udt-" + name + "-" - + counter.getAndIncrement()); - }; + return new Thread(runnable, name + "-" + counter.getAndIncrement()); + } }