diff --git a/fusion_accounting_assets/tests/__init__.py b/fusion_accounting_assets/tests/__init__.py index f21110c5..e45db5b4 100644 --- a/fusion_accounting_assets/tests/__init__.py +++ b/fusion_accounting_assets/tests/__init__.py @@ -18,3 +18,4 @@ from . import test_assets_cron from . import test_engine_property from . import test_method_integration from . import test_asset_book_values_mv +from . import test_performance_benchmarks diff --git a/fusion_accounting_assets/tests/test_performance_benchmarks.py b/fusion_accounting_assets/tests/test_performance_benchmarks.py new file mode 100644 index 00000000..5eb267e0 --- /dev/null +++ b/fusion_accounting_assets/tests/test_performance_benchmarks.py @@ -0,0 +1,117 @@ +"""Performance benchmarks tagged 'benchmark'.""" + +import json +import statistics +import time +from datetime import date + +from odoo.tests.common import HttpCase, TransactionCase, new_test_user +from odoo.tests import tagged + + +@tagged('post_install', '-at_install', 'benchmark') +class TestEngineBenchmarks(TransactionCase): + + def setUp(self): + super().setUp() + self.engine = self.env['fusion.asset.engine'] + + def _percentile(self, samples, p): + if len(samples) <= 1: + return samples[0] if samples else 0 + sorted_s = sorted(samples) + idx = int(len(sorted_s) * p / 100) + return sorted_s[min(idx, len(sorted_s) - 1)] + + def test_compute_schedule_p95(self): + timings = [] + for i in range(10): + asset = self.env['fusion.asset'].create({ + 'name': f'PerfAsset{i}', 'cost': 100000, 'salvage_value': 5000, + 'acquisition_date': date(2026, 1, 1), + 'method': 'straight_line', 'useful_life_years': 10, + }) + start = time.perf_counter() + self.engine.compute_depreciation_schedule(asset) + timings.append((time.perf_counter() - start) * 1000) + p95 = self._percentile(timings, 95) + median = statistics.median(timings) + msg = f"compute_schedule(10yr): median={median:.0f}ms p95={p95:.0f}ms" + print(f"\n PERF: {msg} (target <500ms)") + self.assertLess(p95, 5000, f"way over budget: {msg}") + + def test_post_depreciation_p95(self): + asset = self.env['fusion.asset'].create({ + 'name': 'PostPerf', 'cost': 50000, + 'acquisition_date': date(2026, 1, 1), + 'in_service_date': date(2026, 1, 1), + 'method': 'straight_line', 'useful_life_years': 10, + }) + self.engine.compute_depreciation_schedule(asset) + asset.action_set_running() + timings = [] + for _ in range(5): + start = time.perf_counter() + self.engine.post_depreciation_entry(asset) + timings.append((time.perf_counter() - start) * 1000) + p95 = self._percentile(timings, 95) + median = statistics.median(timings) + msg = f"post_depreciation: median={median:.0f}ms p95={p95:.0f}ms" + print(f"\n PERF: {msg} (target <300ms)") + self.assertLess(p95, 3000) + + def test_dispose_asset_p95(self): + timings = [] + for i in range(5): + asset = self.env['fusion.asset'].create({ + 'name': f'DispPerf{i}', 'cost': 10000, + 'acquisition_date': date(2026, 1, 1), + 'in_service_date': date(2026, 1, 1), + 'method': 'straight_line', 'useful_life_years': 5, + }) + self.engine.compute_depreciation_schedule(asset) + asset.action_set_running() + start = time.perf_counter() + self.engine.dispose_asset(asset, sale_amount=5000) + timings.append((time.perf_counter() - start) * 1000) + p95 = self._percentile(timings, 95) + median = statistics.median(timings) + msg = f"dispose_asset: median={median:.0f}ms p95={p95:.0f}ms" + print(f"\n PERF: {msg} (target <300ms)") + self.assertLess(p95, 3000) + + +@tagged('post_install', '-at_install', 'benchmark') +class TestControllerBenchmarks(HttpCase): + + def test_list_endpoint_p95(self): + new_test_user( + self.env, login='asset_perf', + groups='base.group_user,account.group_account_invoice', + ) + for i in range(20): + self.env['fusion.asset'].create({ + 'name': f'ListPerf{i}', 'cost': 1000, + 'acquisition_date': date(2026, 1, 1), + 'method': 'straight_line', 'useful_life_years': 4, + }) + self.authenticate('asset_perf', 'asset_perf') + timings = [] + for _ in range(5): + start = time.perf_counter() + response = self.url_open( + '/fusion/assets/list', + data=json.dumps({ + 'jsonrpc': '2.0', 'method': 'call', 'id': 1, + 'params': {'company_id': self.env.company.id}, + }), + headers={'Content-Type': 'application/json'}, + ) + timings.append((time.perf_counter() - start) * 1000) + self.assertEqual(response.status_code, 200) + sorted_t = sorted(timings) + p95 = sorted_t[min(int(len(sorted_t) * 0.95), len(sorted_t) - 1)] + median = statistics.median(timings) + msg = f"controller.list: median={median:.0f}ms p95={p95:.0f}ms" + print(f"\n PERF: {msg} (target <300ms)") + self.assertLess(p95, 3000)