| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import time |
| 4 | from types import TracebackType |
| 5 | |
| 6 | |
| 7 | # Collect time and generate time metrics |
| 8 | # |
| 9 | # Example Usage: |
| 10 | # collector = TimeCollector(TimeCollector.US) |
| 11 | # for _ in range(total_iteration): |
| 12 | # with collector: |
| 13 | # ... |
| 14 | # collector.dump_avg_max() |
| 15 | class TimeCollector: |
| 16 | NS: int = 1 |
| 17 | US: int = NS * 1000 |
| 18 | MS: int = US * 1000 |
| 19 | S: int = MS * 1000 |
| 20 | |
| 21 | def __init__(self, scale: int) -> None: |
| 22 | self.cnt: int = 0 |
| 23 | self._sum: int = 0 |
| 24 | self._max: int | None = None |
| 25 | self.scale = scale |
| 26 | self.start_time: int = time.monotonic_ns() |
| 27 | |
| 28 | def collect(self, v: int) -> None: |
| 29 | self.cnt += 1 |
| 30 | self._sum += v |
| 31 | if self._max is None: |
| 32 | self._max = v |
| 33 | else: |
| 34 | self._max = max(self._max, v) |
| 35 | |
| 36 | def avg(self) -> float | str: |
| 37 | return self._sum * 1.0 / self.cnt / self.scale if self.cnt > 0 else "N/A" |
| 38 | |
| 39 | def max(self) -> float | str: |
| 40 | return self._max / self.scale if self._max else "N/A" |
| 41 | |
| 42 | def dump_avg_max(self) -> list[float | str]: |
| 43 | return [self.avg(), self.max()] |
| 44 | |
| 45 | def __enter__(self) -> None: |
| 46 | self.start_time = time.monotonic_ns() |
| 47 | |
| 48 | def __exit__( |
| 49 | self, |
| 50 | exc_type: type[BaseException] | None, |
| 51 | exc_value: BaseException | None, |
| 52 | exc_traceback: TracebackType | None, |
| 53 | ) -> None: |
| 54 | self.collect(time.monotonic_ns() - self.start_time) |