Skip to content

Commit

Permalink
Standardize on pytest_benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Jan 6, 2024
1 parent 5ac2bfb commit d72f50c
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 359 deletions.
92 changes: 0 additions & 92 deletions nautilus_trader/test_kit/performance.py

This file was deleted.

3 changes: 1 addition & 2 deletions tests/performance_tests/test_perf_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from nautilus_trader.model.identifiers import Venue
from nautilus_trader.model.objects import Money
from nautilus_trader.persistence.wranglers import QuoteTickDataWrangler
from nautilus_trader.test_kit.performance import PerformanceHarness
from nautilus_trader.test_kit.providers import TestDataProvider
from nautilus_trader.test_kit.providers import TestInstrumentProvider
from nautilus_trader.test_kit.stubs.data import TestDataStubs
Expand All @@ -44,7 +43,7 @@
USDJPY_SIM = TestInstrumentProvider.default_fx_ccy("USD/JPY")


class TestBacktestEnginePerformance(PerformanceHarness):
class TestBacktestEnginePerformance:
@staticmethod
def test_run_with_empty_strategy(benchmark):
def setup():
Expand Down
85 changes: 43 additions & 42 deletions tests/performance_tests/test_perf_correctness.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,46 @@
# -------------------------------------------------------------------------------------------------

from nautilus_trader.core.correctness import PyCondition
from nautilus_trader.test_kit.performance import PerformanceHarness


