Skip to content

Commit 342c7c8

Browse files
committed
Improve BaseStatistics test/code coverage
Signed-off-by: Eero Tamminen <[email protected]>
1 parent 6f19a4a commit 342c7c8

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

tests/cores/mega/test_base_statistics.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
register_statistics,
1818
statistics_dict,
1919
)
20+
from comps.cores.mega.base_statistics import collect_all_statistics
2021

22+
SVC1 = "opea_service@s1_add"
23+
SVC2 = "open_service@test"
2124

25+
26+
@register_statistics(names=[SVC1])
27+
@register_statistics(names=[SVC2])
2228
@register_microservice(name="s1", host="0.0.0.0", port=8083, endpoint="/v1/add")
23-
@register_statistics(names=["opea_service@s1_add"])
2429
async def s1_add(request: TextDoc) -> TextDoc:
2530
start = time.time()
2631
time.sleep(5)
2732
req = request.model_dump_json()
2833
req_dict = json.loads(req)
2934
text = req_dict["text"]
3035
text += "opea"
31-
statistics_dict["opea_service@s1_add"].append_latency(time.time() - start, None)
36+
statistics_dict[SVC1].append_latency(time.time() - start, None)
3237
return {"text": text}
3338

3439

@@ -49,14 +54,45 @@ async def test_base_statistics(self):
4954
for _ in range(2):
5055
task1 = asyncio.create_task(self.service_builder.schedule(initial_inputs={"text": "hello, "}))
5156
await asyncio.gather(task1)
52-
result_dict1, _ = task1.result()
57+
_result_dict1, _ = task1.result()
5358

5459
response = requests.get("http://localhost:8083/v1/statistics")
5560
res = response.json()
56-
p50 = res["opea_service@s1_add"]["p50_latency"]
57-
p99 = res["opea_service@s1_add"]["p99_latency"]
61+
p50 = res[SVC1]["p50_latency"]
62+
p99 = res[SVC1]["p99_latency"]
5863
self.assertEqual(int(p50), int(p99))
5964

6065

66+
class TestBaseStatisticsLocal(unittest.TestCase):
67+
def test_stats(self):
68+
stats = statistics_dict[SVC2]
69+
70+
stats.append_latency(3)
71+
res = collect_all_statistics()
72+
avg = res[SVC2]["average_latency"]
73+
self.assertIsNotNone(avg)
74+
p99 = res[SVC2]["p99_latency"]
75+
self.assertEqual(int(p99), int(avg))
76+
p50 = res[SVC2]["p50_latency"]
77+
self.assertEqual(int(p99), int(p50))
78+
self.assertIsNone(res[SVC2]["p50_latency_first_token"])
79+
80+
stats.append_latency(2, 1)
81+
res = collect_all_statistics()
82+
avg = res[SVC2]["average_latency_first_token"]
83+
self.assertIsNotNone(avg)
84+
p99 = res[SVC2]["p99_latency_first_token"]
85+
self.assertEqual(int(p99), int(avg))
86+
p50 = res[SVC2]["p50_latency_first_token"]
87+
self.assertEqual(int(p99), int(p50))
88+
89+
stats.append_latency(1)
90+
res = collect_all_statistics()
91+
p50 = res[SVC2]["p50_latency"]
92+
avg = res[SVC2]["average_latency"]
93+
self.assertEqual(int(avg), int(p50))
94+
self.assertEqual(int(p50), 2)
95+
96+
6197
if __name__ == "__main__":
6298
unittest.main()

0 commit comments

Comments
 (0)