File: search_documents.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (53 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download
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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os

from azure_devtools.perfstress_tests import PerfStressTest

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient as SyncClient
from azure.search.documents.aio import SearchClient as AsyncClient


class SearchDocumentsTest(PerfStressTest):
    def __init__(self, arguments):
        super().__init__(arguments)
        api_key = self.get_from_env("AZURE_SEARCH_API_KEY")
        service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
        index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
        key = os.getenv("AZURE_SEARCH_API_KEY")
        self.service_client = SyncClient(service_endpoint, index_name, AzureKeyCredential(api_key))
        self.async_service_client = AsyncClient(service_endpoint, index_name, AzureKeyCredential(api_key))

    @staticmethod
    def add_arguments(parser):
        super(SearchDocumentsTest, SearchDocumentsTest).add_arguments(parser)
        parser.add_argument('--num-documents', nargs='?', type=int,
                            help='The number of results expect to be returned.',
                            default=-1)

    async def global_setup(self):
        await super().global_setup()

    async def close(self):
        await self.async_service_client.close()
        await super().close()

    def run_sync(self):
        if self.args.num_documents == -1:
            results = len(self.service_client.search(search_text="luxury"))
        else:
            results = len(self.service_client.search(search_text="luxury", top=self.args.num_documents))


    async def run_async(self):
        if self.args.num_documents == -1:
            results = await self.async_service_client.search(search_text="luxury")
        else:
            results = await self.async_service_client.search(search_text="luxury", top=self.args.num_documents)
        count = 0
        async for result in results:
            count += count