code: def _test_frequency_with_engine(
      workers=None,
      lower_bound_factor=0.8,
      upper_bound_factor=1.1,
      every=1,
  ):
      if workers is None:
          workers = idist.get_world_size()
  
      artificial_time = 1.0 / workers  # seconds
      total_tokens = 400 // workers
      batch_size = 128 // workers
  
      estimated_wps = batch_size * workers / artificial_time
  
      def update_fn(engine, batch):
          time.sleep(artificial_time)
          return {"ntokens": len(batch)}
  
      engine = Engine(update_fn)
      wps_metric = Frequency(output_transform=lambda x: x["ntokens"])
      event = Events.ITERATION_COMPLETED(every=every)
      wps_metric.attach(engine, "wps", event_name=event)
  
      @engine.on(event)
      def assert_wps(e):
          wps = e.state.metrics["wps"]
          # Skip iterations 2, 3, 4 if backend is Horovod on CUDA,
          # wps is abnormally low for these iterations
          # otherwise, other values of wps are OK
          if (
              idist.model_name() == "horovod-dist"
              and e.state.iteration in (2, 3, 4)
          ):
              return
          low_wps = estimated_wps * lower_bound_factor
          high_wps = estimated_wps * upper_bound_factor
          assert (
              low_wps < wps <= high_wps
          ), f"{e.state.iteration}: {low_wps} < {wps} <= {high_wps}"
  
      data = [
          [i] * batch_size for i in range(0, total_tokens, batch_size)
      ]
      engine.run(data, max_epochs=2)
  
file_path: ignite/tests/ignite/metrics/test_frequency.py
imports: import os
  import sys
  import time
  import pytest
  import torch
  import ignite.distributed
  from ignite.engine import Engine, Events
  from ignite.metrics import Frequency
label_desc: Use image node_id 2 for calling a global function with example usage: _test_frequency_with_engine(workers, lower_bound_factor, upper_bound_factor, every) and returns: dict