Class Profiler

java.lang.Object
org.team1126.lib.logging.Profiler

public final class Profiler extends Object
A simple pseudo call-graph profiler that publishes timings to NetworkTables.

Data is published to /Profiling, with CPU time utilized by the garbage collector published at /Profiling/gc, and time spent publishing values to NT at /Profiling/overhead. All timings are recorded in milliseconds, as the duration of execution. The profiler records the time to execute blocks of code, with support for nesting execution times which are reflected in an expanding NT tree.

All invocations of start(String) are expected to be followed by a closing end(). Additionally, it is expected that a single start(String)/end() pair is found at the highest level of the robot's code, with no other "root" pairs (this is already done if utilizing LoggedRobot). If either of these limitations are left unsatisfied, an error will be printed to the Driver Station console. Profiling across threads is also not supported.

  • Method Details

    • isRunning

      public static boolean isRunning()
      Determines if the profiler is currently running.
      Returns:
      true if the profiler is running.
    • run

      public static void run(String name, Runnable runnable)
      Profiles a section of user code. Serves as a shorthand for calling start(String), {user code}, end().
      Parameters:
      name - The name of the call. Must be unique.
      runnable - The user code to be ran.
    • run

      public static <T> T run(String name, Supplier<T> supplier)
      Profiles a section of user code. Serves as a shorthand for calling start(String), {user code}, end().
      Type Parameters:
      T - The return type of the supplier.
      Parameters:
      name - The name of the call. Must be unique.
      Returns:
      The value returned from the supplier.
    • start

      public static void start(String name)
      Starts to profile a section of user code. This method should be invoked before the user code to be profiled, with end() expected to be after. Additional start(String)/end() pairs can be nested within the section of user code, and will appear as a sub-topic in NetworkTables. Timings of nested pairs are still included in the overall time recorded by the parent pair.
      Parameters:
      name - The name of the call. Must be unique.
    • end

      public static void end()
      Ends profiling a section of code. This method should be invoked after the user code to be profiled. Trailing calls to this method will result in an error printed to the Driver Station console.