ApachePulsar服务性能测试
创始人
2024-09-06 09:30:38
0

为测试Apache Pulsar服务的性能,我们可以使用如下代码示例:

  1. 使用Pulsar生产者:
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.Schema;
import java.util.concurrent.TimeUnit;

public class ProducerTest {
    private static final String SERVICE_URL = "pulsar://localhost:6650";
    private static final String TOPIC_NAME = "test-topic";

    public static void main(String[] args) throws PulsarClientException, InterruptedException {
        PulsarClient client = PulsarClient.builder()
                .serviceUrl(SERVICE_URL)
                .build();
        Producer producer = client.newProducer(Schema.STRING)
                .topic(TOPIC_NAME)
                .create();

        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            String message = "Test message " + i;
            producer.send(message);
        }
        long end = System.currentTimeMillis();

        System.out.println("Total time: " + TimeUnit.MILLISECONDS.toSeconds(end - start) + "s");

        producer.close();
        client.close();
    }
}
  1. 使用Pulsar消费者:
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.PulsarClientException.InvalidConfigurationException;
import org.apache.pulsar.client.api.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.Schema;

public class ConsumerTest {
    private static final String SERVICE_URL = "pulsar://localhost:6650";
    private static final String TOPIC_NAME = "test-topic";
    private static final String SUBSCRIPTION_NAME = "test-subscription";

    public static void main(String[] args) throws InvalidConfigurationException, PulsarClientException {
        PulsarClient client = PulsarClient.builder()
                .serviceUrl(SERVICE_URL)
                .build();
        Consumer consumer = client.newConsumer(Schema.STRING)
                .topic(TOPIC_NAME)
                .subscriptionName(SUBSCRIPTION_NAME)
                .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
                .subscribe();

        while (true) {
             Message message = consumer.receive();
            System.out.println("Received message: " + message.getValue());
            consumer.acknowledge(message);
        }
    }
}

注意:在运行代码时,需要确保Pulsar服务已在本地启动并运行在默认端口上。

相关内容

热门资讯

透视肯定(aapoker辅助工... 透视肯定(aapoker辅助工具)微扑克有辅助吗(详细辅助切实教程)原本存在有挂;小薇(透视辅助)致...
wpk透视辅助工具!wopok... wpk透视辅助工具!wopoker透明挂,(德州app)往昔存在有挂(详细辅助可靠教程);(需添加指...
透视教程(wpk有透视辅助)a... 一、简介了解软件请加微:136704302是一款在线扑克游戏平台,玩家可以在平台上进行多人在线扑克游...
wpk透视辅助工具!德州ai软... wpk透视辅助工具!德州ai软件怎么收费,(wpK)果然是有挂(详细辅助AA德州教程)是一款可以让一...
透视安卓版(德州ai辅助软件)... 透视安卓版(德州ai辅助软件)微扑克有规律吗(详细辅助力荐教程)本来存在有挂1、下载好辅助软件之后点...
wepokeai代打!德州之星... 此外,数据分析德州(德州之星辅助)辅助神器app还具备辅助透视行为开挂功能,通过对客户德州之星辅助透...
透视安装(wepoke有没有挂... 透视安装(wepoke有没有挂)wpkplus有辅助器吗(详细辅助可靠教程)从来真的有挂1、每个玩家...
微扑克辅助软件!轰趴大菠萝十三... 自定义微扑克辅助软件系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你...
透视ai(wpk辅助挂)wpk... 透视ai(wpk辅助挂)wpk微扑克外挂事件(详细辅助2025教程)一直真的是有挂1、下载好辅助软件...
智星德州菠萝辅助器!wpk德州... 智星德州菠萝辅助器!wpk德州有挂吗,(德扑)本来是真的有挂(详细辅助我来教教你)是一款可以让一直输...