Skip to content

Instantly share code, notes, and snippets.

@ripla
Created August 5, 2022 12:03
Show Gist options
  • Save ripla/fe7db19c835ec7a62d9a69a265229ba4 to your computer and use it in GitHub Desktop.
Save ripla/fe7db19c835ec7a62d9a69a265229ba4 to your computer and use it in GitHub Desktop.
import {
Writer,
Connection,
AVRO_SERIALIZER,
STRING_SERIALIZER,
} from "k6/x/kafka";
const brokers = ["kafka:29092"];
const writer = new Writer({
brokers: brokers,
topic: "mytopic",
autoCreateTopic: false,
});
const connection = new Connection({
address: brokers[0],
});
const config = {
producer: {
keySerializer: STRING_SERIALIZER,
valueSerializer: AVRO_SERIALIZER,
},
schemaRegistry: {
url: "http://schemaregistry:8085",
},
};
export const options = {
vus: 10,
duration: "1m",
};
export default function () {
for (let i = 0; i < 5; i++) {
let sentMessages = [
{
key: "someKey",
value: JSON.stringify({
someField: `value-${__VU}-${__ITER}-${i}`,
}),
},
];
writer.produce({
messages: sentMessages,
config: config,
});
}
}
export function teardown(data) {
writer.close();
connection.close();
}
---
version: "3"
networks:
mynetwork:
driver: bridge
name: mynetwork
services:
zookeeper:
image: zookeeper:3.7.0
hostname: zookeeper
ports:
- 2181:2181
networks:
- mynetwork
kafka:
image: wurstmeister/kafka:2.13-2.8.1
ports:
- 9092:9092
depends_on:
- zookeeper
environment:
- KAFKA_BROKER_ID=1
- KAFKA_CREATE_TOPICS=mytopic:1:1
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_LISTENERS=PLAINTEXT://:29092,EXTERNAL://0.0.0.0:9092
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:29092,EXTERNAL://localhost:9092
- KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,EXTERNAL:PLAINTEXT
- KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
- KAFKA_SCHEMA_REGISTRY_URL=schemaregistry:8085
networks:
- mynetwork
kafka-ui:
image: provectuslabs/kafka-ui:latest
ports:
- 8080:8080
depends_on:
- zookeeper
- kafka
- schemaregistry
environment:
KAFKA_CLUSTERS_0_NAME: local
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092
KAFKA_CLUSTERS_0_ZOOKEEPER: zookeeper:2181
KAFKA_CLUSTERS_0_SCHEMAREGISTRY: http://schemaregistry:8085
networks:
- mynetwork
schemaregistry:
image: confluentinc/cp-schema-registry:latest
ports:
- 8085:8085
depends_on:
- zookeeper
- kafka
environment:
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: PLAINTEXT://kafka:29092
SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: zookeeper:2181
SCHEMA_REGISTRY_KAFKASTORE_SECURITY_PROTOCOL: PLAINTEXT
SCHEMA_REGISTRY_HOST_NAME: schemaregistry
SCHEMA_REGISTRY_LISTENERS: http://schemaregistry:8085
SCHEMA_REGISTRY_SCHEMA_REGISTRY_INTER_INSTANCE_PROTOCOL: "http"
networks:
- mynetwork
{
"schema": "{\"type\":\"record\",\"name\":\"MySchema\",\"fields\":[{\"name\":\"someField\",\"type\":\"string\"}]}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment