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

测试结果

相关推荐
東雪蓮☆1 小时前
Filebeat+Kafka+ELK 日志采集实战
分布式·elk·kafka
努力买辣条1 小时前
KafKa概念与安装
分布式·kafka
不太可爱的叶某人3 小时前
【学习笔记】kafka权威指南——第8章 跨集群数据镜像(7-10章只做了解)
笔记·学习·kafka
不太可爱的叶某人1 天前
【学习笔记】kafka权威指南——第7章 构建数据管道(7-10章只做了解)
笔记·学习·kafka
青云交1 天前
Java 大视界 -- 基于 Java 的大数据实时流处理在工业物联网设备故障预测与智能运维中的应用
java·flink·kafka·工业物联网·设备故障预测·智能运维·实时流处理
编啊编程啊程2 天前
gRPC从0到1系列【20】
java·rpc·kafka·dubbo·nio
Hello.Reader2 天前
在运行中的 Kafka 集群渐进式启用安全零停机实战手册(KRaft/Broker 通用)
分布式·安全·kafka
飘飞雪2 天前
深入浅出kafka:kafka演进以及核心功能介绍
数据库·分布式·kafka
不太可爱的叶某人2 天前
【学习笔记】kafka权威指南——第1章 初识kafka
笔记·学习·kafka
不太可爱的叶某人2 天前
【学习笔记】kafka权威指南——第3章 kafka生产者—向kafka写入数据
笔记·学习·kafka