-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_speed.py
More file actions
1649 lines (1330 loc) · 75.9 KB
/
Copy pathtest_speed.py
File metadata and controls
1649 lines (1330 loc) · 75.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Unit tests for the correctness-critical pure logic: progress parsing and the
instantaneous-speed calculation (the thing that must NOT use ffmpeg's cumulative
speed= field). Run: python3 test_speed.py"""
import os
import unittest
import benchmark
from benchmark import (parse_progress_kv, compute_inst_speed, is_session_cap_error,
smbios_mem_speeds, millideg_to_c, estimate_igpu_power,
parse_nvidia_xml, parse_fdinfo, gpu_busy,
parse_amd_engines, engine_pct, clip_master, select_input,
is_video_file, fit_seconds, sample_window,
target_res_options, source_is_10bit, ten_bit_output, is_comparable,
recommended_workers, throughput_saturated,
cpu_baseline_key, baseline_valid, efficiency_ratio, speed_ratio,
compute_vs_cpu, load_baseline, save_baseline,
default_selection, cpu_load_pct,
rapl_delta_uj, rapl_watts, rapl_package_paths,
history_for, run_delta, append_history, load_history, batch_eligible,
batch_skip_reason, _query_str,
load_or_create_install_id, submission_envelope,
death_reason, vram_slope, predict_wall, classify_stop)
class TestParse(unittest.TestCase):
def test_basic(self):
self.assertEqual(parse_progress_kv("out_time_us=5120000"), ("out_time_us", "5120000"))
self.assertEqual(parse_progress_kv("fps=110.5"), ("fps", "110.5"))
self.assertEqual(parse_progress_kv("speed=4.27x"), ("speed", "4.27x"))
def test_whitespace_and_newline(self):
self.assertEqual(parse_progress_kv(" out_time_us = 42 \n"), ("out_time_us", "42"))
def test_value_with_equals(self):
# only split on the first '='
self.assertEqual(parse_progress_kv("tag=a=b"), ("tag", "a=b"))
def test_rejects(self):
self.assertIsNone(parse_progress_kv(""))
self.assertIsNone(parse_progress_kv(" "))
self.assertIsNone(parse_progress_kv("noequalsign"))
class TestSpeed(unittest.TestCase):
def test_realtime(self):
# 1.0s of output produced in 1.0s of wall time => 1.0x
self.assertAlmostEqual(compute_inst_speed(0, 0.0, 1_000_000, 1.0), 1.0)
def test_fast(self):
# 4.0s of output in 1.0s wall => 4.0x (race-ahead, model B)
self.assertAlmostEqual(compute_inst_speed(0, 10.0, 4_000_000, 11.0), 4.0)
def test_below_realtime(self):
# 0.5s of output in 1.0s wall => 0.5x (a failing stream)
self.assertAlmostEqual(compute_inst_speed(2_000_000, 100.0, 2_500_000, 101.0), 0.5)
def test_zero_wall_delta_is_safe(self):
# no wall time elapsed -> 0.0, never divides by zero
self.assertEqual(compute_inst_speed(0, 5.0, 1_000_000, 5.0), 0.0)
def test_negative_wall_delta_is_safe(self):
self.assertEqual(compute_inst_speed(0, 5.0, 1_000_000, 4.0), 0.0)
class TestSessionCap(unittest.TestCase):
def test_detects_nvenc_session_error(self):
self.assertTrue(is_session_cap_error(
"[h264_nvenc @ 0x..] OpenEncodeSessionEx failed: out of memory (10)"))
self.assertTrue(is_session_cap_error("Failed to open encode session"))
def test_ignores_normal_output(self):
self.assertFalse(is_session_cap_error(""))
self.assertFalse(is_session_cap_error(None))
self.assertFalse(is_session_cap_error("frame= 100 fps=120 speed=4.2x"))
class TestSmbios(unittest.TestCase):
def _type17(self, speed, configured, length=0x22):
b = bytearray(length)
b[0] = 17 # Memory Device
b[1] = length
b[0x15] = speed & 0xFF
b[0x16] = (speed >> 8) & 0xFF
b[0x20] = configured & 0xFF
b[0x21] = (configured >> 8) & 0xFF
return bytes(b) + b"\x00\x00" # empty string-set
def test_parses_configured_and_rated(self):
end = b"\x7f\x04\x00\x00\x00\x00" # end-of-table
data = self._type17(2667, 2133) + end
self.assertEqual(smbios_mem_speeds(data), (2133, 2667))
def test_takes_max_across_dimms(self):
end = b"\x7f\x04\x00\x00\x00\x00"
data = self._type17(2667, 2133) + self._type17(2667, 2133) + end
self.assertEqual(smbios_mem_speeds(data), (2133, 2667))
def test_empty(self):
self.assertEqual(smbios_mem_speeds(b""), (None, None))
def _type17_typed(self, mem_type, length=0x22):
b = bytearray(length)
b[0] = 17
b[1] = length
b[0x12] = mem_type
return bytes(b) + b"\x00\x00"
def test_mem_type_ddr4_and_ddr5(self):
# smbios_mem_type mints the "(DDR4)"/"(DDR5)" leaderboard ENTITY suffix — a silent
# regression here would mis-bucket every iGPU/CPU submission on the board
end = b"\x7f\x04\x00\x00\x00\x00"
self.assertEqual(benchmark.smbios_mem_type(self._type17_typed(0x1A) + end), "DDR4")
self.assertEqual(benchmark.smbios_mem_type(self._type17_typed(0x22) + end), "DDR5")
def test_mem_type_unknown_code_and_empty(self):
end = b"\x7f\x04\x00\x00\x00\x00"
self.assertIsNone(benchmark.smbios_mem_type(self._type17_typed(0x99) + end))
self.assertIsNone(benchmark.smbios_mem_type(b""))
def test_mem_type_skips_non_17_records(self):
end = b"\x7f\x04\x00\x00\x00\x00"
other = bytes([4, 0x20]) + bytes(0x1E) + b"\x00\x00" # a Type 4 (CPU) record first
self.assertEqual(benchmark.smbios_mem_type(other + self._type17_typed(0x22) + end), "DDR5")
class TestTemp(unittest.TestCase):
def test_millideg(self):
self.assertEqual(millideg_to_c("36000"), 36.0)
self.assertEqual(millideg_to_c(45500), 45.5)
def test_millideg_bad(self):
self.assertIsNone(millideg_to_c(None))
self.assertIsNone(millideg_to_c("N/A"))
class TestPowerEst(unittest.TestCase):
def test_delta(self):
self.assertEqual(estimate_igpu_power(42.0, 5.0), 37.0)
def test_clamps_zero(self):
self.assertEqual(estimate_igpu_power(4.0, 5.0), 0.0)
def test_none(self):
self.assertIsNone(estimate_igpu_power(None, 5.0))
self.assertIsNone(estimate_igpu_power(42.0, None))
NV_XML = """<?xml version="1.0" ?><nvidia_smi_log><gpu>
<utilization><gpu_util>55 %</gpu_util><encoder_util>96 %</encoder_util><decoder_util>40 %</decoder_util></utilization>
<temperature><gpu_temp>46 C</gpu_temp></temperature>
<gpu_power_readings><power_draw>149.7 W</power_draw><current_power_limit>575.0 W</current_power_limit></gpu_power_readings>
<clocks><sm_clock>2850 MHz</sm_clock></clocks>
<clocks_event_reasons><clocks_event_reason_sw_thermal_slowdown>Not Active</clocks_event_reason_sw_thermal_slowdown></clocks_event_reasons>
<processes><process_info><pid>1</pid><process_name>/usr/bin/ffmpeg</process_name></process_info>
<process_info><pid>2</pid><process_name>emby</process_name></process_info></processes>
</gpu></nvidia_smi_log>"""
class TestNvXml(unittest.TestCase):
def test_parse(self):
d = parse_nvidia_xml(NV_XML)
self.assertEqual(d["util"], 55.0)
self.assertEqual(d["enc"], 96.0)
self.assertEqual(d["dec"], 40.0)
self.assertEqual(d["temp"], 46.0)
self.assertAlmostEqual(d["power"], 149.7)
self.assertEqual(d["clock"], 2850.0)
self.assertFalse(d["throttle"])
self.assertIn("emby", d["procs"])
def test_parse_empty(self):
self.assertEqual(parse_nvidia_xml("not xml"), {})
FDINFO = ("pos:\t0\nflags:\t02\ndrm-driver:\ti915\ndrm-pdev:\t0000:00:02.0\n"
"drm-client-id:\t12\ndrm-engine-video:\t4902815164 ns\n")
class TestFdinfo(unittest.TestCase):
def test_parse(self):
d = parse_fdinfo(FDINFO)
self.assertEqual(d["driver"], "i915")
self.assertEqual(d["pdev"], "0000:00:02.0")
self.assertGreater(d["video_ns"], 0)
def test_not_drm(self):
self.assertEqual(parse_fdinfo("pos:\t0\nflags:\t02\n"), {})
class TestBusy(unittest.TestCase):
def test_busy_from_engines(self):
with benchmark.TELE_LOCK:
benchmark.TELEMETRY.clear()
benchmark.TELEMETRY["engines"] = {"Video": 40.0, "Render/3D": 0.0}
busy, load = gpu_busy({"vendor": "intel"})
self.assertTrue(busy)
self.assertEqual(load, 40.0)
def test_idle_not_busy(self):
with benchmark.TELE_LOCK:
benchmark.TELEMETRY.clear()
benchmark.TELEMETRY["engines"] = {"Video": 2.0}
busy, load = gpu_busy({"vendor": "intel"})
self.assertFalse(busy)
# Real fdinfo of a transcoding ffmpeg on an RX 9070 XT (Navi 48), captured live.
AMD_FD = ("drm-driver:\tamdgpu\n"
"drm-pdev:\t0000:03:00.0\n"
"drm-client-id:\t94\n"
"drm-engine-compute:\t257845131 ns\n"
"drm-engine-enc:\t2973192506 ns\n"
"drm-engine-dec:\t1000000000 ns\n"
"drm-engine-capacity-enc:\t1\n")
class TestAmdEngines(unittest.TestCase):
def test_parse(self):
d = parse_amd_engines(AMD_FD)
self.assertEqual(d["enc_ns"], 2973192506)
self.assertEqual(d["dec_ns"], 1000000000)
self.assertEqual(d["comp_ns"], 257845131) # scale_vaapi VPP runs on the compute ring
self.assertEqual(d["enc_cap"], 1)
self.assertEqual(d["pdev"], "0000:03:00.0")
self.assertEqual(d["client"], "94")
def test_not_amdgpu(self):
# an i915 (Intel) DRM fd is not an amdgpu client
self.assertEqual(parse_amd_engines("drm-driver:\ti915\ndrm-engine-video:\t5 ns\n"), {})
def test_not_drm(self):
self.assertEqual(parse_amd_engines("pos:\t0\nflags:\t02\n"), {})
def test_missing_decode_defaults_zero(self):
# an encode-only client has no drm-engine-dec line
d = parse_amd_engines("drm-driver:\tamdgpu\ndrm-engine-enc:\t5 ns\n")
self.assertEqual(d["dec_ns"], 0)
self.assertEqual(d["enc_cap"], 1) # capacity defaults to 1 when absent
class TestEnginePct(unittest.TestCase):
def test_full_busy(self):
# 1.0s of engine work over a 1.0s wall window on a single-instance engine = 100%
self.assertEqual(engine_pct(1_000_000_000, 1.0, 1), 100.0)
def test_half_busy(self):
self.assertEqual(engine_pct(500_000_000, 1.0, 1), 50.0)
def test_clamps_to_100(self):
# summed work can momentarily exceed the window; occupancy never exceeds 100%
self.assertEqual(engine_pct(3_000_000_000, 1.0, 1), 100.0)
def test_capacity_halves(self):
# two engine instances → the same ns is half the aggregate occupancy
self.assertEqual(engine_pct(1_000_000_000, 1.0, 2), 50.0)
def test_zero_and_negative_dt_safe(self):
self.assertIsNone(engine_pct(100, 0.0, 1))
self.assertIsNone(engine_pct(100, -1.0, 1))
class TestAbort(unittest.TestCase):
"""abort_run() must only fire mid-run (preparing/running) and must set the abort flag."""
def _set_ui(self, ui):
with benchmark.STATE_LOCK:
benchmark.STATE["ui"] = ui
def test_no_abort_when_idle(self):
benchmark._ABORT.clear()
self._set_ui("idle")
self.assertFalse(benchmark.abort_run())
self.assertFalse(benchmark._ABORT.is_set())
def test_no_abort_when_done(self):
benchmark._ABORT.clear()
self._set_ui("done")
self.assertFalse(benchmark.abort_run())
self.assertFalse(benchmark._ABORT.is_set())
def test_abort_when_running(self):
benchmark._ABORT.clear()
self._set_ui("running")
self.assertTrue(benchmark.abort_run())
self.assertTrue(benchmark._ABORT.is_set())
def test_abort_when_preparing(self):
benchmark._ABORT.clear()
self._set_ui("preparing")
self.assertTrue(benchmark.abort_run())
self.assertTrue(benchmark._ABORT.is_set())
class TestClipMaster(unittest.TestCase):
def test_paths(self):
self.assertTrue(clip_master("4k", "hevc").endswith("source_4k_hevc.mkv"))
self.assertTrue(clip_master("1080p", "h264").endswith("source_1080p_h264.mkv"))
class TestInputGating(unittest.TestCase):
"""select_input() must only accept a source codec the selected GPU can hardware-decode,
and only while idle — this is the gate that stops a CPU-fallback benchmark."""
def _setup(self, decodes, ui="idle"):
benchmark._DETECTED = [{"idx": 0, "available": True, "name": "X",
"decodes": decodes, "codecs": ["h264"]}]
with benchmark.STATE_LOCK:
benchmark.STATE["ui"] = ui
benchmark.STATE["selected_idx"] = 0
benchmark.STATE["selected_input"] = "hevc"
def test_accepts_decodable(self):
self._setup(["hevc", "av1"])
self.assertTrue(select_input("av1"))
with benchmark.STATE_LOCK:
self.assertEqual(benchmark.STATE["selected_input"], "av1")
def test_rejects_undecodable(self):
self._setup(["hevc"]) # GPU can't hw-decode AV1
self.assertFalse(select_input("av1"))
with benchmark.STATE_LOCK:
self.assertEqual(benchmark.STATE["selected_input"], "hevc") # unchanged
def test_rejects_when_not_idle(self):
self._setup(["hevc", "av1"], ui="running")
self.assertFalse(select_input("av1"))
class TestVideoFile(unittest.TestCase):
def test_accepts_video(self):
for n in ("a.mkv", "B.MP4", "show.mov", "x.ts", "y.m4v", "z.webm", "w.avi"):
self.assertTrue(is_video_file(n), n)
def test_rejects_nonvideo(self):
for n in (".hidden.mkv", "poster.jpg", "notes.txt", "a.nfo", "subs.srt", "folder"):
self.assertFalse(is_video_file(n), n)
class TestFitSeconds(unittest.TestCase):
BUDGET = 480 * 1024 * 1024 # ~480 MB usable of a 512 MiB ramdisk
def test_low_bitrate_keeps_full_window(self):
# 40 Mbit · 60 s = 300 MB < budget → full 60 s
self.assertEqual(fit_seconds(40_000_000, self.BUDGET, want=60), 60.0)
def test_high_bitrate_trims(self):
# 100 Mbit → 60 s would be 750 MB; must trim below 60
self.assertLess(fit_seconds(100_000_000, self.BUDGET, want=60), 60.0)
def test_extreme_bitrate_returns_tiny_not_floored(self):
# a 10 Gbit stream fits almost nothing; fit_seconds returns the true (tiny) value so
# the caller can refuse, rather than forcing a floor that would overflow the RAM disk
self.assertLess(fit_seconds(10_000_000_000, self.BUDGET, want=60), 1.0)
def test_unknown_bitrate_defaults_full(self):
self.assertEqual(fit_seconds(None, self.BUDGET, want=60), 60.0)
self.assertEqual(fit_seconds(0, self.BUDGET, want=60), 60.0)
class TestSampleWindow(unittest.TestCase):
def test_long_file_centres(self):
# 2-hour movie → 60 s centred on the midpoint (3600 s), i.e. start at 3570
start, length = sample_window(7200.0, want=60)
self.assertEqual(length, 60)
self.assertAlmostEqual(start, 3570.0, places=1)
def test_short_file_uses_whole(self):
start, length = sample_window(45.0, want=60)
self.assertEqual(start, 0.0)
self.assertIsNone(length) # None length ⇒ copy the whole file
def test_none_duration_uses_whole(self):
self.assertEqual(sample_window(None, want=60), (0.0, None))
class TestTranscodeCmd(unittest.TestCase):
"""BOTH the CUDA and VAAPI paths need -noautoscale or scale_cuda/scale_vaapi fails to re-init
at the -stream_loop seam (an 8-bit H.264 source dies on the 2nd pass otherwise — verified live
on the RTX 5090 and the UHD 770 iGPU). It's an OUTPUT option, so it must come after -i."""
def test_nvenc_noautoscale_after_input(self):
cmd = benchmark.transcode_cmd({"api": "nvenc", "index": 0}, "/x.mkv", "hevc")
self.assertIn("-noautoscale", cmd)
self.assertGreater(cmd.index("-noautoscale"), cmd.index("-i"))
def test_vaapi_noautoscale_after_input(self):
cmd = benchmark.transcode_cmd(
{"api": "vaapi", "device": "/dev/dri/renderD129", "vendor": "intel"}, "/x.mkv", "h264")
self.assertIn("scale_vaapi=w=1920:h=1080:format=nv12", cmd)
self.assertIn("h264_vaapi", cmd)
self.assertIn("-noautoscale", cmd)
self.assertGreater(cmd.index("-noautoscale"), cmd.index("-i"))
def test_cpu_software_path(self):
# CPU device: pure software (no -hwaccel), libx265, named preset, software scale
cmd = benchmark.transcode_cmd(
{"api": "software", "vendor": "cpu"}, "/x.mkv", "hevc", "1080p", False, "medium")
self.assertNotIn("-hwaccel", cmd)
self.assertNotIn("-noautoscale", cmd) # loop-seam bug is hwaccel-only
self.assertIn("libx265", cmd)
self.assertIn("scale=1920:1080", cmd)
self.assertEqual(cmd[cmd.index("-preset") + 1], "medium")
def test_cpu_av1_numeric_preset(self):
# libsvtav1 needs a NUMBER, not a named preset
cmd = benchmark.transcode_cmd(
{"api": "software", "vendor": "cpu"}, "/x.mkv", "av1", "1080p", False, "veryfast")
self.assertIn("libsvtav1", cmd)
self.assertEqual(cmd[cmd.index("-preset") + 1], "10") # veryfast → svt 10
class TestResolution(unittest.TestCase):
def test_no_upscaling(self):
# target must be <= source resolution
self.assertEqual(target_res_options("4k"), ["4k", "1080p", "720p"])
self.assertEqual(target_res_options("1080p"), ["1080p", "720p"])
def test_source_is_10bit(self):
# the shipped 10-bit clips are the HEVC/AV1 masters; H.264 is 8-bit
self.assertTrue(source_is_10bit("hevc"))
self.assertTrue(source_is_10bit("av1"))
self.assertFalse(source_is_10bit("h264"))
def test_ten_bit_output_only_when_preserving(self):
# keep 4K + 10-bit source + HEVC/AV1 out ⇒ preserve 10-bit
self.assertTrue(ten_bit_output("4k", "4k", "hevc", "av1"))
self.assertTrue(ten_bit_output("4k", "4k", "av1", "hevc"))
# downscale ⇒ 8-bit (accepting a lighter version)
self.assertFalse(ten_bit_output("4k", "1080p", "hevc", "av1"))
# H.264 output has no hardware 10-bit ⇒ 8-bit
self.assertFalse(ten_bit_output("4k", "4k", "hevc", "h264"))
# 8-bit source (H.264) ⇒ nothing to preserve
self.assertFalse(ten_bit_output("4k", "4k", "h264", "av1"))
def test_is_comparable_only_canonical(self):
# only 4K -> 1080p is leaderboard-comparable; everything else is local-only
self.assertTrue(is_comparable("4k", "1080p"))
self.assertFalse(is_comparable("4k", "4k"))
self.assertFalse(is_comparable("1080p", "1080p"))
self.assertFalse(is_comparable("1080p", "720p"))
class TestConversionAdvice(unittest.TestCase):
LEVELS = [{"n": 1, "combined": 6.0}, {"n": 2, "combined": 9.5},
{"n": 3, "combined": 10.0}, {"n": 4, "combined": 10.1}]
def test_recommended_workers_fastest(self):
# recommend the worker count with the HIGHEST combined throughput (peak, n=4)
self.assertEqual(recommended_workers(self.LEVELS, 10.1), 4)
def test_recommended_workers_single_when_flat(self):
# already saturated at 1 stream → recommend 1
self.assertEqual(recommended_workers([{"n": 1, "combined": 8.0}], 8.0), 1)
def test_recommended_workers_single_when_declining(self):
# the UHD 770 case: 2 streams do LESS combined work than 1 (one engine, already
# saturated + context-switch overhead) → the fastest is 1 worker
self.assertEqual(
recommended_workers([{"n": 1, "combined": 4.61}, {"n": 2, "combined": 3.76}], 4.61), 1)
def test_recommended_workers_capped(self):
# a driver session cap below the fastest level clamps the recommendation
self.assertEqual(recommended_workers(self.LEVELS, 10.1, cap=2), 2)
def test_throughput_saturated(self):
# a level gaining <5% over the best-so-far means the engine has plateaued
self.assertTrue(throughput_saturated(10.2, 10.0)) # +2% → saturated
self.assertFalse(throughput_saturated(9.5, 6.0)) # +58% → keep ramping
self.assertTrue(throughput_saturated(9.9, 10.0)) # declined → saturated
class TestCpuBaseline(unittest.TestCase):
def test_key(self):
self.assertEqual(cpu_baseline_key("convert", "1080p H264 -> 1080p HEVC"),
"convert|1080p H264 -> 1080p HEVC")
def test_baseline_valid(self):
entry = {"cpu_model": "i9-13900K", "tool_version": "1.0"}
self.assertTrue(baseline_valid(entry, "i9-13900K", "1.0"))
self.assertTrue(baseline_valid(entry, "i9-13900K", "1.4")) # same major → compatible
self.assertFalse(baseline_valid(entry, "Ryzen 9", "1.0")) # different CPU
self.assertFalse(baseline_valid(entry, "i9-13900K", "2.0")) # major bump → stale
self.assertFalse(baseline_valid(None, "i9-13900K", "1.0"))
self.assertFalse(baseline_valid({}, "i9-13900K", "1.0"))
def test_efficiency_ratio(self):
self.assertEqual(efficiency_ratio(120.0, 8.0), 15.0) # CPU 120 W/stream vs GPU 8 → 15×
self.assertIsNone(efficiency_ratio(120.0, 0))
self.assertIsNone(efficiency_ratio(None, 8.0))
def test_speed_ratio(self):
self.assertEqual(speed_ratio(21.4, 4.6), 4.7) # GPU 21.4× vs CPU 4.6× per file
self.assertIsNone(speed_ratio(10, 0)) # CPU sustained 0 streams
self.assertIsNone(speed_ratio(10, None))
def test_compute_vs_cpu_conversion(self):
gpu = {"single_stream": 21.4, "max_sustained": 0, "watts_per_stream": 8.0,
"peak_power_w": 40.0}
cpu = {"single_stream": 4.6, "max_sustained": 0, "watts_per_stream": 120.0,
"peak_power_w": 130.0, "preset": "medium", "encoder": "libx265"}
v = compute_vs_cpu("convert", gpu, cpu, is_dgpu=True)
self.assertEqual(v["efficiency"], 15.0)
self.assertEqual(v["speed"], 4.7)
self.assertEqual(v["speed_kind"], "perfile")
self.assertTrue(v["dgpu_caveat"])
self.assertEqual(v["watts_cpu"], 130.0)
# watts_per_stream = W ÷ throughput(×realtime) which IS Wh per hour of video —
# do NOT divide by speed again (the original double-division bug)
self.assertEqual(v["energy_gpu"], 8.0)
self.assertEqual(v["energy_cpu"], 120.0)
def test_compute_vs_cpu_streaming_zero(self):
# CPU can't sustain even one stream at realtime → speed is None, flagged
gpu = {"single_stream": 7.3, "max_sustained": 6, "watts_per_stream": 3.0,
"peak_power_w": 12.0}
cpu = {"single_stream": 0.4, "max_sustained": 0, "watts_per_stream": 60.0,
"peak_power_w": 95.0, "preset": "veryfast", "encoder": "libx264"}
v = compute_vs_cpu("streaming", gpu, cpu, is_dgpu=False)
self.assertEqual(v["speed_kind"], "streams")
self.assertIsNone(v["speed"]) # cpu max_sustained 0 → no ratio
self.assertFalse(v["cpu_could_sustain"])
self.assertFalse(v["dgpu_caveat"])
def test_load_save_baseline_roundtrip(self):
import tempfile, os
with tempfile.TemporaryDirectory() as d:
p = os.path.join(d, "cpu_baseline.json")
self.assertEqual(load_baseline(p), {}) # missing file → {}
entry = {"cpu_model": "x", "single_stream": 4.6}
self.assertTrue(save_baseline(p, "convert|A -> B", entry))
self.assertTrue(save_baseline(p, "streaming|A -> B", {"single_stream": 1.0}))
got = load_baseline(p)
self.assertEqual(got["convert|A -> B"], entry) # both keys coexist (no overwrite)
self.assertIn("streaming|A -> B", got)
def test_save_baseline_unwritable(self):
# no such directory → degrade to False, never raise
self.assertFalse(save_baseline("/no/such/dir/x.json", "k", {"a": 1}))
class TestDefaultSelection(unittest.TestCase):
GPU = {"idx": 0, "vendor": "intel", "available": True}
NV = {"idx": 1, "vendor": "nvidia", "available": True}
CPU = {"idx": 2, "vendor": "cpu", "available": True}
def test_single_hw_gpu_wins_over_cpu(self):
# the CPU device is always present — it must not break single-GPU auto-select
self.assertEqual(default_selection([self.GPU, self.CPU]), self.GPU)
def test_two_hw_gpus_no_auto(self):
self.assertIsNone(default_selection([self.GPU, self.NV, self.CPU]))
def test_cpu_only_box_selects_cpu(self):
self.assertEqual(default_selection([self.CPU]), self.CPU)
def test_empty(self):
self.assertIsNone(default_selection([]))
class TestHistory(unittest.TestCase):
E = [
{"ts": 3, "gpu": "UHD 770", "mode": "streaming", "profile": "A", "max_sustained": 5,
"single_stream": 2.0, "ram_speed": "DDR4-3200"},
{"ts": 2, "gpu": "UHD 770", "mode": "streaming", "profile": "A", "max_sustained": 3,
"single_stream": 1.9, "ram_speed": "DDR4-2133"},
{"ts": 1, "gpu": "RTX 5090", "mode": "streaming", "profile": "A", "max_sustained": 12},
{"ts": 0, "gpu": "UHD 770", "mode": "convert", "profile": "A", "single_stream": 4.4},
]
def test_history_for_filters_and_sorts_newest_first(self):
got = history_for(self.E, "UHD 770", "streaming", "A")
self.assertEqual([e["ts"] for e in got], [3, 2])
def test_history_for_limit(self):
got = history_for(self.E, "UHD 770", "streaming", "A", limit=1)
self.assertEqual(len(got), 1)
def test_run_delta_streaming(self):
d = run_delta({"mode": "streaming", "max_sustained": 3},
{"mode": "streaming", "max_sustained": 5})
self.assertEqual((d["metric"], d["prev"], d["cur"]), ("streams", 3, 5))
self.assertEqual(d["pct"], 67) # +67%
def test_run_delta_convert(self):
d = run_delta({"mode": "convert", "single_stream": 4.0},
{"mode": "convert", "single_stream": 3.0})
self.assertEqual((d["metric"], d["pct"]), ("perfile", -25))
def test_run_delta_guards(self):
self.assertIsNone(run_delta(None, {"mode": "streaming", "max_sustained": 5}))
# previous headline 0 → no ratio
d = run_delta({"mode": "streaming", "max_sustained": 0},
{"mode": "streaming", "max_sustained": 2})
self.assertIsNone(d["pct"])
self.assertEqual(d["prev"], 0)
def test_append_history_caps_and_persists(self):
import tempfile, os
with tempfile.TemporaryDirectory() as d:
p = os.path.join(d, "history.json")
self.assertEqual(load_history(p), [])
for i in range(7):
self.assertTrue(append_history(p, {"ts": i}, cap=5))
got = load_history(p)
self.assertEqual(len(got), 5) # capped
self.assertEqual([e["ts"] for e in got], [2, 3, 4, 5, 6]) # oldest dropped
def test_append_history_unwritable(self):
self.assertFalse(append_history("/no/such/dir/h.json", {"ts": 1}))
class TestBatchEligible(unittest.TestCase):
G = {"available": True, "decodes": ["h264", "hevc"], "codecs": ["h264", "hevc"]}
def test_eligible(self):
self.assertTrue(batch_eligible(self.G, "hevc", "h264"))
def test_cannot_decode(self):
self.assertFalse(batch_eligible(self.G, "av1", "h264"))
def test_cannot_encode(self):
self.assertFalse(batch_eligible(self.G, "hevc", "av1"))
def test_unavailable(self):
g = dict(self.G, available=False)
self.assertFalse(batch_eligible(g, "hevc", "h264"))
def test_skip_reason(self):
# a skipped device gets a human-readable reason for the comparison table
self.assertIsNone(batch_skip_reason(self.G, "hevc", "h264"))
self.assertEqual(batch_skip_reason(self.G, "hevc", "av1"), "can't encode AV1")
self.assertEqual(batch_skip_reason(self.G, "av1", "h264"), "can't decode AV1")
self.assertEqual(batch_skip_reason(dict(self.G, available=False), "hevc", "h264"),
"not testable")
class TestLimitClassification(unittest.TestCase):
def test_death_reason_session(self):
self.assertEqual(death_reason("OpenEncodeSessionEx failed: out of memory (10)"), "session")
self.assertEqual(death_reason("No capable devices: maximum number of encode sessions"),
"session")
def test_death_reason_memory(self):
# the REAL signature from the patched-5090 VRAM wall (scale_cuda alloc failure)
self.assertEqual(death_reason("Error while filtering: Cannot allocate memory"), "memory")
def test_death_reason_unknown(self):
self.assertIsNone(death_reason("some novel driver error"))
self.assertIsNone(death_reason(""))
self.assertIsNone(death_reason(None))
def test_vram_slope_median_of_increments(self):
# the measured 5090 shape: metronomic ~1415 MiB per session (level-1 absolute unused)
samples = [(1, 1414), (2, 2827), (3, 4261), (4, 5674), (5, 7091)]
self.assertAlmostEqual(vram_slope(samples), 1415, delta=5)
def test_vram_slope_skips_gaps_and_needs_two_increments(self):
self.assertIsNone(vram_slope([(1, 1400)]))
self.assertIsNone(vram_slope([(1, 1400), (2, 2800)])) # one increment isn't a median
# non-consecutive levels normalise per session
self.assertAlmostEqual(vram_slope([(2, 2800), (4, 5600), (6, 8400)]), 1400, delta=1)
def test_vram_slope_robust_to_one_outlier(self):
samples = [(1, 1400), (2, 2800), (3, 4200), (4, 9000), (5, 10400)]
# increments: 1400, 1400, 4800, 1400 → median 1400 (the co-tenant blip is ignored)
self.assertAlmostEqual(vram_slope(samples), 1400, delta=1)
def test_predict_wall(self):
# at N=5 with 18.4 GB free and 1.415 GB/session → 5 + 13 = 18 (the real 5090 case)
self.assertEqual(predict_wall(5, 18400, 1415), 18)
self.assertIsNone(predict_wall(5, 18400, 0))
self.assertIsNone(predict_wall(5, None, 1415))
def test_classify_throughput(self):
self.assertEqual(classify_stop(False, None, 14, None), "throughput")
def test_classify_session(self):
self.assertEqual(classify_stop(True, "session", 13, None), "session")
def test_classify_memory_by_signature(self):
self.assertEqual(classify_stop(True, "memory", 19, None), "memory")
def test_classify_memory_by_prediction_agreement(self):
# no recognised signature, but the VRAM prediction said the wall was here (±1)
self.assertEqual(classify_stop(True, None, 19, 18), "memory")
self.assertEqual(classify_stop(True, None, 18, 18), "memory")
def test_classify_unknown(self):
# hard death, no signature, prediction says the wall was much further away
self.assertEqual(classify_stop(True, None, 14, 18), "unknown")
self.assertEqual(classify_stop(True, None, 14, None), "unknown")
class TestSubmission(unittest.TestCase):
def test_install_id_created_and_stable(self):
import tempfile, os, uuid
with tempfile.TemporaryDirectory() as d:
p = os.path.join(d, "install_id")
a = load_or_create_install_id(p)
uuid.UUID(a) # valid uuid4
self.assertEqual(load_or_create_install_id(p), a) # persisted → stable
def test_install_id_unwritable_still_returns_id(self):
import uuid
a = load_or_create_install_id("/no/such/dir/install_id")
uuid.UUID(a) # ephemeral but usable — never raises
def test_envelope(self):
result = {"tool_version": "1.0", "comparable": True, "max_sustained": 7}
env = submission_envelope(result, "abc-123", ts=1783100000)
self.assertEqual(env["schema"], 1)
self.assertEqual(env["install_id"], "abc-123")
self.assertEqual(env["submitted_at"], 1783100000)
self.assertEqual(env["result"]["max_sustained"], 7)
class TestQueryDecode(unittest.TestCase):
def test_url_decoding(self):
# real media filenames have spaces/parens — encodeURIComponent sends %20 etc., and the
# server must DECODE before matching (the bug that made custom files unselectable)
self.assertEqual(_query_str("/custom?f=Aliens%20(1986)%20Remux-2160p.mkv", "f"),
"Aliens (1986) Remux-2160p.mkv")
self.assertEqual(_query_str("/codec?c=h264", "c"), "h264")
self.assertIsNone(_query_str("/custom", "f"))
self.assertEqual(_query_str("/custom?f=", "f"), "")
class TestRapl(unittest.TestCase):
def test_delta_monotonic(self):
self.assertEqual(rapl_delta_uj(1_000_000, 3_500_000, 262_143_328_850), 2_500_000)
def test_delta_wraparound(self):
# counter wrapped at max_energy_range_uj (5950X: ~65.5e9): cur < prev
self.assertEqual(rapl_delta_uj(65_532_610_000, 987, 65_532_610_987), 1974)
def test_delta_guards(self):
self.assertIsNone(rapl_delta_uj(None, 5, 100))
self.assertIsNone(rapl_delta_uj(5, None, 100))
self.assertIsNone(rapl_delta_uj(10, 5, None)) # wrapped but range unknown → can't tell
def test_watts(self):
# 25,000,000 µJ over 2 s = 12.5 W
self.assertEqual(rapl_watts(25_000_000, 2.0), 12.5)
self.assertIsNone(rapl_watts(None, 2.0))
self.assertIsNone(rapl_watts(1000, 0))
def test_package_discovery(self):
import tempfile, os
with tempfile.TemporaryDirectory() as d:
def mk(rel, name):
p = os.path.join(d, rel)
os.makedirs(p, exist_ok=True)
with open(os.path.join(p, "name"), "w") as f:
f.write(name + "\n")
mk("intel-rapl/intel-rapl:0", "package-0") # keep
mk("intel-rapl/intel-rapl:1", "psys") # skip (not a package)
mk("intel-rapl/intel-rapl:0/intel-rapl:0:0", "core") # nested subdomain: not matched
mk("intel-rapl-mmio/intel-rapl-mmio:0", "package-0") # mmio duplicate: not matched
got = rapl_package_paths(d)
self.assertEqual([os.path.basename(p) for p in got], ["intel-rapl:0"])
def test_package_discovery_missing_root(self):
self.assertEqual(rapl_package_paths("/no/such/powercap"), [])
class TestCpuLoadPct(unittest.TestCase):
def test_basic(self):
# 1-min loadavg 8 on a 32-thread box = 25% busy
self.assertEqual(cpu_load_pct(8.0, 32), 25.0)
self.assertEqual(cpu_load_pct(0.5, 8), 6.2)
def test_guards(self):
self.assertIsNone(cpu_load_pct(None, 8))
self.assertIsNone(cpu_load_pct(1.0, 0))
self.assertEqual(cpu_load_pct(64.0, 16), 100.0) # clamped
class TestProcStatCpu(unittest.TestCase):
"""Instantaneous whole-box CPU busy% from /proc/stat deltas — the live tile for CPU-device
runs (loadavg is a 1-minute average, far too sluggish for a 1 Hz display)."""
STAT_A = "cpu 100 0 50 800 50 0 0 0 0 0\ncpu0 25 0 12 200 12 0 0 0 0 0\n"
STAT_B = "cpu 160 0 90 850 50 0 0 0 0 0\ncpu0 40 0 22 212 12 0 0 0 0 0\n"
def test_parse(self):
# busy = total - idle - iowait = 1000 - 800 - 50
self.assertEqual(benchmark.parse_proc_stat(self.STAT_A), (150, 1000))
def test_parse_bad(self):
self.assertIsNone(benchmark.parse_proc_stat(""))
self.assertIsNone(benchmark.parse_proc_stat(None))
self.assertIsNone(benchmark.parse_proc_stat("cpu 1 2\n")) # too few fields
self.assertIsNone(benchmark.parse_proc_stat("cpu a b c d\n")) # non-numeric
self.assertIsNone(benchmark.parse_proc_stat("cpu0 1 2 3 4\n")) # per-core only
def test_pct_delta(self):
a = benchmark.parse_proc_stat(self.STAT_A)
b = benchmark.parse_proc_stat(self.STAT_B)
# delta busy 100, delta total 150 -> 66.7%
self.assertEqual(benchmark.cpu_stat_pct(a, b), 66.7)
def test_pct_guards(self):
a = benchmark.parse_proc_stat(self.STAT_A)
self.assertIsNone(benchmark.cpu_stat_pct(None, a))
self.assertIsNone(benchmark.cpu_stat_pct(a, None))
self.assertIsNone(benchmark.cpu_stat_pct(a, a)) # zero time delta
# counters never go backwards in reality, but a clamp guards a torn read
self.assertEqual(benchmark.cpu_stat_pct((200, 1000), (100, 2000)), 0.0)
class TestOriginOk(unittest.TestCase):
"""CSRF guard for the local controller: a cross-origin browser POST carries an Origin
header whose host won't match ours. Reject only when Origin is present AND mismatched —
same-origin page requests and non-browser clients (curl, no Origin) pass."""
def test_no_origin_passes(self): # curl / same-origin navigations omit Origin
self.assertTrue(benchmark.origin_ok(None, "tower:8088"))
self.assertTrue(benchmark.origin_ok("", "tower:8088"))
def test_same_origin_passes(self):
self.assertTrue(benchmark.origin_ok("http://tower:8088", "tower:8088"))
self.assertTrue(benchmark.origin_ok("http://192.0.2.10:8088", "192.0.2.10:8088"))
def test_cross_origin_rejected(self):
self.assertFalse(benchmark.origin_ok("http://evil.example", "tower:8088"))
self.assertFalse(benchmark.origin_ok("https://tower:8088", "tower:9999"))
def test_null_and_garbage_origin_rejected(self):
self.assertFalse(benchmark.origin_ok("null", "tower:8088"))
self.assertFalse(benchmark.origin_ok("http://", "tower:8088"))
class TestNvencLockState(unittest.TestCase):
"""Detect the keylase NVENC session-cap patch by scanning libnvidia-encode.so for the
per-version stock vs patched byte signatures. Drives the leaderboard's locked/unlocked
NVIDIA entity split + badge."""
# a real pair (595.71.05): stock has test/jne, patched has sub eax,eax
SIGS = {"595.71.05": ["e85121feff4189c685c0", "e85121feff29c04189c6"],
"550.00": ["85c00f8596000000", "29c090909090909090"]}
def test_patched_is_unlocked(self):
lib = b"\x00\x00" + bytes.fromhex("e85121feff29c04189c6") + b"\xff\xff"
self.assertEqual(benchmark.nvenc_lock_state(lib, "595.71.05", self.SIGS), "unlocked")
def test_stock_is_locked(self):
lib = b"\x11" + bytes.fromhex("e85121feff4189c685c0") + b"\x22"
self.assertEqual(benchmark.nvenc_lock_state(lib, "595.71.05", self.SIGS), "locked")
def test_unknown_version_is_none(self):
lib = bytes.fromhex("e85121feff29c04189c6")
self.assertIsNone(benchmark.nvenc_lock_state(lib, "999.99", self.SIGS))
def test_neither_signature_present_is_none(self):
self.assertIsNone(benchmark.nvenc_lock_state(b"\x00" * 64, "595.71.05", self.SIGS))
def test_empty_or_missing(self):
self.assertIsNone(benchmark.nvenc_lock_state(b"", "595.71.05", self.SIGS))
self.assertIsNone(benchmark.nvenc_lock_state(None, "595.71.05", self.SIGS))
self.assertIsNone(benchmark.nvenc_lock_state(b"\x00", "595.71.05", {}))
def test_shipped_sig_table_loads_and_covers_5090_driver(self):
# the real committed data file must load and carry the driver the fleet runs
sigs = benchmark.load_nvenc_sigs()
self.assertIn("595.71.05", sigs)
self.assertEqual(len(sigs["595.71.05"]), 2)
class TestDisplayUnit(unittest.TestCase):
"""Temperature display unit from Unraid's dynamix.cfg (optional RO mount) — the tool
should show °F when the server dashboard does. Data stays °C everywhere; this only
drives the UI default (browser toggle can still override)."""
CFG_F = '[display]\ntty="15"\nunit="F"\nnumber=".,"\n'
CFG_C = '[display]\nunit="C"\n'
def test_fahrenheit(self):
self.assertEqual(benchmark.parse_display_unit(self.CFG_F), "F")
def test_celsius(self):
self.assertEqual(benchmark.parse_display_unit(self.CFG_C), "C")
def test_default_celsius(self):
self.assertEqual(benchmark.parse_display_unit(""), "C")
self.assertEqual(benchmark.parse_display_unit(None), "C")
self.assertEqual(benchmark.parse_display_unit('[display]\ntty="15"\n'), "C")
# docker creates a DIRECTORY at the target when the host file is missing —
# _read() returns None there, which must still land on C (covered above)
def test_not_fooled_by_lookalikes(self):
self.assertEqual(benchmark.parse_display_unit('somekey_unit="F"\n'), "C")
self.assertEqual(benchmark.parse_display_unit('unit="X"\n'), "C")
class TestOsVersion(unittest.TestCase):
"""The OS-version reader must handle non-Unraid version strings cleanly — a MOS host
reported version="MOS 0.5.0" and the old digit-anchored regex dumped the whole raw line."""
def test_unraid_numeric(self):
self.assertEqual(benchmark.parse_os_version('version="7.3.2"\n'), "7.3.2")
def test_non_numeric_os(self):
self.assertEqual(benchmark.parse_os_version('version="MOS 0.5.0"'), "MOS 0.5.0")
def test_unquoted(self):
self.assertEqual(benchmark.parse_os_version('version=7.0.1'), "7.0.1")
def test_empty_or_none(self):
self.assertIsNone(benchmark.parse_os_version(""))
self.assertIsNone(benchmark.parse_os_version(None))
def test_capped_to_sane_length(self):
self.assertLessEqual(len(benchmark.parse_os_version('version="' + "x" * 200 + '"')), 60)
class TestOsRelease(unittest.TestCase):
"""The non-Unraid fallback reads PRETTY_NAME from /etc/os-release. It must return a clean,
self-describing distro name and strip a trailing CPU-arch suffix."""
SAMPLE = ('NAME="Ubuntu"\nVERSION="22.04.3 LTS (Jammy Jellyfish)"\n'
'PRETTY_NAME="Ubuntu 22.04.3 LTS"\nID=ubuntu\nVERSION_ID="22.04"\n')
def test_ubuntu_pretty_name(self):
self.assertEqual(benchmark.parse_os_release(self.SAMPLE), "Ubuntu 22.04.3 LTS")
def test_debian_with_parens(self):
self.assertEqual(
benchmark.parse_os_release('PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"\n'),
"Debian GNU/Linux 12 (bookworm)")
def test_strips_trailing_arch(self):
# Unraid's own os-release carries the arch; Unraid is read from the version file first,
# but the stripping must still work if we ever land here.
self.assertEqual(benchmark.parse_os_release('PRETTY_NAME="Unraid OS 7.3 x86_64"'),
"Unraid OS 7.3")
def test_no_pretty_name(self):
self.assertIsNone(benchmark.parse_os_release('NAME="Foo"\nID=foo\n'))
def test_empty_or_none(self):
self.assertIsNone(benchmark.parse_os_release(""))
self.assertIsNone(benchmark.parse_os_release(None))
def test_capped_to_sane_length(self):
self.assertLessEqual(len(benchmark.parse_os_release('PRETTY_NAME="' + "x" * 200 + '"')), 60)
XE_FDINFO_SAMPLE = """pos:\t0
flags:\t0100002
mnt_id:\t97
ino:\t14
drm-driver:\txe
drm-client-id:\t17558
drm-pdev:\t0000:04:00.0
drm-total-system:\t0
drm-resident-system:\t0
drm-total-gtt:\t12 MiB
drm-resident-gtt:\t12 MiB
drm-total-vram0:\t40972 KiB
drm-shared-vram0:\t0
drm-active-vram0:\t40772 KiB
drm-resident-vram0:\t40972 KiB
drm-total-stolen:\t0
drm-resident-stolen:\t0
drm-cycles-rcs:\t0
drm-total-cycles-rcs:\t2072727642368
drm-cycles-vcs:\t8000
drm-total-cycles-vcs:\t2072727642368
drm-engine-capacity-vcs:\t2
drm-cycles-vecs:\t63638128
drm-total-cycles-vecs:\t2072727642368
drm-engine-capacity-vecs:\t2
"""
class TestXeFdinfo(unittest.TestCase):
"""Intel xe (Battlemage) / i915 (Alchemist) fdinfo parsing — captured from a live ffmpeg
VAAPI transcode on a real Arc Pro B50 (xe, kernel 6.18). VRAM lives in drm-resident-vram0
(KiB), media engines in cycle counters: vcs = video decode+encode, vecs = video enhance."""
def test_vram_resident_kib(self):
d = benchmark.parse_xe_fdinfo(XE_FDINFO_SAMPLE)
self.assertEqual(d["vram_kib"], 40972)
def test_mib_region_converts(self):
d = benchmark.parse_xe_fdinfo(
"drm-driver:\txe\ndrm-client-id:\t1\ndrm-resident-vram0:\t12 MiB\n")
self.assertEqual(d["vram_kib"], 12288)
def test_i915_local_region(self):
# Alchemist dGPUs run i915, whose VRAM regions are named localN
d = benchmark.parse_xe_fdinfo(
"drm-driver:\ti915\ndrm-client-id:\t2\ndrm-resident-local0:\t2048 KiB\n")
self.assertEqual(d["vram_kib"], 2048)
def test_cycles_and_capacity(self):
d = benchmark.parse_xe_fdinfo(XE_FDINFO_SAMPLE)
self.assertEqual(d["cycles"]["vcs"], 8000)
self.assertEqual(d["cycles"]["vcs_cap"], 2)
self.assertEqual(d["cycles"]["vecs"], 63638128)
self.assertEqual(d["cycles"]["vcs_total"], 2072727642368)
def test_pdev_and_client(self):
d = benchmark.parse_xe_fdinfo(XE_FDINFO_SAMPLE)
self.assertEqual(d["pdev"], "0000:04:00.0")
self.assertEqual(d["client"], "17558")
def test_wrong_driver_rejected(self):
self.assertEqual(benchmark.parse_xe_fdinfo("drm-driver:\tamdgpu\ndrm-memory-vram:\t100\n"), {})
self.assertEqual(benchmark.parse_xe_fdinfo(""), {})
class TestCyclePct(unittest.TestCase):
"""xe engine busy% from cycle deltas: busy / (wall × instances), clamped [0,100]."""
def test_half_busy_single(self):
self.assertEqual(benchmark.cycle_pct(500, 1000, 1), 50.0)