java – 为什么我看到接受新频道的Netty服务器引导程序的性能下降?

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

> 43 x客户完成.持续时间:0
> 26 x客户完成.持续时间:16
> 16 x客户完成.持续时间:0
> 3 x客户完成.持续时间:517
> 11 x客户完成.持续时间:3003
> 1 x客户完成.持续时间:6036

因此,有1个客户端必须等待6秒才能获得开放的TCP / IP通道,11个客户端必须等待3秒钟.我还检查了花费/丢失的时间.它始终是客户端的新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();
    }
}

}

最佳答案 我认为你记录的时间不是全部花在开放套接字上,它花费在线程切换上,因为当线程A打开套接字然后CPU可能切换到线程B,然后当套接字打开时,CPU可能不会切换到线程A一次但在执行了许多其他线程之后.我已经更改你的PlainSocketClient来添加一个synchronized来确保减少线程切换的影响:

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.您可以自己进行测试.
它只是证明了线程切换的耗时,并不意味着您需要在代码中添加synchronized.

点赞