test(fusion_accounting_assets): performance benchmarks with P95 targets

Made-with: Cursor
This commit is contained in:
gsinghpal
2026-04-19 17:26:01 -04:00
parent fec1c12246
commit 475d17c1aa
2 changed files with 118 additions and 0 deletions

View File

@@ -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

View File

@@ -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)