Recipes: Performance measurements#

wait_until#

stbt.wait_until will wait until any condition specified by you. As soon as it returns, use Python’s time.time to calculate the elapsed time:

keypress = stbt.press("KEY_HOME")
assert stbt.wait_until(lambda: XXX)  # Replace "XXX" with your custom expression
end_time = time.time()
duration = end_time - keypress.end_time

If you are waiting for a Page Object to become visible, then you can use the Page Object’s frame timestamp:

keypress = stbt.press("KEY_HOME")
page = stbt.wait_until(Home)  # "Home" is a stbt.FrameObject class
assert page
duration = page._frame.time - keypress.end_time

Logging#

Anything that you print goes to stbt.log. For example, to print the duration calculated above:

print(f"Measurement of XXX: {duration:.2f} seconds")

Any files that you write to the working directory are saved as artifacts of the test-run. For example, to write all your measurements to a json file:

measurements = {
    "XXX": duration,
}
with open("measurements.json", "w") as f:
    json.dump(measurements, f)

press_and_wait#

stbt.press_and_wait uses motion detection: It presses a key, then waits for the screen to change, then waits for it to stop changing.

transition = stbt.press_and_wait("KEY_HOME")
assert transition
print(f"Measurement of Home page load: {transition.duration:.2f} seconds")

You may need to provide a mask parameter to press_and_wait to ignore some areas of the screen. See Regions and Masks.

You should also check that the target screen loaded successfully, to ensure the measurement is valid. For example if Home is a FrameObject class:

transition = stbt.press_and_wait("KEY_HOME")
assert transition
page = Home(transition.frame)
assert page
print(f"Measurement of Home page load: {transition.duration:.2f} seconds")