当前位置: 首页 > article >正文

使用k6进行kafka负载测试

1.安装环境

kafka环境

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

xk6-kafka环境

./xk6 build --with github.com/mostafa/xk6-kafka@latest

查看安装情况

2.编写脚本

test_kafka.js

// 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服务,打开终端输入命令

./k6 run test_kafka.js --vus 50 --duration 10s

测试结果


http://www.kler.cn/a/442215.html

相关文章:

  • dl学习笔记:(4)简单神经网络
  • 二叉树总结(hot100)
  • RV1126+FFMPEG推流项目(9)AI和AENC模块绑定,并且开启线程采集
  • 差分(前缀和的逆运算)
  • 快手极速版如何查找ip归属地?怎么关掉
  • 【Pytorch实用教程】TCN(Temporal Convolutional Network,时序卷积网络)简介
  • 允许某段网络访问Linux服务器上的MariaDB
  • Excel技巧:使用PowerQuery批量提取文件名
  • 如何在铁威马NAS上安装内网穿透,实现对铁威马NAS的远程访问管理
  • el-table表格嵌套子表格:展开所有内容;对当前展开行内容修改,当前行默认展开;
  • OpenCV相机标定与3D重建(24)计算两个二维点集之间的最佳仿射变换矩阵(2x3)函数estimateAffine2D()的使用
  • 【第七节】Git 进阶操作
  • 土地档案管理关系[源码+文档]
  • 包子凑数(2017年蓝桥杯试题H)
  • 提前对风险进行预警并实施管控,运用AI技术将管理推向新时代的智慧地产开源了。
  • 大腾智能CAD:国产云原生三维设计新选择
  • 基于python对网页进行爬虫简单教程
  • ISCTF复现-misc
  • docker 搭建自动唤醒UpSnap工具
  • CAN配置---波特率中断引脚等---autochips-AC7811-ARM-M3内核
  • 指针的一些题目
  • Python中opencv的一些函数及应用
  • AngularJS 输入验证
  • ONES 功能上新|ONES Copilot、ONES Wiki 新功能一览
  • 指针的深入讲解
  • 达梦8-达梦数据的示例用户和表