Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: instrument timing for slow requests #7217

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0428d89
add a visium dataset to our functional tests
Bento007 May 24, 2024
a44303f
add better names to test collections
Bento007 May 24, 2024
ce91477
fix uploaded dataset
Bento007 May 24, 2024
abd2e50
double timeout
Bento007 May 24, 2024
b965486
Merge branch 'main' into tsmith/visium-functional-tewt
Bento007 May 24, 2024
908e84a
Merge branch 'refs/heads/main' into tsmith/visium-functional-tewt
Bento007 May 24, 2024
5684f07
Merge branch 'main' into tsmith/visium-functional-tewt
kaloster May 28, 2024
b4ac45c
trim dataset
Bento007 May 28, 2024
0418de5
Merge remote-tracking branch 'origin/tsmith/visium-functional-tewt' i…
Bento007 May 28, 2024
1896c08
remove fullres
Bento007 May 28, 2024
2323887
fix dataset
Bento007 May 28, 2024
4ba5a6a
Merge branch 'main' into tsmith/visium-functional-tewt
Bento007 May 28, 2024
4cd4c0b
try running in revision test
Bento007 May 29, 2024
c341ee5
only upload visium dataset in dataset test
Bento007 May 29, 2024
a922dcc
Merge remote-tracking branch 'origin/tsmith/visium-functional-tewt' i…
Bento007 May 29, 2024
27d9ca9
Merge branch 'main' into tsmith/visium-functional-tewt
nayib-jose-gloria Jun 4, 2024
cb9d31a
update test to use pytest fixtures
Bento007 Jun 12, 2024
b213d09
run functional test in parallel.
Bento007 Jun 13, 2024
d58d7c2
fix imports
Bento007 Jun 13, 2024
cc5f742
update tests to pytest
Bento007 Jun 13, 2024
c5db011
verify time it takes for the request to finish
Bento007 Jun 20, 2024
a74ddc7
fix output
Bento007 Jun 20, 2024
4b0a70a
remove default string value
Bento007 Jun 20, 2024
b188007
handle if no records
Bento007 Jun 20, 2024
2355d29
include original request details response log
Bento007 Jun 20, 2024
e380070
Merge branch 'main' into tsmith/7190-get-collection-timeout
Bento007 Jun 26, 2024
6e53c79
Merge branch 'refs/heads/main' into tsmith/pytest-functional
Bento007 Jun 28, 2024
fb9c579
remove visium test
Bento007 Jun 28, 2024
fa0c635
verify time it takes for the request to finish
Bento007 Jun 20, 2024
a66244a
fix output
Bento007 Jun 20, 2024
6162038
remove default string value
Bento007 Jun 20, 2024
dccf7c5
handle if no records
Bento007 Jun 20, 2024
4cfa601
include original request details response log
Bento007 Jun 20, 2024
c4c301f
Merge remote-tracking branch 'origin/tsmith/7190-get-collection-timeo…
Bento007 Jun 28, 2024
4bb6227
manually commit
Bento007 Jun 28, 2024
b881b9f
use a single session per transactions
Bento007 Jun 28, 2024
b451be7
Merge branch 'main' into tsmith/7190-get-collection-timeout
Bento007 Jul 3, 2024
e1f0cd5
remove extra changes
Bento007 Jul 3, 2024
8cbe0e0
remove extra changes
Bento007 Jul 3, 2024
e4123eb
Update backend/common/server/config.py
Bento007 Jul 3, 2024
3366381
add response to request
Bento007 Jul 3, 2024
e7c66b3
undo session work
Bento007 Jul 4, 2024
d7b0505
Merge branch 'main' into tsmith/7190-get-collection-timeout
nayib-jose-gloria Jul 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/common/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def after_request(response: Response):
status_code=response.status_code,
content_length=response.content_length,
response_time=time.time() - g.start,
request=dict(url=request.path, method=request.method),
Bento007 marked this conversation as resolved.
Show resolved Hide resolved
),
)
)
Expand Down
75 changes: 74 additions & 1 deletion backend/common/utils/timer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,89 @@
import logging
import time
from contextlib import contextmanager
from typing import Any, Optional

import numpy as np

logging.basicConfig(level=logging.INFO)

Accumulators: dict[str, Any] = dict()


@contextmanager
def log_time_taken(description: str = "Code block"):
def log_time_taken(
description: str,
):
start_time = time.time()
try:
yield
finally:
end_time = time.time()
elapsed_time = end_time - start_time
logging.info(dict(type="METRIC", details=dict(description=description, time=elapsed_time, unit="seconds")))


class TimeAccumulator:
def __init__(self, group: str):
self.group = group
self.records = []

@contextmanager
def time(
self,
description: Optional[str] = None,
):
start_time = time.time()
try:
yield
finally:
end_time = time.time()
logging.info(
dict(
type="METRIC",
details=dict(description=description, time=end_time - start_time, unit="seconds", group=self.group),
)
)
self.records.append(dict(description=description, elapsed=end_time - start_time))

def todict(self):
records = self.records
if not records:
return dict(
group=self.group,
records=0,
total_time=0,
max_time=0,
min_time=0,
median_time=0,
_95th_percentile=0,
_99th_percentile=0,
unit="seconds",
)
records.sort(key=lambda x: x["elapsed"])
times = [record["elapsed"] for record in records]
self.total_time = sum(times)
self.max_time = max(times)
self.min_time = min(times)
self.median_time = np.median(times)
self._95th_percentile, self._99th_percentile = np.percentile(times, [95, 99])
return dict(
group=self.group,
records=len(records),
total_time=self.total_time,
max_time=self.max_time,
min_time=self.min_time,
median_time=self.median_time,
_95th_percentile=self._95th_percentile,
_99th_percentile=self._99th_percentile,
unit="seconds",
)


@contextmanager
def log_accumulative_time_taken(group: str) -> TimeAccumulator:
accumulator = TimeAccumulator(group)
try:
yield accumulator
finally:
logging.info(dict(type="METRIC", details=accumulator.todict()))
9 changes: 6 additions & 3 deletions backend/curation/api/v1/curation/collections/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import backend.common.doi as doi
from backend.common.utils.http_exceptions import ForbiddenHTTPException, InvalidParametersHTTPException
from backend.common.utils.timer import log_accumulative_time_taken
from backend.curation.api.v1.curation.collections.common import reshape_for_curation_api
from backend.layers.auth.user_info import UserInfo
from backend.layers.business.entities import CollectionQueryFilter
Expand Down Expand Up @@ -42,9 +43,11 @@ def get(visibility: str, token_info: dict, curator: str = None):
logging.info(filters)

resp_collections = []
for collection_version in get_business_logic().get_collections(CollectionQueryFilter(**filters)):
resp_collection = reshape_for_curation_api(collection_version, user_info, preview=True)
resp_collections.append(resp_collection)
with log_accumulative_time_taken("reshape_for_curation_api") as time_accumulator:
for collection_version in get_business_logic().get_collections(CollectionQueryFilter(**filters)):
with time_accumulator.time(f"{collection_version.collection_id.id}"):
resp_collection = reshape_for_curation_api(collection_version, user_info, preview=True)
resp_collections.append(resp_collection)
return jsonify(resp_collections)


Expand Down
Loading