Module blight.actions.benchmark

The Benchmark action.

Expand source code
"""
The `Benchmark` action.
"""

import time
from pathlib import Path

from pydantic import BaseModel

from blight.action import Action
from blight.tool import Tool
from blight.util import flock_append


class BenchmarkRecord(BaseModel):
    """
    Represents a single `Benchmark` record. Each record contains a representation
    of the tool invocation, the elapsed time between the `before_run`
    and `after_run` handlers (in milliseconds), and a flag indicating whether
    the underlying tool was actually run.
    """

    tool: Tool
    """
    The `Tool` invocation.
    """

    elapsed: int
    """
    The invocation's runtime, in milliseconds.
    """

    run_skipped: bool
    """
    Whether or not the tool was actually run.
    """

    class Config:
        arbitrary_types_allowed = True
        json_encoders = {Tool: lambda t: t.asdict()}


class Benchmark(Action):
    def before_run(self, tool: Tool) -> None:
        self._start_nanos = time.monotonic_ns()

    def after_run(self, tool: Tool, *, run_skipped: bool = False) -> None:
        elapsed = (time.monotonic_ns() - self._start_nanos) // 1000

        bench_file = Path(self._config["output"])
        with flock_append(bench_file) as io:
            bench = BenchmarkRecord(tool=tool, elapsed=elapsed, run_skipped=run_skipped)
            print(bench.json(), file=io)

Classes

class Benchmark (config: Dict[str, str])

A generic action, run with every tool (both before and after the tool's execution).

Expand source code
class Benchmark(Action):
    def before_run(self, tool: Tool) -> None:
        self._start_nanos = time.monotonic_ns()

    def after_run(self, tool: Tool, *, run_skipped: bool = False) -> None:
        elapsed = (time.monotonic_ns() - self._start_nanos) // 1000

        bench_file = Path(self._config["output"])
        with flock_append(bench_file) as io:
            bench = BenchmarkRecord(tool=tool, elapsed=elapsed, run_skipped=run_skipped)
            print(bench.json(), file=io)

Ancestors

Inherited members

class BenchmarkRecord (**data: Any)

Represents a single Benchmark record. Each record contains a representation of the tool invocation, the elapsed time between the before_run and after_run handlers (in milliseconds), and a flag indicating whether the underlying tool was actually run.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class BenchmarkRecord(BaseModel):
    """
    Represents a single `Benchmark` record. Each record contains a representation
    of the tool invocation, the elapsed time between the `before_run`
    and `after_run` handlers (in milliseconds), and a flag indicating whether
    the underlying tool was actually run.
    """

    tool: Tool
    """
    The `Tool` invocation.
    """

    elapsed: int
    """
    The invocation's runtime, in milliseconds.
    """

    run_skipped: bool
    """
    Whether or not the tool was actually run.
    """

    class Config:
        arbitrary_types_allowed = True
        json_encoders = {Tool: lambda t: t.asdict()}

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Class variables

var Config
var elapsed : int

The invocation's runtime, in milliseconds.

var run_skipped : bool

Whether or not the tool was actually run.

var toolTool

The Tool invocation.