Class Metrics

java.lang.Object
es.urjc.etsii.grafo.metrics.Metrics

public final class Metrics extends Object
Manages metrics instances. Example usage: - Call resetMetrics() to initialize a new instance of empty metrics - Run the algorithm. Any algorithm component can get the current metrics instance using getCurrentThreadMetrics(), and add data points to it using AbstractMetric.add(long, double). - Do something with the metrics after the algorithm finishes, for example merging (see below). - Reset metrics before the next algorithm start executing.

Note that metrics are always ThreadLocal, which means that every thread works on its own independent copy. Metrics are always disabled by default, and must be enabled by either the framework or manually by the user Metrics from different threads can later be merged using merge(MetricsStorage...)

  • Method Details

    • getCurrentThreadMetrics

      public static MetricsStorage getCurrentThreadMetrics()
      Get metrics collected by the current thread.
      Returns:
      metrics instance for the current thread
      Throws:
      IllegalStateException - if the metrics have not been initialized for the current thread, or if the metrics are disabled
    • resetMetrics

      public static void resetMetrics()
      Initialize a new metrics object for use in the current thread
      Throws:
      IllegalStateException - if the metrics are disabled
    • resetMetrics

      public static void resetMetrics(long referenceTime)
      Initialize a new metrics object for use in the current thread
      Parameters:
      referenceTime - reference time, see MetricsStorage(long) for a more detailed explanation.
      Throws:
      IllegalStateException - if the metrics are disabled
    • enableMetrics

      public static void enableMetrics()
      Enable metrics
    • disableMetrics

      public static void disableMetrics()
      Disable metrics and empty all internal data structures
    • enableTimeStats

      public static void enableTimeStats()
    • disableTimeStats

      public static void disableTimeStats()
    • areTimeStatsEnabled

      public static boolean areTimeStatsEnabled()
      Return true if time stats are enabled, false otherwise.
      Returns:
      true if time stats are enabled, false otherwise
    • areMetricsEnabled

      public static boolean areMetricsEnabled()
      Return true if metrics are enabled, false otherwise.
      Returns:
      true if metrics are enabled, false otherwise
    • get

      public static <T extends AbstractMetric> T get(String metricName)
    • get

      public static <T extends AbstractMetric> T get(Class<T> metric)
    • register

      public static <T extends AbstractMetric> void register(String metricName, Function<Long,T> initializer)
      Register a new metric so the framework automatically tracks it
      Type Parameters:
      T - metric type
      Parameters:
      metricName - metric name
      initializer - method to initialize the given metric
    • register

      public static <T extends AbstractMetric> void register(Class<T> metric, Function<Long,T> initializer)
      Register a new metric so the framework automatically tracks it
      Type Parameters:
      T - Metric type
      Parameters:
      metric - metric implementation to track, must extend AbstractMetric
      initializer - Method used to initialize the given metric
    • merge

      public static MetricsStorage merge(MetricsStorage... metrics)
      Merge several metrics instances. All data points will be translated to the lowest reference point For example, if metrics A was created at 5, and metrics B reference time is 10, the returned metrics will have 5 as reference point and all data points from metrics B will have their instant incremented by 5.
      Parameters:
      metrics - metrics to merge
      Returns:
      new metric instance containing the data of all the metrics provided, with time corrected as necessary.
    • merge

      public static MetricsStorage merge(Iterable<MetricsStorage> metrics)
      Merge several metrics instances. For example, if metrics A was created at 5, and metrics B reference time is 10, the returned metrics will have 5 as reference point and all data points from metrics B will have their instant incremented by 5.
      Parameters:
      metrics - metric instances
      Returns:
      new metric instance containing the data of all the metrics provided, with time corrected as necessary.
    • addCurrentObjectives

      public static <S extends Solution<S, I>, I extends Instance> void addCurrentObjectives(S solution)
    • add

      public static void add(String metricName, double value)
    • add

      public static <T extends AbstractMetric> void add(Class<T> metricType, double value)
    • add

      public static void add(String metricName, long instant, double value)
    • add

      public static <T extends AbstractMetric> void add(Class<T> metricType, long instant, double value)