Skip to content

Commit bc00950

Browse files
authored
Merge pull request #966 from launchableinc/measure-performance
Measure the performance in `launchable record tests` command
2 parents d6ba371 + a1a2cd1 commit bc00950

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

launchable/commands/helper.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from time import time
12
from typing import Optional, Sequence, Tuple
23

34
import click
@@ -137,6 +138,12 @@ def find_or_create_session(
137138
return read_session(saved_build_name)
138139

139140

141+
def time_ns():
142+
# time.time_ns() method is new in Python version 3.7
143+
# As a workaround, we convert time.time() to nanoseconds.
144+
return int(time() * 1e9)
145+
146+
140147
def _check_observation_mode_status(session: str, is_observation: bool,
141148
tracking_client: TrackingClient, app: Optional[Application] = None):
142149
if not is_observation:

launchable/commands/record/tests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ...utils.logger import Logger
2323
from ...utils.no_build import NO_BUILD_BUILD_NAME, NO_BUILD_TEST_SESSION_ID
2424
from ...utils.session import parse_session, read_build
25-
from ..helper import find_or_create_session
25+
from ..helper import find_or_create_session, time_ns
2626
from .case_event import CaseEvent, CaseEventType
2727

2828
GROUP_NAME_RULE = re.compile("^[a-zA-Z0-9][a-zA-Z0-9_-]*$")
@@ -535,14 +535,24 @@ def recorded_result() -> Tuple[int, int, int, float]:
535535
return test_count, success_count, fail_count, duration / 60 # sec to min
536536

537537
try:
538+
start = time_ns()
538539
tc = testcases(self.reports)
540+
end = time_ns()
541+
tracking_client.send_event(
542+
event_name=Tracking.Event.PERFORMANCE,
543+
metadata={
544+
"elapsedTime": end - start,
545+
"measurementTarget": "testcases method(parsing report file)"
546+
}
547+
)
539548

540549
if report_paths:
541550
# diagnostics mode to just report test paths
542551
for t in tc:
543552
print(unparse_test_path(t['testPath']))
544553
return
545554

555+
start = time_ns()
546556
exceptions = []
547557
for chunk in ichunked(tc, post_chunk):
548558
p, es = payload(
@@ -555,6 +565,14 @@ def recorded_result() -> Tuple[int, int, int, float]:
555565

556566
send(p)
557567
exceptions.extend(es)
568+
end = time_ns()
569+
tracking_client.send_event(
570+
event_name=Tracking.Event.PERFORMANCE,
571+
metadata={
572+
"elapsedTime": end - start,
573+
"measurementTarget": "events API"
574+
}
575+
)
558576

559577
if len(exceptions) > 0:
560578
raise Exception(exceptions)

launchable/utils/tracking.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Tracking:
1313
# General events
1414
class Event(Enum):
1515
SHALLOW_CLONE = 'SHALLOW_CLONE' # this event is an example
16+
PERFORMANCE = 'PERFORMANCE'
1617

1718
# Error events
1819
class ErrorEvent(Enum):

tests/commands/test_api_error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def test_all_workflow_when_server_down(self):
451451
result = self.cli("record", "tests", "--session", self.session, "minitest", str(self.test_files_dir) + "/")
452452
self.assert_success(result)
453453
# Since Timeout error is caught inside of LaunchableClient, the tracking event is sent twice.
454-
self.assert_tracking_count(tracking=tracking, count=8)
454+
self.assert_tracking_count(tracking=tracking, count=9)
455455

456456
def assert_tracking_count(self, tracking, count: int):
457457
# Prior to 3.6, `Response` object can't be obtained.

0 commit comments

Comments
 (0)