为什么我在接受新频道的 Netty 服务器 bootstrap 上看到性能下降?

Why do I see performance drops on Netty server bootstrap accepting new channels?

我还在用Netty 3.10。我写了一个单元测试来检查 Netty 老板线程的性能。我在单元测试主线程中使用一个简单的 Netty 服务器 bootstrap,并在缓存线程池中生成 100 个 Java 同步 IO 客户端。我注意到我认为很奇怪的性能下降。每个客户端打开一个套接字,写入数据并关闭,关闭后记录持续时间(毫秒)。附上我的单元测试。我的单元测试的典型输出是,按给定的顺序:

  1. 43 x 客户端完成。持续时间:0
  2. 26 x 客户端完成。时长:16
  3. 16 x 客户端完成。持续时间:0
  4. 3 x 客户完成。时长:517
  5. 11 x 客户端完成。时长:3003
  6. 1 x 客户完成。时长:6036

因此有 1 个客户端必须等待 6 秒才能获得打开的 TCP/IP 通道,而 11 个客户端必须等待 3 秒。我还检查了时间spent/lost。在客户端总是 new Socket(String,int) 。在服务器端,当管道工厂被触发时,时间已经过去了。

我的单元测试的线程模型是这个原因还是真的 Netty bootstrap/boss?

import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

public class NettyServerBossTest {

    private static final String SRV_HOST = "localhost";
    private static final int SRV_PORT = 8080;
    private static final byte[] MSG = "Hello world!".getBytes(Charset.forName("UTF-8"));
    private static final int WAIT_MAX_MILLIS = 10 * 1000;

    private final ChannelGroup channels = new DefaultChannelGroup();

    private final int expected = 100;
    private final AtomicInteger actual = new AtomicInteger();

    private volatile boolean failed;
    private ExecutorService clientThreads;
    private Throwable cause;
    private ServerBootstrap bootstrap;

    @Test
    public void test() {
        createServer();
        createClients();
        awaitClients();
        verifyFailure();
    }

    private void awaitClients() {
        final long startMillis = System.currentTimeMillis();
        final long maxMillis = startMillis + WAIT_MAX_MILLIS;
        while ((this.actual.get() < this.expected) && !isFailed() && (System.currentTimeMillis() < maxMillis)) {
            try {
                Thread.sleep(250L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Total duration: " + (System.currentTimeMillis() - startMillis));
        Assert.assertEquals(this.expected, this.actual.get());
    }

    private void createClients() {
        this.clientThreads = Executors.newCachedThreadPool();
        for (int i = 0; i < this.expected; i++) {
            this.clientThreads.execute(new PlainSocketClient());
        }
    }

    private void closeChannels() {
        try {
            this.channels.close().await(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void createServer() {
        final ExecutorService bosses = Executors.newCachedThreadPool();
        final ExecutorService workers = Executors.newCachedThreadPool();
        final ChannelFactory factory = new NioServerSocketChannelFactory(bosses, workers);
        this.bootstrap = new ServerBootstrap(factory);
        this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() {
                return Channels.pipeline(new DiscardServerHandler());
            }
        });
        this.bootstrap.setOption("child.tcpNoDelay", Boolean.TRUE);
        this.bootstrap.setOption("child.keepAlive", Boolean.TRUE);
        this.bootstrap.bind(new InetSocketAddress(SRV_HOST, SRV_PORT));
    }

    /**
     * Fail unit test
     *
     * @param cause
     *            cause of failure
     */
    public synchronized void setCause(Throwable cause) {
        if (!this.failed && (cause == null)) {
            this.failed = true;
            this.cause = cause;
        }
    }

    @After
    public void after() {
        closeChannels();
        if (this.clientThreads != null) {
            this.clientThreads.shutdownNow();
        }
        if (this.bootstrap != null) {
            this.bootstrap.releaseExternalResources();
        }
    }

    /**
     * Check if unit test has failed
     *
     * @return <code>true</code> if failed, <code>false</code> if still OK
     */
    public boolean isFailed() {
        return this.failed;
    }

    /**
     * Get cause of failure
     *
     * @return cause or <code>null</code>
     */
    public synchronized Throwable getCause() {
        return this.cause;
    }

    /**
     * Make sure test has not failed with exception
     */
    public void verifyFailure() {
        if (this.failed) {
            throw new IllegalStateException("test failed", getCause());
        }
    }

public abstract class TestRunnable implements Runnable {

    @Override
    public final void run() {
        try {
            execute();
        } catch (Exception e) {
            handleException(e);
        }
    }

    protected abstract void handleException(Throwable e);

    protected abstract void execute() throws Exception;

}

public abstract class AsyncThreadsTestRunnable extends TestRunnable {

    @Override
    protected final void handleException(Throwable e) {
        setCause(e);
    }

}

public class PlainSocketClient extends AsyncThreadsTestRunnable {

    @Override
    protected void execute() throws Exception {
        final long startMillis = System.currentTimeMillis();
        try (Socket sock = new Socket(SRV_HOST, SRV_PORT)) {
            sock.getOutputStream().write(MSG);
        }
        NettyServerBossTest.this.actual.incrementAndGet();
        System.out.println("Client done. Duration: " + (System.currentTimeMillis() - startMillis));
    }

}

public class DiscardServerHandler extends SimpleChannelHandler {

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
        NettyServerBossTest.this.channels.add(e.getChannel());
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        // discard
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        e.getCause().printStackTrace();

        Channel ch = e.getChannel();
        ch.close();
    }
}

}

我认为你登录的时间并不都是用在打开socket上,它是在线程切换上花费的,因为当线程A打开一个socket然后CPU可能会切换到Thread B,然后当socket打开完成时, CPU 可能不会立即切换到线程 A,而是在执行了许多其他线程之后。我已将您的 PlainSocketClient 更改为添加同步以确保线程切换影响较小:

public class PlainSocketClient extends AsyncThreadsTestRunnable {
    private static final String LOCK = "LOCK";

    @Override
    protected void execute() throws Exception {
        synchronized (LOCK) {
            final long startMillis = System.currentTimeMillis();
            try (Socket sock = new Socket(SRV_HOST, SRV_PORT)) {
                sock.getOutputStream().write(MSG);
            }
            NettyServerBossTest.this.actual.incrementAndGet();
            System.out.println("Client done. Duration: " + (System.currentTimeMillis() - startMillis));
        }
    }
}

然后他们几乎只输出0或1。你可以自己测试一下。 它只是证明线程切换的耗时,并不意味着你需要在代码中添加同步。