Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
Signed-off-by: saimedhi <saimedhi@amazon.com>
  • Loading branch information
saimedhi committed Apr 3, 2024
1 parent 77dfd9d commit cbeafd5
Showing 1 changed file with 71 additions and 5 deletions.
76 changes: 71 additions & 5 deletions test_opensearchpy/test_server/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit cbeafd5

Please sign in to comment.