MetricSampler.java

net.minecraft.util.profiling.metrics.MetricSampler

信息

  • 全限定名:net.minecraft.util.profiling.metrics.MetricSampler
  • 类型:public class
  • 包:net.minecraft.util.profiling.metrics
  • 源码路径:src/main/java/net/minecraft/util/profiling/metrics/MetricSampler.java
  • 起始行号:L13
  • 职责:

    TODO

字段/常量

  • name

    • 类型: String
    • 修饰符: private final
    • 源码定位: L14
    • 说明:

      TODO

  • category

    • 类型: MetricCategory
    • 修饰符: private final
    • 源码定位: L15
    • 说明:

      TODO

  • sampler

    • 类型: DoubleSupplier
    • 修饰符: private final
    • 源码定位: L16
    • 说明:

      TODO

  • ticks

    • 类型: ByteBuf
    • 修饰符: private final
    • 源码定位: L17
    • 说明:

      TODO

  • values

    • 类型: ByteBuf
    • 修饰符: private final
    • 源码定位: L18
    • 说明:

      TODO

  • isRunning

    • 类型: boolean
    • 修饰符: private volatile
    • 源码定位: L19
    • 说明:

      TODO

  • beforeTick

    • 类型: Runnable
    • 修饰符: private final
    • 源码定位: L20
    • 说明:

      TODO

  • thresholdTest

    • 类型: MetricSampler.ThresholdTest
    • 修饰符: final
    • 源码定位: L21
    • 说明:

      TODO

  • currentValue

    • 类型: double
    • 修饰符: private
    • 源码定位: L22
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.profiling.metrics.MetricSampler.MetricSamplerBuilder

    • 类型: class
    • 修饰符: public static
    • 源码定位: L134
    • 说明:

      TODO

  • net.minecraft.util.profiling.metrics.MetricSampler.SamplerResult

    • 类型: class
    • 修饰符: public static
    • 源码定位: L164
    • 说明:

      TODO

  • net.minecraft.util.profiling.metrics.MetricSampler.ThresholdTest

    • 类型: interface
    • 修饰符: public
    • 源码定位: L188
    • 说明:

      TODO

  • net.minecraft.util.profiling.metrics.MetricSampler.ValueIncreasedByPercentage

    • 类型: class
    • 修饰符: public static
    • 源码定位: L192
    • 说明:

      TODO

构造器

protected MetricSampler(String name, MetricCategory category, DoubleSupplier sampler, Runnable beforeTick, MetricSampler.ThresholdTest thresholdTest) @ L24

  • 构造器名:MetricSampler
  • 源码定位:L24
  • 修饰符:protected

参数:

  • name: String
  • category: MetricCategory
  • sampler: DoubleSupplier
  • beforeTick: Runnable
  • thresholdTest: MetricSampler.ThresholdTest

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static MetricSampler create(String name, MetricCategory category, DoubleSupplier sampler) @ L37

  • 方法名:create
  • 源码定位:L37
  • 返回类型:MetricSampler
  • 修饰符:public static

参数:

  • name: String
  • category: MetricCategory
  • sampler: DoubleSupplier

说明:

TODO

public static <T> MetricSampler create(String metricName, MetricCategory category, T context, ToDoubleFunction<T> sampler) @ L41

  • 方法名:create
  • 源码定位:L41
  • 返回类型: MetricSampler
  • 修饰符:public static

参数:

  • metricName: String
  • category: MetricCategory
  • context: T
  • sampler: ToDoubleFunction

说明:

TODO

public static <T> MetricSampler.MetricSamplerBuilder<T> builder(String metricName, MetricCategory category, ToDoubleFunction<T> sampler, T context) @ L45

  • 方法名:builder
  • 源码定位:L45
  • 返回类型: MetricSampler.MetricSamplerBuilder
  • 修饰符:public static

参数:

  • metricName: String
  • category: MetricCategory
  • sampler: ToDoubleFunction
  • context: T

说明:

TODO

public void onStartTick() @ L53

  • 方法名:onStartTick
  • 源码定位:L53
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void onEndTick(int currentTick) @ L63

  • 方法名:onEndTick
  • 源码定位:L63
  • 返回类型:void
  • 修饰符:public

参数:

  • currentTick: int

说明:

TODO

public void onFinished() @ L70

  • 方法名:onFinished
  • 源码定位:L70
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

private void verifyRunning() @ L77

  • 方法名:verifyRunning
  • 源码定位:L77
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

DoubleSupplier getSampler() @ L83

  • 方法名:getSampler
  • 源码定位:L83
  • 返回类型:DoubleSupplier
  • 修饰符:package-private

参数:

说明:

TODO

public String getName() @ L87

  • 方法名:getName
  • 源码定位:L87
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public MetricCategory getCategory() @ L91

  • 方法名:getCategory
  • 源码定位:L91
  • 返回类型:MetricCategory
  • 修饰符:public

参数:

说明:

TODO

public MetricSampler.SamplerResult result() @ L95

  • 方法名:result
  • 源码定位:L95
  • 返回类型:MetricSampler.SamplerResult
  • 修饰符:public

参数:

说明:

TODO

public boolean triggersThreshold() @ L113

  • 方法名:triggersThreshold
  • 源码定位:L113
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public boolean equals(Object o) @ L117

  • 方法名:equals
  • 源码定位:L117
  • 返回类型:boolean
  • 修饰符:public

参数:

  • o: Object

说明:

TODO

public int hashCode() @ L129

  • 方法名:hashCode
  • 源码定位:L129
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

代码

