forked from opea-project/GenAIComps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance statistics ut coverage (opea-project#252)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Signed-off-by: Yogesh Pandey <[email protected]>
- Loading branch information
1 parent
50c114e
commit 524e82e
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import asyncio | ||
import json | ||
import time | ||
import unittest | ||
|
||
import requests | ||
|
||
from comps import ( | ||
ServiceOrchestrator, | ||
TextDoc, | ||
opea_microservices, | ||
register_microservice, | ||
register_statistics, | ||
statistics_dict, | ||
) | ||
|
||
|
||
@register_microservice(name="s1", host="0.0.0.0", port=8083, endpoint="/v1/add") | ||
@register_statistics(names=["opea_service@s1_add"]) | ||
async def s1_add(request: TextDoc) -> TextDoc: | ||
start = time.time() | ||
time.sleep(5) | ||
req = request.model_dump_json() | ||
req_dict = json.loads(req) | ||
text = req_dict["text"] | ||
text += "opea" | ||
statistics_dict["opea_service@s1_add"].append_latency(time.time() - start, None) | ||
return {"text": text} | ||
|
||
|
||
class TestBaseStatistics(unittest.IsolatedAsyncioTestCase): | ||
def setUp(self): | ||
self.s1 = opea_microservices["s1"] | ||
self.s1.start() | ||
|
||
self.service_builder = ServiceOrchestrator() | ||
self.service_builder.add(opea_microservices["s1"]) | ||
|
||
def tearDown(self): | ||
self.s1.stop() | ||
|
||
async def test_base_statistics(self): | ||
for _ in range(2): | ||
task1 = asyncio.create_task(self.service_builder.schedule(initial_inputs={"text": "hello, "})) | ||
await asyncio.gather(task1) | ||
result_dict1 = task1.result() | ||
|
||
response = requests.get("http://localhost:8083/v1/statistics") | ||
res = response.json() | ||
p50 = res["opea_service@s1_add"]["p50_latency"] | ||
p99 = res["opea_service@s1_add"]["p99_latency"] | ||
self.assertEqual(int(p50), int(p99)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |