|
| 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 sys |
| 9 | + |
| 10 | +from azure_devtools.perfstress_tests import PerfStressTest |
| 11 | +from azure.identity import DefaultAzureCredential |
| 12 | +from azure.identity.aio import DefaultAzureCredential as AsyncDefaultAzureCredential |
| 13 | +from azure.schemaregistry import SchemaRegistryClient |
| 14 | +from azure.schemaregistry.aio import SchemaRegistryClient as AsyncSchemaRegistryClient |
| 15 | + |
| 16 | + |
| 17 | +class _SchemaRegistryTest(PerfStressTest): |
| 18 | + def __init__(self, arguments): |
| 19 | + super().__init__(arguments) |
| 20 | + |
| 21 | + self.fully_qualified_namespace = self.get_from_env( |
| 22 | + "SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE" |
| 23 | + ) |
| 24 | + self.group_name = self.get_from_env("SCHEMAREGISTRY_GROUP") |
| 25 | + self.name = "your-schema-name" |
| 26 | + self.format = "Avro" |
| 27 | + self.definition = self._create_schema_definition() |
| 28 | + |
| 29 | + def _create_schema_definition(self): |
| 30 | + schema_size = self.args.schema_size |
| 31 | + |
| 32 | + fields = [] |
| 33 | + schema = { |
| 34 | + "type": "record", |
| 35 | + "name": "example.User", |
| 36 | + "fields": fields, |
| 37 | + } |
| 38 | + |
| 39 | + # 100 bytes |
| 40 | + schema_no_fields_size = sys.getsizeof(json.dumps(schema, separators=(",", ":"))) |
| 41 | + fields.append({"name": "favor_number00000", "type": ["int", "null"]}) |
| 42 | + # each additional field is 50 bytes |
| 43 | + schema_one_field_size = sys.getsizeof(json.dumps(schema, separators=(",", ":"))) |
| 44 | + field_size = schema_one_field_size - schema_no_fields_size |
| 45 | + |
| 46 | + # calculate number of fields to add to get args.schema_size rounded down to nearest 50 multiple |
| 47 | + num_fields = math.floor((schema_size - schema_no_fields_size) / field_size) |
| 48 | + |
| 49 | + for i in range(1, num_fields): |
| 50 | + num_idx = f"{i:05d}" |
| 51 | + fields.append( |
| 52 | + {"name": f"favo_number{num_idx}", "type": ["int", "null"]}, |
| 53 | + ) |
| 54 | + definition = json.dumps(schema, separators=(",", ":")) |
| 55 | + return definition |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def add_arguments(parser): |
| 59 | + super(_SchemaRegistryTest, _SchemaRegistryTest).add_arguments(parser) |
| 60 | + parser.add_argument( |
| 61 | + "--schema-size", |
| 62 | + nargs="?", |
| 63 | + type=int, |
| 64 | + help="Size of a single schema. Max 1000000 bytes. Defaults to 150 bytes", |
| 65 | + default=150, |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "--num-schemas", |
| 69 | + nargs="?", |
| 70 | + type=int, |
| 71 | + help="""Number of schemas to register/get by ID/get properties for. Default is 10. |
| 72 | + May result in 'Forbidden' Exception for `RegisterSchemaTest` operation, if reached |
| 73 | + the limit of schemas allowed for Schema Registry tier.""", |
| 74 | + default=10, |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +class _RegisterTest(_SchemaRegistryTest): |
| 79 | + def __init__(self, arguments): |
| 80 | + super().__init__(arguments) |
| 81 | + self.sync_credential = DefaultAzureCredential() |
| 82 | + self.sync_client = SchemaRegistryClient( |
| 83 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 84 | + credential=self.sync_credential, |
| 85 | + ) |
| 86 | + self.async_credential = AsyncDefaultAzureCredential() |
| 87 | + self.async_client = AsyncSchemaRegistryClient( |
| 88 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 89 | + credential=self.async_credential, |
| 90 | + ) |
| 91 | + |
| 92 | + async def global_setup(self): |
| 93 | + await super().global_setup() |
| 94 | + |
| 95 | + async def close(self): |
| 96 | + self.sync_client.close() |
| 97 | + self.sync_credential.close() |
| 98 | + await self.async_client.close() |
| 99 | + await self.async_credential.close() |
| 100 | + await super().close() |
| 101 | + |
| 102 | + |
| 103 | +class _GetSchemaTest(_SchemaRegistryTest): |
| 104 | + def __init__(self, arguments): |
| 105 | + super().__init__(arguments) |
| 106 | + self.sync_credential = DefaultAzureCredential() |
| 107 | + self.sync_client = SchemaRegistryClient( |
| 108 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 109 | + credential=self.sync_credential, |
| 110 | + ) |
| 111 | + self.async_credential = AsyncDefaultAzureCredential() |
| 112 | + self.async_client = AsyncSchemaRegistryClient( |
| 113 | + fully_qualified_namespace=self.fully_qualified_namespace, |
| 114 | + credential=self.async_credential, |
| 115 | + ) |
| 116 | + self.schema_id = self._preregister_schema() |
| 117 | + |
| 118 | + def _preregister_schema(self): |
| 119 | + with self.sync_client as client: |
| 120 | + schema_properties = client.register_schema( |
| 121 | + self.group_name, self.name, self.definition, self.format |
| 122 | + ) |
| 123 | + return schema_properties.id |
| 124 | + |
| 125 | + async def global_setup(self): |
| 126 | + await super().global_setup() |
| 127 | + |
| 128 | + async def close(self): |
| 129 | + self.sync_client.close() |
| 130 | + self.sync_credential.close() |
| 131 | + await self.async_client.close() |
| 132 | + await self.async_credential.close() |
| 133 | + await super().close() |
0 commit comments