"""Самопроверка ядра скриптов perf/analysis (без стенда).""" import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "analysis")) import cpuprofile_top # noqa: E402 import timeline_breakdown # noqa: E402 def test_self_times_nested_and_category_inheritance(): evs = [ {"name": "RunTask", "ts": 0, "dur": 100}, {"name": "FunctionCall", "ts": 10, "dur": 50}, {"name": "V8.Unknown", "ts": 20, "dur": 10}, # наследует scripting {"name": "Layout", "ts": 70, "dur": 20}, ] st = {e["name"]: (s, c) for e, s, c in timeline_breakdown.self_times(evs)} assert st["RunTask"] == (30.0, "other") assert st["FunctionCall"] == (40.0, "scripting") assert st["V8.Unknown"] == (10.0, "scripting") assert st["Layout"] == (20.0, "rendering") def test_vlq_decode(): assert cpuprofile_top.vlq_decode("AAAA") == [0, 0, 0, 0] assert cpuprofile_top.vlq_decode("CAAC") == [1, 0, 0, 1] assert cpuprofile_top.vlq_decode("D") == [-1] assert cpuprofile_top.vlq_decode("gB") == [16] def test_sourcemap_lookup(tmp_path): # строка 0: col 0 -> a.js:1:0, col 5 -> a.js:3:2; строка 1: col 0 -> b.js:1:0 m = {"version": 3, "sources": ["../../../../../web/src/a.js", "../../../../../web/node_modules/x/b.js"], "names": [], "mappings": "AAAA,KAEE;ACFF"} p = tmp_path / "t.js.map" p.write_text(json.dumps(m)) sm = cpuprofile_top.SourceMap(p) assert sm.lookup(0, 3)[:2] == ("web/src/a.js", 1) assert sm.lookup(0, 9)[:2] == ("web/src/a.js", 3) assert sm.lookup(1, 0)[0] == "web/node_modules/x/b.js" assert cpuprofile_top.package_of("web/node_modules/@emotion/hash/dist/x.js") == "node_modules/@emotion/hash"