package com.ssyf.task.models;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 性能指标类
 */
public class PerformanceMetrics {
    String taskName;
    AtomicLong totalExecutions = new AtomicLong(0);
    AtomicLong successfulExecutions = new AtomicLong(0);
    AtomicLong failedExecutions = new AtomicLong(0);
    AtomicLong totalExecutionTime = new AtomicLong(0);
    AtomicLong maxExecutionTime = new AtomicLong(0);
    AtomicLong minExecutionTime = new AtomicLong(Long.MAX_VALUE);

    public PerformanceMetrics(String taskName) {
        this.taskName = taskName;
    }

    public void recordExecution(boolean success, long executionTime) {
        totalExecutions.incrementAndGet();
        if (success) {
            successfulExecutions.incrementAndGet();
        } else {
            failedExecutions.incrementAndGet();
        }
        totalExecutionTime.addAndGet(executionTime);
        maxExecutionTime.set(Math.max(maxExecutionTime.get(), executionTime));
        minExecutionTime.set(Math.min(minExecutionTime.get(), executionTime));
    }

    public double getSuccessRate() {
        return totalExecutions.get() > 0 ? (double) successfulExecutions.get() / totalExecutions.get() : 0.0;
    }

    public double getAverageExecutionTime() {
        return totalExecutions.get() > 0 ? (double) totalExecutionTime.get() / totalExecutions.get() : 0.0;
    }
}