class TestCorrectnessConditionPerformance(PerformanceHarness):
def test_condition_none(self):
self.benchmark.pedantic(
target=PyCondition.none,
args=(None, "param"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.1μs / 142ns minimum of 100,000 runs @ 1 iteration each run.

def test_condition_true(self):
self.benchmark.pedantic(
target=PyCondition.true,
args=(True, "this should be true"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.1μs / 149ns minimum of 100,000 runs @ 1 iteration each run.

# 100000 iterations @ 12ms with boolean except returning False
# 100000 iterations @ 12ms with void except returning * !

def test_condition_valid_string(self):
self.benchmark.pedantic(
target=PyCondition.valid_string,
args=("abc123", "string_param"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.2μs / 205ns minimum of 100,000 runs @ 1 iteration each run.

def test_condition_type_or_none(self):
self.benchmark.pedantic(
target=PyCondition.type_or_none,
args=("hello", str, "world"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.2μs / 224ns minimum of 100,000 runs @ 1 iteration each run.


def test_condition_none(benchmark):
benchmark.pedantic(
target=PyCondition.none,
args=(None, "param"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.1μs / 142ns minimum of 100,000 runs @ 1 iteration each run.


def test_condition_true(benchmark):
benchmark.pedantic(
target=PyCondition.true,
args=(True, "this should be true"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.1μs / 149ns minimum of 100,000 runs @ 1 iteration each run.

# 100000 iterations @ 12ms with boolean except returning False
# 100000 iterations @ 12ms with void except returning * !


def test_condition_valid_string(benchmark):
benchmark.pedantic(
target=PyCondition.valid_string,
args=("abc123", "string_param"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.2μs / 205ns minimum of 100,000 runs @ 1 iteration each run.


def test_condition_type_or_none(benchmark):
benchmark.pedantic(
target=PyCondition.type_or_none,
args=("hello", str, "world"),
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.2μs / 224ns minimum of 100,000 runs @ 1 iteration each run.
19 changes: 9 additions & 10 deletions tests/performance_tests/test_perf_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
from nautilus_trader.model.identifiers import Symbol
from nautilus_trader.model.identifiers import TraderId
from nautilus_trader.model.identifiers import Venue
from nautilus_trader.test_kit.performance import PerformanceBench


STUB_ORDER_DENIED = OrderDenied(
_STUB_ORDER_DENIED = OrderDenied(
trader_id=TraderId("TRADER-001"),
strategy_id=StrategyId("SCALPER-001"),
instrument_id=InstrumentId(Symbol("BTCUSDT"), Venue("BINANCE")),
Expand All @@ -51,26 +50,26 @@ def stub_order_denied() -> OrderDenied:
)


def test_order_denied_to_dict():
def test_order_denied_to_dict(benchmark):
def call_to_dict() -> None:
OrderDenied.to_dict(STUB_ORDER_DENIED)
OrderDenied.to_dict(_STUB_ORDER_DENIED)

PerformanceBench.profile_function(
benchmark.pedantic(
target=call_to_dict,
runs=100_000,
rounds=100_000,
iterations=1,
)
# ~0.0ms / ~1.8μs / 1841ns minimum of 100,000 runs @ 1 iteration each run.


def test_order_denied_to_dict_then_msgspec_to_json():
def test_order_denied_to_dict_then_msgspec_to_json(benchmark):
def call_to_dict_then_json() -> None:
denied_dict = OrderDenied.to_dict(STUB_ORDER_DENIED)
denied_dict = OrderDenied.to_dict(_STUB_ORDER_DENIED)
msgspec.json.encode(denied_dict)

PerformanceBench.profile_function(
benchmark.pedantic(
target=call_to_dict_then_json,
runs=100_000,
rounds=100_000,
iterations=1,
)
# ~0.0ms / ~2.4μs / 2441ns minimum of 100,000 runs @ 1 iteration each run.
10 changes: 4 additions & 6 deletions tests/performance_tests/test_perf_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
from nautilus_trader.core.message import Event
from nautilus_trader.core.uuid import UUID4
from nautilus_trader.model.events import OrderSubmitted
from nautilus_trader.test_kit.performance import PerformanceHarness


EVENT = Event(UUID4(), 0, 0)


class Experiments:
Expand All @@ -34,7 +30,7 @@ def built_in_arithmetic():
return x


class TestPerformanceExperiments(PerformanceHarness):
class TestPerformanceExperiments:
@staticmethod
def test_builtin_arithmetic(benchmark):
benchmark.pedantic(
Expand All @@ -55,9 +51,11 @@ def test_class_name(benchmark):

@staticmethod
def test_is_instance(benchmark):
event = Event(UUID4(), 0, 0)

benchmark.pedantic(
target=isinstance,
args=(EVENT, OrderSubmitted),
args=(event, OrderSubmitted),
iterations=100_000,
rounds=1,
)
Expand Down
17 changes: 8 additions & 9 deletions tests/performance_tests/test_perf_fill_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,27 @@
# -------------------------------------------------------------------------------------------------

from nautilus_trader.backtest.models import FillModel
from nautilus_trader.test_kit.performance import PerformanceHarness


model = FillModel(
_FILL_MODEL = FillModel(
prob_fill_on_stop=0.95,
prob_fill_on_limit=0.5,
random_seed=42,
)


class TestFillModelPerformance(PerformanceHarness):
def test_is_limit_filled(self):
self.benchmark.pedantic(
target=model.is_limit_filled,
class TestFillModelPerformance:
def test_is_limit_filled(self, benchmark):
benchmark.pedantic(
target=_FILL_MODEL.is_limit_filled,
iterations=100_000,
rounds=1,
)
# ~0.0ms / ~0.1μs / 106ns minimum of 100,000 runs @ 1 iteration each run.

def test_is_stop_filled(self):
self.benchmark.pedantic(
target=model.is_stop_filled,
def test_is_stop_filled(self, benchmark):
benchmark.pedantic(
target=_FILL_MODEL.is_stop_filled,
iterations=100_000,
rounds=1,
)
Expand Down
13 changes: 6 additions & 7 deletions tests/performance_tests/test_perf_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@

from nautilus_trader.model.identifiers import Symbol
from nautilus_trader.model.identifiers import Venue
from nautilus_trader.test_kit.performance import PerformanceBench


def test_symbol_equality():
def test_symbol_equality(benchmark):
symbol = Symbol("AUD/USD")

def symbol_equality() -> bool:
return symbol == symbol

PerformanceBench.profile_function(
benchmark.pedantic(
target=symbol_equality,
runs=1_000_000,
rounds=1_000_000,
iterations=1,
)


def test_venue_equality():
def test_venue_equality(benchmark):
venue = Venue("SIM")

def venue_equality() -> bool:
return venue == venue

PerformanceBench.profile_function(
benchmark.pedantic(
target=venue_equality,
runs=1_000_000,
rounds=1_000_000,
iterations=1,
)
Loading

0 comments on commit d72f50c

Please sign in to comment.