使用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

测试结果

相关推荐
cui_win10 小时前
Kafka 配置参数详解:ZooKeeper 模式与 KRaft 模式对比
分布式·zookeeper·kafka
cui_win14 小时前
深入理解 Kafka 核心:主题、分区与副本的协同机制
网络·分布式·kafka
黄雪超17 小时前
Kafka——无消息丢失配置怎么实现?
大数据·分布式·kafka
livemetee18 小时前
springboot 整合spring-kafka客户端:SASL_SSL+PLAINTEXT方式
spring boot·spring·kafka
黄雪超2 天前
Kafka——应该选择哪种Kafka?
大数据·分布式·kafka
remCoding2 天前
Java大厂面试实录:从Spring Boot到AI大模型的深度技术拷问
java·spring boot·redis·spring cloud·ai·kafka·microservices
Linux-palpitate2 天前
kafka的部署
分布式·kafka
飘飘渺渺渺红尘2 天前
消息中间件(Kafka VS RocketMQ)
分布式·kafka·rocketmq
黄雪超2 天前
Kafka——生产者压缩算法
大数据·分布式·kafka
亿牛云爬虫专家2 天前
Kafka与Flink打造流式数据采集方案:以二手房信息为例
flink·kafka·数据采集·爬虫代理·数据处理·二手房·定时抓取