Skip to content

Commit ba1d11d

Browse files
enhance statistics ut coverage (#252)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e237454 commit ba1d11d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import asyncio
5+
import json
6+
import time
7+
import unittest
8+
9+
import requests
10+
11+
from comps import (
12+
ServiceOrchestrator,
13+
TextDoc,
14+
opea_microservices,
15+
register_microservice,
16+
register_statistics,
17+
statistics_dict,
18+
)
19+
20+
21+
@register_microservice(name="s1", host="0.0.0.0", port=8083, endpoint="/v1/add")
22+
@register_statistics(names=["opea_service@s1_add"])
23+
async def s1_add(request: TextDoc) -> TextDoc:
24+
start = time.time()
25+
time.sleep(5)
26+
req = request.model_dump_json()
27+
req_dict = json.loads(req)
28+
text = req_dict["text"]
29+
text += "opea"
30+
statistics_dict["opea_service@s1_add"].append_latency(time.time() - start, None)
31+
return {"text": text}
32+
33+
34+
class TestBaseStatistics(unittest.IsolatedAsyncioTestCase):
35+
def setUp(self):
36+
self.s1 = opea_microservices["s1"]
37+
self.s1.start()
38+
39+
self.service_builder = ServiceOrchestrator()
40+
self.service_builder.add(opea_microservices["s1"])
41+
42+
def tearDown(self):
43+
self.s1.stop()
44+
45+
async def test_base_statistics(self):
46+
for _ in range(2):
47+
task1 = asyncio.create_task(self.service_builder.schedule(initial_inputs={"text": "hello, "}))
48+
await asyncio.gather(task1)
49+
result_dict1 = task1.result()
50+
51+
response = requests.get("http://localhost:8083/v1/statistics")
52+
res = response.json()
53+
p50 = res["opea_service@s1_add"]["p50_latency"]
54+
p99 = res["opea_service@s1_add"]["p99_latency"]
55+
self.assertEqual(int(p50), int(p99))
56+
57+
58+
if __name__ == "__main__":
59+
unittest.main()

0 commit comments

Comments
 (0)