Skip to content

Commit b490d87

Browse files
Schemaregistry performance metrics (#134)
* Add performance tests for serdes * Add and register json schema
1 parent 1d12e0b commit b490d87

File tree

5 files changed

+172
-12
lines changed

5 files changed

+172
-12
lines changed

package-lock.json

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
roots: [".."],
3+
transform: {
4+
'^.+\\.tsx?$': 'ts-jest',
5+
},
6+
};

schemaregistry-examples/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"devDependencies": {
1111
"@confluentinc/kafka-javascript": "file:..",
1212
"@confluentinc/schemaregistry": "file:../schemaregistry",
13+
"@types/jest": "^29.5.14",
1314
"axios": "^1.7.7",
15+
"jest": "^29.7.0",
1416
"uuid": "^10.0.0"
1517
}
1618
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import {
2+
AvroSerializer, AvroDeserializer, AvroSerializerConfig, SerdeType, Serializer, Deserializer,
3+
JsonSerializer, JsonDeserializer, JsonSerializerConfig,
4+
ClientConfig, SchemaRegistryClient, SchemaInfo
5+
} from "@confluentinc/schemaregistry";
6+
import { localAuthCredentials } from "../constants";
7+
import { v4 } from "uuid";
8+
import { beforeEach, describe, it } from '@jest/globals';
9+
10+
const clientConfig: ClientConfig = {
11+
baseURLs: ['http://localhost:8081'],
12+
cacheCapacity: 512,
13+
cacheLatestTtlSecs: 60,
14+
basicAuthCredentials: localAuthCredentials,
15+
};
16+
17+
const avroSchemaString: string = JSON.stringify({
18+
type: 'record',
19+
name: 'User',
20+
fields: [
21+
{ name: 'name', type: 'string' },
22+
{ name: 'age', type: 'int' },
23+
{ name: 'address', type: 'string' },
24+
],
25+
});
26+
27+
const jsonSchemaString: string = JSON.stringify({
28+
"$schema": "http://json-schema.org/draft-07/schema#",
29+
"title": "User",
30+
"type": "object",
31+
"properties": {
32+
"name": {
33+
"type": "string"
34+
},
35+
"age": {
36+
"type": "integer"
37+
},
38+
"address": {
39+
"type": "string"
40+
}
41+
},
42+
"required": ["name", "age", "address"]
43+
});
44+
45+
const avroSchemaInfo: SchemaInfo = {
46+
schema: avroSchemaString,
47+
schemaType: 'AVRO'
48+
};
49+
50+
const jsonSchemaInfo: SchemaInfo = {
51+
schema: jsonSchemaString,
52+
schemaType: 'JSON'
53+
};
54+
55+
const data: { name: string; age: number; address: string; }[] = [];
56+
57+
let schemaRegistryClient: SchemaRegistryClient;
58+
59+
function generateData(numRecords: number) {
60+
for (let i = 0; i < numRecords; i++) {
61+
data.push({
62+
name: `User ${i}`,
63+
age: Math.floor(Math.random() * 100),
64+
address: v4()
65+
});
66+
}
67+
}
68+
69+
generateData(10000);
70+
71+
async function serializeAndDeserializeSchemas(serializer: Serializer, deserializer: Deserializer, topic: string) {
72+
Promise.all(
73+
data.map(async (record) => {
74+
const serialized = await serializer.serialize(topic, record);
75+
await deserializer.deserialize(topic, serialized);
76+
})
77+
);
78+
}
79+
80+
describe('Serialization Performance Test', () => {
81+
82+
beforeEach(async () => {
83+
schemaRegistryClient = new SchemaRegistryClient(clientConfig);
84+
});
85+
86+
it("Should measure serialization and deserialization performance for JSON", async () => {
87+
const topic = v4();
88+
await schemaRegistryClient.register(topic + "-value", jsonSchemaInfo);
89+
90+
const jsonSerializerConfig: JsonSerializerConfig = { useLatestVersion: true };
91+
const jsonSerializer: JsonSerializer = new JsonSerializer(schemaRegistryClient, SerdeType.VALUE, jsonSerializerConfig);
92+
const jsonDeserializer: JsonDeserializer = new JsonDeserializer(schemaRegistryClient, SerdeType.VALUE, {});
93+
94+
const start = performance.now();
95+
await serializeAndDeserializeSchemas(jsonSerializer, jsonDeserializer, topic);
96+
const end = performance.now();
97+
98+
console.log(`JSON serialization and deserialization took ${end - start} ms`);
99+
});
100+
101+
it("Should measure serialization and deserialization performance for Avro", async () => {
102+
const topic = v4();
103+
await schemaRegistryClient.register(topic + "-value", avroSchemaInfo);
104+
105+
const avroSerializerConfig: AvroSerializerConfig = { useLatestVersion: true };
106+
const serializer: AvroSerializer = new AvroSerializer(schemaRegistryClient, SerdeType.VALUE, avroSerializerConfig);
107+
const deserializer: AvroDeserializer = new AvroDeserializer(schemaRegistryClient, SerdeType.VALUE, {});
108+
109+
const start = performance.now();
110+
await serializeAndDeserializeSchemas(serializer, deserializer, topic);
111+
const end = performance.now();
112+
113+
console.log(`Avro serialization and deserialization took ${end - start} ms`);
114+
});
115+
116+
// it("Should measure serialization and deserialization performance for Protobuf", async () => {
117+
// const protobufSerializerConfig: ProtobufSerializerConfig = { useLatestVersion: true };
118+
// const serializer: ProtobufSerializer = new ProtobufSerializer(schemaRegistryClient, SerdeType.VALUE, protobufSerializerConfig);
119+
// const deserializer: ProtobufDeserializer = new ProtobufDeserializer(schemaRegistryClient, SerdeType.VALUE, {});
120+
121+
// const start = performance.now();
122+
// await serializeAndDeserializeSchemas(serializer, deserializer, topic);
123+
// const end = performance.now();
124+
125+
// console.log(`Protobuf serialization and deserialization took ${end - start} ms`);
126+
// });
127+
});

schemaregistry-examples/tsconfig.json

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
{
22
"compilerOptions": {
3-
"target": "ES6",
4-
"module": "commonjs",
5-
"outDir": "./dist",
6-
"rootDir": "./src",
7-
"strict": true,
3+
"baseUrl": ".",
4+
"target": "es2021",
5+
"lib": [
6+
"es2021", "dom"
7+
],
8+
"declaration": true,
9+
"outDir": "dist",
10+
"types": ["../node_modules/@types/node"],
811
"esModuleInterop": true,
9-
"skipLibCheck": true,
10-
"forceConsistentCasingInFileNames": true
12+
"strict": true,
13+
"forceConsistentCasingInFileNames": true,
14+
"allowUnusedLabels": false,
15+
"allowUnreachableCode": false,
16+
"noFallthroughCasesInSwitch": true,
17+
"noImplicitOverride": true,
18+
"noImplicitReturns": true,
19+
"noPropertyAccessFromIndexSignature": true,
20+
"noUnusedLocals": true,
21+
"useUnknownInCatchVariables": true,
22+
"resolveJsonModule": true,
23+
"moduleResolution": "nodenext",
24+
"module": "nodenext",
25+
"skipLibCheck": true
1126
},
12-
"include": ["src/**/*"],
13-
"exclude": ["node_modules"]
27+
"include": [
28+
"**/*",
29+
"../test/**/*"
30+
],
31+
"exclude": [
32+
"dist"
33+
]
1434
}

0 commit comments

Comments
 (0)