feat: add selectable performance history windows
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import threading
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
@@ -229,6 +231,42 @@ class ValidationHarnessTests(unittest.TestCase):
|
||||
self.assertIn("historical performance graphs are shown in Operations / Performance below", bundle)
|
||||
self.assertIn("setInterval(pollRuntime, 1000)", bundle)
|
||||
|
||||
def test_performance_sample_uses_minute_bucket_and_runtime_values(self):
|
||||
sample = api._performance_sample({
|
||||
"captured_at": 1700000061,
|
||||
"memory_total_bytes": 200,
|
||||
"memory_used_bytes": 100,
|
||||
"swap_total_bytes": 100,
|
||||
"swap_used_bytes": 25,
|
||||
"ollama_model_bytes": 1024 ** 3,
|
||||
"model_memory": [{"name": "model"}],
|
||||
"cpu": {"usage_percent": 12.5, "load_average": [1.25]},
|
||||
"gpu": {"utilization_percent": 40, "gpus": [{"total_bytes": 100, "used_bytes": 50}]},
|
||||
"disk": {"used_percent": 33.3},
|
||||
})
|
||||
self.assertEqual(sample["captured_at"], 1700000040)
|
||||
self.assertEqual(sample["memory_used_percent"], 50.0)
|
||||
self.assertEqual(sample["gpu_vram_used_percent"], 50.0)
|
||||
self.assertEqual(sample["resident_model_count"], 1)
|
||||
|
||||
def test_performance_history_accepts_only_requested_windows(self):
|
||||
for hours in (1, 6, 9, 12, 24):
|
||||
self.assertEqual(api._validate_performance_hours(hours), hours)
|
||||
with self.assertRaises(api.HTTPException) as context:
|
||||
api._validate_performance_hours(2)
|
||||
self.assertEqual(context.exception.status_code, 400)
|
||||
|
||||
def test_performance_history_deduplicates_minute_buckets(self):
|
||||
with tempfile.TemporaryDirectory() as directory, patch.object(api, "_home", return_value=Path(directory)):
|
||||
base = int(time.time()) - 120
|
||||
runtime = {"captured_at": base + 1, "memory_total_bytes": 1, "memory_used_bytes": 1, "cpu": {}, "gpu": {}, "disk": {}, "model_memory": []}
|
||||
first = api._record_performance_sample(runtime)
|
||||
runtime["captured_at"] = base + 2
|
||||
second = api._record_performance_sample(runtime)
|
||||
self.assertEqual(len(first), 1)
|
||||
self.assertEqual(len(second), 1)
|
||||
self.assertEqual(second[0]["captured_at"], ((base + 2) // 60) * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user