Skip to content

Instantly share code, notes, and snippets.

@converge
Created October 2, 2023 16:29
Show Gist options
  • Save converge/aabd783f622e441ec3032d6462117125 to your computer and use it in GitHub Desktop.
Save converge/aabd783f622e441ec3032d6462117125 to your computer and use it in GitHub Desktop.
/*
This is a k6 test script that imports the xk6-kafka and
tests Kafka by sending 200 Avro messages per iteration
without any associated key.
*/
import {
Writer,
SchemaRegistry,
SCHEMA_TYPE_STRING,
Connection,
SASL_SSL,
TLS_1_2,
} from "k6/x/kafka"; // import kafka extension
const brokers = [""]
const topic = 'knative-demo-topic'
const saslConfig = {
username: '',
password: '',
algorithm: SASL_SSL,
};
const tlsConfig = {
// Enable/disable TLS (default: false)
enableTls: true,
// Skip TLS verification if the certificate is invalid or self-signed (default: false)
insecureSkipTlsVerify: false,
// Possible values:
// TLS_1_0
// TLS_1_1
// TLS_1_2 (default)
// TLS_1_3
minVersion: TLS_1_2,
// Only needed if you have a custom or self-signed certificate and keys
// clientCertPem: "/path/to/your/client.pem",
// clientKeyPem: "/path/to/your/client-key.pem",
// serverCaPem: "/path/to/your/ca.pem",
};
const connection = new Connection({
address: brokers[0],
sasl: saslConfig,
tls: tlsConfig,
});
if (__VU == 0) {
console.log(
"Existing topics: ",
connection.listTopics(saslConfig, tlsConfig),
);
}
const writer = new Writer({
brokers: brokers,
topic: topic,
sasl: saslConfig,
tls: tlsConfig,
// autoCreateTopic: true,
});
const schemaRegistry = new SchemaRegistry();
// let initialValue = 1;
// const getSequence = () => {
// let num = initialValue++;
// return num.toString();
// }
export default async function () {
// export default async () => {
for (let index = 0; index < 100; index++) {
writer.produce({
// ProduceConfig object
messages: [
// Message object(s)
{
key: schemaRegistry.serialize({
data: "my-key",
schemaType: SCHEMA_TYPE_STRING,
}),
value: schemaRegistry.serialize({
data: "my-value-123",
// data: getSequence(),
schemaType: SCHEMA_TYPE_STRING,
}),
},
],
});
}
}
export function teardown(data) {
writer.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment