From cbeafd53073989e3a33a58349e82d12f63713643 Mon Sep 17 00:00:00 2001 From: saimedhi Date: Tue, 2 Apr 2024 18:23:49 -0700 Subject: [PATCH] updated Signed-off-by: saimedhi --- test_opensearchpy/test_server/test_metrics.py | 76 +++++++++++++++++-- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/test_opensearchpy/test_server/test_metrics.py b/test_opensearchpy/test_server/test_metrics.py index dfca5062..e2b20c89 100644 --- a/test_opensearchpy/test_server/test_metrics.py +++ b/test_opensearchpy/test_server/test_metrics.py @@ -9,17 +9,83 @@ from __future__ import unicode_literals +import time + +from opensearchpy import RequestsHttpConnection from opensearchpy.metrics.metrics_events import MetricsEvents from . import OpenSearchTestCase, get_client -class Metrics(OpenSearchTestCase): - def test_metrics(self) -> None: +class TestMetrics(OpenSearchTestCase): + def tearDown(self) -> None: + client = get_client() + client.indices.delete(index=["test-index"], ignore_unavailable=True) + + def test_metrics_default_behavior(self) -> None: + # Test default behavior when metrics is not passed to the client + client = get_client() + index_name = "test-index" + index_body = {"settings": {"index": {"number_of_shards": 4}}} + response1 = client.indices.create(index=index_name, body=index_body) + metrics = MetricsEvents() + assert metrics.service_time is None + +class TestMetricsEvents(OpenSearchTestCase): + def tearDown(self) -> None: + client = get_client() + client.indices.delete(index=["test-index"], ignore_unavailable=True) + + def test_metrics_events_with_urllib3_connection(self) -> None: + # Test MetricsEvents behavior with urllib3 connection metrics = MetricsEvents() client = get_client(metrics=metrics) + + # Calculate service time for create index operation + index_name = "test-index" + index_body = {"settings": {"index": {"number_of_shards": 4}}} + start1 = time.perf_counter() + response1 = client.indices.create(index=index_name, body=index_body) + duration1 = time.perf_counter() - start1 + assert ( + isinstance(metrics.service_time, float) and metrics.service_time < duration1 + ) + + # Calculate service time for adding document operation + document = {"title": "Moneyball", "director": "Bennett Miller", "year": "2011"} + id = "1" + start2 = time.perf_counter() + response2 = client.index(index=index_name, body=document, id=id, refresh=True) + duration2 = time.perf_counter() - start2 + assert ( + isinstance(metrics.service_time, float) + and metrics.service_time < duration2 + and metrics.service_time != duration1 + ) + + def test_metrics_events_with_requests_http_connection(self) -> None: + # Test MetricsEvents behavior with requests HTTP connection + metrics = MetricsEvents() + client = get_client(metrics=metrics, connection_class=RequestsHttpConnection) + + # Calculate service time for create index operation index_name = "test-index" index_body = {"settings": {"index": {"number_of_shards": 4}}} - client.indices.create(index_name, body=index_body) - print("metrics.service_time", metrics.service_time) - assert 1 == 2 + start1 = time.perf_counter() + response1 = client.indices.create(index_name, body=index_body) + duration1 = time.perf_counter() - start1 + assert ( + isinstance(metrics.service_time, float) and metrics.service_time < duration1 + ) + + # Calculate service time for adding document operation + document = {"title": "Moneyball", "director": "Bennett Miller", "year": "2011"} + id = "1" + start2 = time.perf_counter() + response2 = client.index(index=index_name, body=document, id=id, refresh=True) + duration2 = time.perf_counter() - start2 + assert ( + isinstance(metrics.service_time, float) + and metrics.service_time < duration2 + and metrics.service_time != duration1 + )