|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for perfetto.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import tempfile |
| 19 | +import time |
| 20 | + |
| 21 | +from absl.testing import absltest |
| 22 | +from tunix.perf.experimental import perfetto |
| 23 | +from tunix.perf.experimental import tracer |
| 24 | + |
| 25 | + |
| 26 | +class PerfettoTest(absltest.TestCase): |
| 27 | + |
| 28 | + def test_create_span_name(self): |
| 29 | + # Test basic span name with global_step |
| 30 | + name = perfetto._create_span_name("my_span", {"global_step": 10}) |
| 31 | + self.assertEqual(name, "my_span (step=10)") |
| 32 | + |
| 33 | + # Test peft_train_step with role |
| 34 | + name = perfetto._create_span_name( |
| 35 | + "peft_train_step", {"global_step": 20, "role": "actor"} |
| 36 | + ) |
| 37 | + self.assertEqual(name, "peft_train_step (step=20, role=actor)") |
| 38 | + |
| 39 | + # Test rollout with group_id and pair_index |
| 40 | + name = perfetto._create_span_name( |
| 41 | + "rollout", {"group_id": 5, "pair_index": 3, "global_step": 100} |
| 42 | + ) |
| 43 | + self.assertEqual(name, "rollout (step=100, group_id=5, pair_index=3)") |
| 44 | + |
| 45 | + # Test rollout with missing pair_index |
| 46 | + name = perfetto._create_span_name("rollout", {"group_id": 5}) |
| 47 | + self.assertEqual(name, "rollout (group_id=5)") |
| 48 | + |
| 49 | + # Test unknown name with extra tags (should ignore specific logic but keep step) |
| 50 | + name = perfetto._create_span_name( |
| 51 | + "unknown_span", {"role": "actor", "global_step": 50} |
| 52 | + ) |
| 53 | + self.assertEqual(name, "unknown_span (step=50)") |
| 54 | + |
| 55 | + # Test no tags |
| 56 | + name = perfetto._create_span_name("simple_span", {}) |
| 57 | + self.assertEqual(name, "simple_span") |
| 58 | + |
| 59 | + # TODO(noghabi): Add more tests for PerfettoTraceWriter. |
| 60 | + def test_perfetto_trace_writer(self): |
| 61 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 62 | + writer = perfetto.PerfettoTraceWriter(trace_dir=tmp_dir) |
| 63 | + |
| 64 | + # Create some dummy timelines |
| 65 | + t = tracer.Timeline("test_timeline", time.perf_counter()) |
| 66 | + s = t.start_span("test_span", time.perf_counter()) |
| 67 | + time.sleep(0.001) |
| 68 | + t.stop_span(time.perf_counter()) |
| 69 | + |
| 70 | + timelines = {"test_timeline": t} |
| 71 | + |
| 72 | + writer.write_timelines(timelines) |
| 73 | + |
| 74 | + # Check if file was created |
| 75 | + files = os.listdir(tmp_dir) |
| 76 | + self.assertLen(files, 1) |
| 77 | + self.assertTrue(files[0].startswith("perfetto_trace_v2_")) |
| 78 | + self.assertTrue(files[0].endswith(".pb")) |
| 79 | + |
| 80 | + # We could parse the proto back to verify content, but just existence is good for now. |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + absltest.main() |
0 commit comments