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

使用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 objectbrokers: ["localhost:9092"],topic: "my-topic",
});const reader = new Reader({// ReaderConfig objectbrokers: ["localhost:9092"],topic: "my-topic",
});const connection = new Connection({// ConnectionConfig objectaddress: "localhost:9092",
});const schemaRegistry = new SchemaRegistry();
// Can accept a SchemaRegistryConfig objectif (__VU == 0) {// Create a topic on initialization (before producing messages)connection.createTopic({// TopicConfig objecttopic: "my-topic",});
}export default function () {// Fetch the list of all topicsconst topics = connection.listTopics();console.log(topics); // list of topics// Produces message to Kafkawriter.produce({// ProduceConfig objectmessages: [// 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 Kafkalet messages = reader.consume({// ConsumeConfig objectlimit: 10,});// your messagesconsole.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 topicconnection.deleteTopic("my-topic");// Close all connectionswriter.close();reader.close();connection.close();
}

3.运行测试

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

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

测试结果

http://www.lryc.cn/news/508206.html

相关文章:

  • Unity A*算法实现+演示
  • 浏览器要求用户确认 Cookies Privacy(隐私相关内容)是基于隐私法规的要求,VUE 实现,html 代码
  • 如何设计高效的商品系统并提升扩展性:从架构到实践的全方位探索
  • 使用计算机创建一个虚拟世界
  • datasets笔记:两种数据集对象
  • 【ETCD】【Linearizable Read OR Serializable Read】ETCD 数据读取:强一致性 vs 高性能,选择最适合的读取模式
  • 【CSS in Depth 2 精译_089】15.2:CSS 过渡特效中的定时函数
  • 不常用命令指南
  • spring mvc | servlet :serviceImpl无法自动装配 UserMapper
  • STM32 HAL库之串口接收不定长字符
  • Pyqt6的tableWidget填充数据
  • ASP.NET Core - 依赖注入 自动批量注入
  • UVM 验证方法学之interface学习系列文章(十一)virtual interface 再续篇
  • 面试题整理5----进程、线程、协程区别及僵尸进程处理
  • OpenTK 中帧缓存的深度解析与应用实践
  • 第2节-Test Case如何调用Object Repository中的请求并关联参数
  • 【HarmonyOS NEXT】Web 组件的基础用法以及 H5 侧与原生侧的双向数据通讯
  • Android学习(六)-Kotlin编程语言-数据类与单例类
  • CV-OCR经典论文解读|An Empirical Study of Scaling Law for OCR/OCR 缩放定律的实证研究
  • 力扣274. H 指数
  • 挑战一个月基本掌握C++(第五天)了解运算符,循环,判断
  • Python的sklearn中的RandomForestRegressor使用详解
  • ReactPress 1.6.0:重塑博客体验,引领内容创新
  • 人脸生成3d模型 Era3D
  • kubeadm搭建k8s集群
  • centOS系统进程管理基础知识
  • STM32中ADC模数转换器
  • 初学stm32 --- 外部中断
  • wordpress调用指定分类ID下 相同标签的内容
  • SQL语法基础知识总结