-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtest_avro_with_schema_registry.js
157 lines (145 loc) · 3.37 KB
/
test_avro_with_schema_registry.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
This is a k6 test script that imports the xk6-kafka and
tests Kafka with a 100 Avro messages per iteration.
*/
import { check } from "k6";
import {
Writer,
Reader,
Connection,
SchemaRegistry,
KEY,
VALUE,
TOPIC_NAME_STRATEGY,
RECORD_NAME_STRATEGY,
SCHEMA_TYPE_AVRO,
} from "k6/x/kafka"; // import kafka extension
const brokers = ["localhost:9092"];
const topic = "com.example.person";
const writer = new Writer({
brokers: brokers,
topic: topic,
autoCreateTopic: true,
});
const reader = new Reader({
brokers: brokers,
topic: topic,
});
const connection = new Connection({
address: brokers[0],
});
const schemaRegistry = new SchemaRegistry({
url: "http://localhost:8081",
});
if (__VU == 0) {
connection.createTopic({ topic: topic });
}
const keySchema = `{
"name": "KeySchema",
"type": "record",
"namespace": "com.example.key",
"fields": [
{
"name": "ssn",
"type": "string"
}
]
}
`;
const valueSchema = `{
"name": "ValueSchema",
"type": "record",
"namespace": "com.example.value",
"fields": [
{
"name": "firstName",
"type": "string"
},
{
"name": "lastName",
"type": "string"
}
]
}`;
const keySubjectName = schemaRegistry.getSubjectName({
topic: topic,
element: KEY,
subjectNameStrategy: TOPIC_NAME_STRATEGY,
schema: keySchema,
});
const valueSubjectName = schemaRegistry.getSubjectName({
topic: topic,
element: VALUE,
subjectNameStrategy: RECORD_NAME_STRATEGY,
schema: valueSchema,
});
const keySchemaObject = schemaRegistry.createSchema({
subject: keySubjectName,
schema: keySchema,
schemaType: SCHEMA_TYPE_AVRO,
});
const valueSchemaObject = schemaRegistry.createSchema({
subject: valueSubjectName,
schema: valueSchema,
schemaType: SCHEMA_TYPE_AVRO,
});
export default function () {
for (let index = 0; index < 100; index++) {
let messages = [
{
key: schemaRegistry.serialize({
data: {
ssn: "ssn-" + index,
},
schema: keySchemaObject,
schemaType: SCHEMA_TYPE_AVRO,
}),
value: schemaRegistry.serialize({
data: {
firstName: "firstName-" + index,
lastName: "lastName-" + index,
},
schema: valueSchemaObject,
schemaType: SCHEMA_TYPE_AVRO,
}),
},
];
writer.produce({ messages: messages });
}
let messages = reader.consume({ limit: 20 });
check(messages, {
"20 message returned": (msgs) => msgs.length == 20,
"key starts with 'ssn-' string": (msgs) =>
schemaRegistry
.deserialize({
data: msgs[0].key,
schema: keySchemaObject,
schemaType: SCHEMA_TYPE_AVRO,
})
.ssn.startsWith("ssn-"),
"value contains 'firstName-' and 'lastName-' strings": (msgs) =>
schemaRegistry
.deserialize({
data: msgs[0].value,
schema: valueSchemaObject,
schemaType: SCHEMA_TYPE_AVRO,
})
.firstName.startsWith("firstName-") &&
schemaRegistry
.deserialize({
data: msgs[0].value,
schema: valueSchemaObject,
schemaType: SCHEMA_TYPE_AVRO,
})
.lastName.startsWith("lastName-"),
});
}
export function teardown(data) {
if (__VU == 0) {
// Delete the topic
connection.deleteTopic(topic);
}
writer.close();
reader.close();
connection.close();
}