|
| 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 | +}); |
0 commit comments