pub / sub上的ZeroMQ + Java性能

时间:2014-06-20 13:57:22

标签: java performance zeromq publish-subscribe

没有运气的谷歌搜索,我的问题是标题。对于某些人,我每秒只获得大约50K的消息。 64字节消息,只有一个简单的pub / sub测试发送一个const字符串。无论我是在做TCP://或INPROC://,还是在Windows或Ubuntu下,同一个框都表现出非常稳定的性能。

硬件和软件配置为:

i5-4670 3.4G x 4核心

16GB 1666Mhz RAM

Windows 7 64位

JDK 1.8.05

ZeroMQ 3.2.3

jzmq 2.2.2

示例程序如下所示,我创建了2个线程,1个做pub,1个做sub。发送消息1000次并等待用户全部接收消息。性能始终在30毫秒左右,这只是我假设ZeroMQ应该提供的性能的1/10。该代码演示了在IDE中独立运行并作为JUnit测试运行的相同性能。

有人能给我一些暗示吗?

EDIT1:似乎我将消息数量扩大到5000我从JVM获得了核心转储。这让我想到了真正的问题,虽然我不认为我在线程模型中有任何错误。什么可能是罪魁祸首?

public class ZMQReadynessTest {

private ZMQ.Context context;

@Before
public void setUp() {
    ZMQLoader.initialize();
    context = ZMQ.context(2);
}

@Test
public void testSimpleMessage() {
    final int totalCount = 1000;
    String topic = "tcp://127.0.0.1:31216";
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch latch = new CountDownLatch(totalCount);

    // create a simple subscriber
    final ZMQ.Socket subscribeSocket = context.socket(ZMQ.SUB);
    subscribeSocket.connect(topic);
    subscribeSocket.subscribe("TestTopic".getBytes());

    Thread subThread = new Thread() {

        @Override
        public void run() {
            try {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    fail();
                }

                startLatch.countDown();
                ByteBuffer buffer = ByteBuffer.allocateDirect(100);

                while (latch.getCount() > 0) {

                    // get the message
                    int count = 0;
                    if ((count = subscribeSocket.recvZeroCopy(buffer, buffer.remaining(), 0)) > 0) {

                        // another receive for content
                        count = subscribeSocket.recvZeroCopy(buffer, buffer.remaining(), 0);
                        buffer.flip();

                        byte[] b = new byte[count];
                        buffer.get(b);
                        assertEquals("This is test string", new String(b));

                        latch.countDown();
                        buffer.clear();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(latch.getCount());
            } finally {
                subscribeSocket.close();
            }
        }
    };

    // create a simple publisher - wait 3 sec to make sure its ready
    ZMQ.Socket publishSocket = context.socket(ZMQ.PUB);
    publishSocket.bind("tcp://*:31216");

    try {
        subThread.start();
        try {
            startLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail();
        }

        // publish a sample message
        long before = System.currentTimeMillis(), after;
        try {
            for (int i = 0; i < totalCount; i++) {
                publishSocket.send("NotTestTopic".getBytes(), ZMQ.SNDMORE | ZMQ.DONTWAIT);
                publishSocket.send("Not received".getBytes(), 0);
                publishSocket.send("TestTopic".getBytes(), ZMQ.SNDMORE | ZMQ.DONTWAIT);
                publishSocket.send("This is test string".getBytes(), ZMQ.DONTWAIT);
            }

            latch.await(10000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail();
        } finally {
            after = System.currentTimeMillis();
            publishSocket.close();
        }

        assertEquals(latch.getCount(), 0);
        System.out.println(String.valueOf(totalCount) + " messages took " + (after - before) + " ms.");
    } finally {
        publishSocket.close();
        subscribeSocket.close();
    }
}

}

0 个答案:

没有答案