-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathtest_azure_event_hub.js
More file actions
128 lines (110 loc) · 2.79 KB
/
Copy pathtest_azure_event_hub.js
File metadata and controls
128 lines (110 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
This is a k6 test script that imports the xk6-kafka and
tests Event Hub with 1 string message per iteration. It
also uses Azure Entra authentication.
The small number and size of messages per iteration is to
reduce cost for smoke testing. Event Hub charges for
throughput on Standard tier, and Premium tier is expensive
for a solo developer to purchase.
If you are benchmarking a high scale use case, you can
adjust the test settings to accomodate your throughput
requirements.
*/
import { check } from "k6";
import {
Producer,
Consumer,
AdminClient,
SchemaRegistry,
SCHEMA_TYPE_STRING,
SASL_AZURE_ENTRA,
TLS_1_2,
} from "k6/x/kafka";
if (!__ENV.EVENT_HUB_NAMESPACE) {
throw new Error(`Environment variable EVENT_HUB_NAMESPACE is missing!`);
}
const brokers = [`${__ENV.EVENT_HUB_NAMESPACE}.servicebus.windows.net:9093`];
const topic = "k6-event-hub-test";
const numPartitions = 1;
const groupId = "k6";
const saslConfig = {
algorithm: SASL_AZURE_ENTRA,
};
const tlsConfig = {
enableTls: true,
insecureSkipTlsVerify: false,
minVersion: TLS_1_2,
};
const producer = new Producer({
brokers: brokers,
topic: topic,
sasl: saslConfig,
tls: tlsConfig,
});
const consumer = new Consumer({
brokers: brokers,
topic: topic,
groupId: groupId,
// Event Hub does not support all rebalancing strategies
groupBalancers: ["group_balancer_round_robin"],
sasl: saslConfig,
tls: tlsConfig,
// Need to allow time for rebalance
maxWait: "30s",
});
const adminClient = new AdminClient({
brokers: brokers,
sasl: saslConfig,
tls: tlsConfig,
});
const schemaRegistry = new SchemaRegistry();
export function setup() {
try {
adminClient.createTopic({ topic: topic });
} catch {
// topic already exists
}
}
export default function main() {
produce();
consume();
}
function produce() {
for (let i = 0; i < 10; i++) {
let messages = [
{
key: schemaRegistry.serialize({
data: String(i),
schemaType: SCHEMA_TYPE_STRING,
}),
value: schemaRegistry.serialize({
data: `Hello, Event Hub!`,
schemaType: SCHEMA_TYPE_STRING,
}),
},
];
producer.produce({ messages: messages });
}
}
function consume() {
let messages = consumer.consume({ limit: 10 });
check(messages, {
"10 messages returned": (msgs) => msgs.length == 10,
"key is correct": (msgs) =>
schemaRegistry.deserialize({
data: msgs[0].key,
schemaType: SCHEMA_TYPE_STRING,
}) != undefined,
"value is correct": (msgs) =>
schemaRegistry.deserialize({
data: msgs[0].value,
schemaType: SCHEMA_TYPE_STRING,
}) == "Hello, Event Hub!",
});
}
export function teardown(data) {
adminClient.deleteTopic(topic);
adminClient.close();
producer.close();
consumer.close();
}