public class MetricSampler {
    private final String name;
    private final MetricCategory category;
    private final DoubleSupplier sampler;
    private final ByteBuf ticks;
    private final ByteBuf values;
    private volatile boolean isRunning;
    private final @Nullable Runnable beforeTick;
    final MetricSampler.@Nullable ThresholdTest thresholdTest;
    private double currentValue;
 
    protected MetricSampler(
        String name, MetricCategory category, DoubleSupplier sampler, @Nullable Runnable beforeTick, MetricSampler.@Nullable ThresholdTest thresholdTest
    ) {
        this.name = name;
        this.category = category;
        this.beforeTick = beforeTick;
        this.sampler = sampler;
        this.thresholdTest = thresholdTest;
        this.values = ByteBufAllocator.DEFAULT.buffer();
        this.ticks = ByteBufAllocator.DEFAULT.buffer();
        this.isRunning = true;
    }
 
    public static MetricSampler create(String name, MetricCategory category, DoubleSupplier sampler) {
        return new MetricSampler(name, category, sampler, null, null);
    }
 
    public static <T> MetricSampler create(String metricName, MetricCategory category, T context, ToDoubleFunction<T> sampler) {
        return builder(metricName, category, sampler, context).build();
    }
 
    public static <T> MetricSampler.MetricSamplerBuilder<T> builder(String metricName, MetricCategory category, ToDoubleFunction<T> sampler, T context) {
        if (sampler == null) {
            throw new IllegalStateException();
        } else {
            return new MetricSampler.MetricSamplerBuilder<>(metricName, category, sampler, context);
        }
    }
 
    public void onStartTick() {
        if (!this.isRunning) {
            throw new IllegalStateException("Not running");
        } else {
            if (this.beforeTick != null) {
                this.beforeTick.run();
            }
        }
    }
 
    public void onEndTick(int currentTick) {
        this.verifyRunning();
        this.currentValue = this.sampler.getAsDouble();
        this.values.writeDouble(this.currentValue);
        this.ticks.writeInt(currentTick);
    }
 
    public void onFinished() {
        this.verifyRunning();
        this.values.release();
        this.ticks.release();
        this.isRunning = false;
    }
 
    private void verifyRunning() {
        if (!this.isRunning) {
            throw new IllegalStateException(String.format(Locale.ROOT, "Sampler for metric %s not started!", this.name));
        }
    }
 
    DoubleSupplier getSampler() {
        return this.sampler;
    }
 
    public String getName() {
        return this.name;
    }
 
    public MetricCategory getCategory() {
        return this.category;
    }
 
    public MetricSampler.SamplerResult result() {
        Int2DoubleMap result = new Int2DoubleOpenHashMap();
        int firstTick = Integer.MIN_VALUE;
        int lastTick = Integer.MIN_VALUE;
 
        while (this.values.isReadable(8)) {
            int currentTick = this.ticks.readInt();
            if (firstTick == Integer.MIN_VALUE) {
                firstTick = currentTick;
            }
 
            result.put(currentTick, this.values.readDouble());
            lastTick = currentTick;
        }
 
        return new MetricSampler.SamplerResult(firstTick, lastTick, result);
    }
 
    public boolean triggersThreshold() {
        return this.thresholdTest != null && this.thresholdTest.test(this.currentValue);
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            MetricSampler that = (MetricSampler)o;
            return this.name.equals(that.name) && this.category.equals(that.category);
        } else {
            return false;
        }
    }
 
    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
 
    public static class MetricSamplerBuilder<T> {
        private final String name;
        private final MetricCategory category;
        private final DoubleSupplier sampler;
        private final T context;
        private @Nullable Runnable beforeTick;
        private MetricSampler.@Nullable ThresholdTest thresholdTest;
 
        public MetricSamplerBuilder(String name, MetricCategory category, ToDoubleFunction<T> sampler, T context) {
            this.name = name;
            this.category = category;
            this.sampler = () -> sampler.applyAsDouble(context);
            this.context = context;
        }
 
        public MetricSampler.MetricSamplerBuilder<T> withBeforeTick(Consumer<T> beforeTick) {
            this.beforeTick = () -> beforeTick.accept(this.context);
            return this;
        }
 
        public MetricSampler.MetricSamplerBuilder<T> withThresholdAlert(MetricSampler.ThresholdTest thresholdTest) {
            this.thresholdTest = thresholdTest;
            return this;
        }
 
        public MetricSampler build() {
            return new MetricSampler(this.name, this.category, this.sampler, this.beforeTick, this.thresholdTest);
        }
    }
 
    public static class SamplerResult {
        private final Int2DoubleMap recording;
        private final int firstTick;
        private final int lastTick;
 
        public SamplerResult(int firstTick, int lastTick, Int2DoubleMap recording) {
            this.firstTick = firstTick;
            this.lastTick = lastTick;
            this.recording = recording;
        }
 
        public double valueAtTick(int tick) {
            return this.recording.get(tick);
        }
 
        public int getFirstTick() {
            return this.firstTick;
        }
 
        public int getLastTick() {
            return this.lastTick;
        }
    }
 
    public interface ThresholdTest {
        boolean test(final double value);
    }
 
    public static class ValueIncreasedByPercentage implements MetricSampler.ThresholdTest {
        private final float percentageIncreaseThreshold;
        private double previousValue = Double.MIN_VALUE;
 
        public ValueIncreasedByPercentage(float percentageIncreaseThreshold) {
            this.percentageIncreaseThreshold = percentageIncreaseThreshold;
        }
 
        @Override
        public boolean test(double value) {
            boolean result;
            if (this.previousValue != Double.MIN_VALUE && !(value <= this.previousValue)) {
                result = (value - this.previousValue) / this.previousValue >= this.percentageIncreaseThreshold;
            } else {
                result = false;
            }
 
            this.previousValue = value;
            return result;
        }
    }
}

引用的其他类