The profiling module tests (test_profile.py) verify the performance profiling functionality, including both PyInstrument and PyTorch profiler integration.
deftest_get_context_manager(self):"""Test get_context_manager function."""# Test with pyinstrument profilerctx=profile.get_context_manager(profiler_type="pyinstrument",rank_zero_only=True,outdir="/tmp")assertctxisnotNone# Test with torch profilerctx=profile.get_context_manager(profiler_type="torch",wait=1,warmup=1,active=2,repeat=1,rank_zero_only=True,outdir="/tmp")assertctxisnotNone
deftest_pyinstrument_profiler(self):"""Test PyInstrumentProfiler context manager."""profiler=profile.PyInstrumentProfiler(rank_zero_only=True,outdir="/tmp")# Test that it can be used as a context managerwithprofiler:# Do some worktime.sleep(0.1)result=1+1assertresult==2
deftest_null_context_when_not_rank_zero(self):"""Test that profiler returns null context when not rank zero and rank_zero_only=True."""ctx=profile.get_context_manager(profiler_type="pyinstrument",rank_zero_only=True,outdir="/tmp")# Should work without errorswithctx:result=1+1assertresult==2