Class PerformanceEvent

java.lang.Object
io.github.jspinak.brobot.logging.events.PerformanceEvent

public final class PerformanceEvent extends Object
Event representing performance metrics for Brobot operations.

Captures detailed timing and resource usage information for operations, including memory usage and timing breakdowns. This is essential for performance monitoring and optimization.

Example usage:


 PerformanceEvent event = PerformanceEvent.builder()
     .operation("findAndClick")
     .duration(Duration.ofMillis(250))
     .memoryUsed(1024 * 1024) // 1MB
     .breakdown(Map.of(
         "find", Duration.ofMillis(200),
         "click", Duration.ofMillis(50)
     ))
     .build();
 
  • Method Details

    • success

      public static PerformanceEvent success(String operation, Duration duration)
      Create a PerformanceEvent for a successful operation.
      Parameters:
      operation - The operation name
      duration - The total duration
      Returns:
      A new PerformanceEvent for a successful operation
    • failure

      public static PerformanceEvent failure(String operation, Duration duration, String errorMessage)
      Create a PerformanceEvent for a failed operation.
      Parameters:
      operation - The operation name
      duration - The duration before failure
      errorMessage - The error message
      Returns:
      A new PerformanceEvent for a failed operation
    • getMemoryDelta

      public long getMemoryDelta()
      Get memory delta (difference between before and after).
      Returns:
      Memory delta in bytes (positive = memory increased)
    • getMemoryUsedMB

      public double getMemoryUsedMB()
      Get memory usage in megabytes.
      Returns:
      Memory used in MB
    • getPeakMemoryUsedMB

      public double getPeakMemoryUsedMB()
      Get peak memory usage in megabytes.
      Returns:
      Peak memory used in MB
    • getOperationsPerSecond

      public double getOperationsPerSecond()
      Get operations per second rate.
      Returns:
      Operations per second, or 0 if duration is zero
    • getBreakdownPercentages

      public Map<String,Double> getBreakdownPercentages()
      Get the percentage of time spent in each sub-operation.
      Returns:
      Map of operation names to percentage of total time
    • isSlow

      public boolean isSlow(long thresholdMs)
      Check if this operation was slow compared to a threshold.
      Parameters:
      thresholdMs - The threshold in milliseconds
      Returns:
      true if the operation was slower than the threshold
    • isMemoryIntensive

      public boolean isMemoryIntensive(long thresholdBytes)
      Check if this operation used excessive memory.
      Parameters:
      thresholdBytes - The threshold in bytes
      Returns:
      true if memory usage exceeded the threshold
    • getDescription

      public String getDescription()
      Get a human-readable description of this performance event.
      Returns:
      A formatted description
    • getBreakdownDescription

      public String getBreakdownDescription()
      Get a detailed breakdown description.
      Returns:
      A formatted breakdown description
    • getLevel

      public LogLevel getLevel()
      Get the log level for this performance event. Successful operations log at INFO level, failures at ERROR level, slow operations log at WARN level.
      Returns:
      The appropriate log level
    • getMessage

      public String getMessage()
      Get a message describing this performance event.
      Returns:
      A formatted message
    • getMemoryUsage

      public Long getMemoryUsage()
      Get the memory usage for this performance event.
      Returns:
      Memory usage in bytes, or null if not measured
    • getOperationCount

      public Long getOperationCount()
      Get the operation count for this performance event.
      Returns:
      The number of operations performed
    • getError

      public Throwable getError()
      Get any error associated with this performance event.
      Returns:
      null since performance events only have error messages
    • builder

    • toBuilder

    • getTimestamp

      public Instant getTimestamp()
      Timestamp when the operation was initiated
    • getOperation

      public String getOperation()
      Name/description of the operation
    • getDuration

      public Duration getDuration()
      Total duration of the operation
    • getMemoryUsed

      public long getMemoryUsed()
      Memory used by the operation in bytes
    • getBreakdown

      public Map<String,Duration> getBreakdown()
      Detailed timing breakdown of sub-operations
    • getMetadata

      public Map<String,Object> getMetadata()
      Additional performance metadata
    • getCorrelationId

      public String getCorrelationId()
      Correlation ID for tracking related operations
    • getCurrentState

      public String getCurrentState()
      Current state when operation was performed
    • getThreadName

      public String getThreadName()
      Thread name where operation was executed
    • getCpuTime

      public Duration getCpuTime()
      CPU time used (if available)
    • isSuccess

      public boolean isSuccess()
      Whether this operation was considered successful
    • getErrorMessage

      public String getErrorMessage()
      Error message if operation failed
    • getPeakMemoryUsed

      public long getPeakMemoryUsed()
      Peak memory usage during operation
    • getMemoryBefore

      public long getMemoryBefore()
      Memory usage before operation started
    • getMemoryAfter

      public long getMemoryAfter()
      Memory usage after operation completed
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object