使用k6进行kafka负载测试

1.安装环境

kafka环境

参考Docker搭建kafka环境-CSDN博客

xk6-kafka环境

bash 复制代码
./xk6 build --with github.com/mostafa/xk6-kafka@latest

查看安装情况

2.编写脚本

test_kafka.js

javascript 复制代码
// Either import the module object
import * as kafka from "k6/x/kafka";

// Or individual classes and constants
import {
    Writer,
    Reader,
    Connection,
    SchemaRegistry,
    SCHEMA_TYPE_STRING,
} from "k6/x/kafka";

// Creates a new Writer object to produce messages to Kafka
const writer = new Writer({
    // WriterConfig object
    brokers: ["localhost:9092"],
    topic: "my-topic",
});

const reader = new Reader({
    // ReaderConfig object
    brokers: ["localhost:9092"],
    topic: "my-topic",
});

const connection = new Connection({
    // ConnectionConfig object
    address: "localhost:9092",
});

const schemaRegistry = new SchemaRegistry();
// Can accept a SchemaRegistryConfig object

if (__VU == 0) {
    // Create a topic on initialization (before producing messages)
    connection.createTopic({
    // TopicConfig object
    topic: "my-topic",
    });
}

export default function () {
    // Fetch the list of all topics
    const topics = connection.listTopics();
    console.log(topics); // list of topics

    // Produces message to Kafka
    writer.produce({
    // ProduceConfig object
    messages: [
        // Message object(s)
        {
        key: schemaRegistry.serialize({
            data: "my-key",
            schemaType: SCHEMA_TYPE_STRING,
        }),
        value: schemaRegistry.serialize({
            data: "my-value",
            schemaType: SCHEMA_TYPE_STRING,
        }),
        },
    ],
    });

    // Consume messages from Kafka
    let messages = reader.consume({
    // ConsumeConfig object
    limit: 10,
    });

    // your messages
    console.log(messages);

    // You can use checks to verify the contents,
    // length and other properties of the message(s)

    // To serialize the data back into a string, you should use
    // the deserialize method of the Schema Registry client. You
    // can use it inside a check, as shown in the example scripts.
    let deserializedValue = schemaRegistry.deserialize({
    data: messages[0].value,
    schemaType: SCHEMA_TYPE_STRING,
    });
}

export function teardown(data) {
    // Delete the topic
    connection.deleteTopic("my-topic");

    // Close all connections
    writer.close();
    reader.close();
    connection.close();
}

3.运行测试

运作之前先开启kafka服务,打开终端输入命令

bash 复制代码
./k6 run test_kafka.js --vus 50 --duration 10s

测试结果

相关推荐
架构师老Y18 小时前
011、消息队列应用:RabbitMQ、Kafka与Celery
python·架构·kafka·rabbitmq·ruby
talen_hx2961 天前
《kafka核心源码解读》学习笔记 Day 02
笔记·学习·kafka
lifallen1 天前
如何保证 Kafka 的消息顺序性?
java·大数据·分布式·kafka
真实的菜1 天前
Kafka 2.x vs 3.x,我为什么选择升级?
kafka
时光追逐者1 天前
分享四款开源且实用的 Kafka 管理工具
分布式·kafka·开源
Rick19931 天前
rabbitmq, rocketmq, kafka这三种消息如何分别保住可靠性,顺序性,以及应用场景?
kafka·rabbitmq·rocketmq
☞遠航☜1 天前
kafka快速上手
分布式·kafka·linq
工具罗某人1 天前
docker compose部署kafka集群搭建
docker·容器·kafka
qq_297574672 天前
【Kafka 系列・入门第六篇】Kafka 集群部署(3 节点)+ 负载均衡配置
分布式·kafka·负载均衡