|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import json |
| 7 | +import math |
| 8 | +import random, string |
| 9 | +import sys |
| 10 | + |
| 11 | +from azure_devtools.perfstress_tests import PerfStressTest |
| 12 | +from azure.identity import DefaultAzureCredential |
| 13 | +from azure.identity.aio import DefaultAzureCredential as AsyncDefaultAzureCredential |
| 14 | +from azure.schemaregistry import SchemaRegistryClient |
| 15 | +from azure.schemaregistry.encoder.avroencoder import AvroEncoder |
| 16 | +from azure.schemaregistry.aio import SchemaRegistryClient as AsyncSchemaRegistryClient |
| 17 | +from azure.schemaregistry.encoder.avroencoder.aio import AvroEncoder as AsyncAvroEncoder |
| 18 | + |
| 19 | + |
| 20 | +class _SchemaRegistryAvroTest(PerfStressTest): |
| 21 | + def __init__(self, arguments): |
| 22 | + super().__init__(arguments) |
| 23 | + |
| 24 | + self.fully_qualified_namespace = self.get_from_env( |
| 25 | + "SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE" |
| 26 | + ) |
| 27 | + self.group_name = self.get_from_env("SCHEMAREGISTRY_GROUP") |
| 28 | + self.definition, num_fields = self._create_schema_definition() |
| 29 | + self.content = self._create_content(num_fields) |
| 30 | + |
| 31 | + def _create_schema_definition(self): |
| 32 | + schema_size = self.args.schema_size |
| 33 | + |
| 34 | + # random string to avoid conflicting requests |
| 35 | + letters = string.ascii_lowercase |
| 36 | + randletters = ''.join(random.choice(letters) for i in range(10)) |
| 37 | + |
| 38 | + fields = [] |
| 39 | + schema = { |
| 40 | + "type": "record", |
| 41 | + "name": f"example.User{randletters}", |
| 42 | + "fields": fields, |
| 43 | + } |
| 44 | + |
| 45 | + # 100 bytes |
| 46 | + schema_no_fields_size = sys.getsizeof(json.dumps(schema, separators=(",", ":"))) |
| 47 | + fields.append({"name": "favor_number00000", "type": ["int", "null"]}) |
| 48 | + # each additional field is 50 bytes |
| 49 | + schema_one_field_size = sys.getsizeof(json.dumps(schema, separators=(",", ":"))) |
| 50 | + field_size = schema_one_field_size - schema_no_fields_size |
| 51 | + |
| 52 | + # calculate number of fields to add to get args.schema_size rounded down to nearest 50 multiple |
| 53 | + num_fields = math.floor((schema_size - schema_no_fields_size) / field_size) |
| 54 | + |
| 55 | + for i in range(1, num_fields): |
| 56 | + num_idx = f"{i:05d}" |
| 57 | + fields.append( |
| 58 | + {"name": f"favo_number{num_idx}", "type": ["int", "null"]}, |
| 59 | + ) |
| 60 | + definition = json.dumps(schema, separators=(",", ":")) |
| 61 | + return definition, num_fields |
| 62 | + |
| 63 | + def _create_content(self, num_fields): |
| 64 | + content = {"favor_number00000": 0} |
| 65 | + for i in range(1, num_fields): |
| 66 | + num_idx = f"{i:05d}" |
| 67 | + content[f"favo_number{num_idx}"] = i |
| 68 | + return content |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def add_arguments(parser): |
| 72 | + super(_SchemaRegistryAvroTest, _SchemaRegistryAvroTest).add_arguments(parser) |
| 73 | + parser.add_argument( |
| 74 | + "--schema-size", |
| 75 | + nargs="?", |
| 76 | + type=int, |
| 77 | + help="Size of a single schema. Max 1000000 bytes. Defaults to 150 bytes", |
| 78 | + default=150, |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--num-values", |
| 82 | + nargs="?", |
| 83 | + type=int, |
| 84 | + help="Number of values to encode/decode with given schema. Default is 1.", |
| 85 | + default=1, |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +class _EncodeTest(_SchemaRegistryAvroTest): |
| 90 | + def __init__(self, arguments): |
| 91 | + super().__init__(arguments) |
| 92 | + self.sync_credential = DefaultAzureCredential() |
| 93 | + self.sync_client = SchemaRegistryClient( |
| 94 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 95 | + credential=self.sync_credential, |
| 96 | + ) |
| 97 | + self.sync_encoder = AvroEncoder( |
| 98 | + client=self.sync_client, group_name=self.group_name, auto_register_schemas=True |
| 99 | + ) |
| 100 | + self.async_credential = AsyncDefaultAzureCredential() |
| 101 | + self.async_client = AsyncSchemaRegistryClient( |
| 102 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 103 | + credential=self.async_credential, |
| 104 | + ) |
| 105 | + self.async_encoder = AsyncAvroEncoder( |
| 106 | + client=self.async_client, group_name=self.group_name, auto_register_schemas=True |
| 107 | + ) |
| 108 | + |
| 109 | + async def global_setup(self): |
| 110 | + await super().global_setup() |
| 111 | + |
| 112 | + async def close(self): |
| 113 | + self.sync_client.close() |
| 114 | + self.sync_credential.close() |
| 115 | + self.sync_encoder.close() |
| 116 | + await self.async_client.close() |
| 117 | + await self.async_credential.close() |
| 118 | + await self.async_encoder.close() |
| 119 | + await super().close() |
| 120 | + |
| 121 | + |
| 122 | +class _DecodeTest(_SchemaRegistryAvroTest): |
| 123 | + def __init__(self, arguments): |
| 124 | + super().__init__(arguments) |
| 125 | + self.sync_credential = DefaultAzureCredential() |
| 126 | + self.sync_client = SchemaRegistryClient( |
| 127 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 128 | + credential=self.sync_credential, |
| 129 | + ) |
| 130 | + self.sync_encoder = AvroEncoder( |
| 131 | + client=self.sync_client, group_name=self.group_name, auto_register_schemas=True |
| 132 | + ) |
| 133 | + self.async_credential = AsyncDefaultAzureCredential() |
| 134 | + self.async_client = AsyncSchemaRegistryClient( |
| 135 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 136 | + credential=self.async_credential, |
| 137 | + ) |
| 138 | + self.async_encoder = AsyncAvroEncoder( |
| 139 | + client=self.async_client, group_name=self.group_name, auto_register_schemas=True |
| 140 | + ) |
| 141 | + self.encoded_content = self._encode_content() |
| 142 | + |
| 143 | + def _encode_content(self): |
| 144 | + with self.sync_encoder as encoder: |
| 145 | + return encoder.encode(self.content, schema=self.definition) |
| 146 | + |
| 147 | + async def global_setup(self): |
| 148 | + await super().global_setup() |
| 149 | + |
| 150 | + async def close(self): |
| 151 | + self.sync_client.close() |
| 152 | + self.sync_credential.close() |
| 153 | + self.sync_encoder.close() |
| 154 | + await self.async_client.close() |
| 155 | + await self.async_credential.close() |
| 156 | + await self.async_encoder.close() |
| 157 | + await super().close() |
0 commit comments