MetricsRegistry .java

net.minecraft.util.profiling.metrics.MetricsRegistry (class): members: AggregatedMetricSampler, getRegisteredSamplers, measuredInstances, INSTANCE, add.

net.minecraft.util.profiling.metricsclass97 linescommon2.9 KB

API Surface (4)

void add(final ProfilerMeasured profilerMeasured)
List<MetricSampler> getRegisteredSamplers()
boolean equals(final @Nullable Object o)
int hashCode()

Fields & Constants (2)

INSTANCE
measuredInstances

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

MetricsRegistry.java97 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.profiling.metrics;
2
3
import java.util.List;
4
import java.util.Map;
5
import java.util.Objects;
6
import java.util.WeakHashMap;
7
import java.util.stream.Collectors;
8
import org.jspecify.annotations.Nullable;
9
10
public class MetricsRegistry {
11
	public static final MetricsRegistry INSTANCE = new MetricsRegistry();
12
	private final WeakHashMap<ProfilerMeasured, Void> measuredInstances = new WeakHashMap<>();
13
14
	private MetricsRegistry() {
15
	}
16
17
	public void add(final ProfilerMeasured profilerMeasured) {
18
		this.measuredInstances.put(profilerMeasured, null);
19
	}
20
21
	public List<MetricSampler> getRegisteredSamplers() {
22
		Map<String, List<MetricSampler>> samplersByName = this.measuredInstances
23
			.keySet()
24
			.stream()
25
			.flatMap(measuredInstance -> measuredInstance.profiledMetrics().stream())
26
			.collect(Collectors.groupingBy(MetricSampler::getName));
27
		return aggregateDuplicates(samplersByName);
28
	}
29
30
	private static List<MetricSampler> aggregateDuplicates(final Map<String, List<MetricSampler>> potentialDuplicates) {
31
		return potentialDuplicates.entrySet().stream().map(entry -> {
32
			String samplerName = entry.getKey();
33
			List<MetricSampler> duplicateSamplers = entry.getValue();
34
			return duplicateSamplers.size() > 1 ? new MetricsRegistry.AggregatedMetricSampler(samplerName, duplicateSamplers) : duplicateSamplers.get(0);
35
		}).collect(Collectors.toList());
36
	}
37
38
	private static class AggregatedMetricSampler extends MetricSampler {
39
		private final List<MetricSampler> delegates;
40
41
		private AggregatedMetricSampler(final String name, final List<MetricSampler> delegates) {
42
			super(
43
				name,
44
				MetricSampler.SamplingPhase.END_TICK,
45
				delegates.get(0).getCategory(),
46
				() -> averageValueFromDelegates(delegates),
47
				() -> beforeTick(delegates),
48
				thresholdTest(delegates)
49
			);
50
			this.delegates = delegates;
51
		}
52
53
		private static MetricSampler.ThresholdTest thresholdTest(final List<MetricSampler> delegates) {
54
			return value -> delegates.stream().anyMatch(delegate -> delegate.thresholdTest != null ? delegate.thresholdTest.test(value) : false);
55
		}
56
57
		private static void beforeTick(final List<MetricSampler> delegates) {
58
			for (MetricSampler delegate : delegates) {
59
				delegate.onStartTick();
60
			}
61
		}
62
63
		private static double averageValueFromDelegates(final List<MetricSampler> delegates) {
64
			double aggregatedValue = 0.0;
65
66
			for (MetricSampler delegate : delegates) {
67
				aggregatedValue += delegate.getSampler().getAsDouble();
68
			}
69
70
			return aggregatedValue / delegates.size();
71
		}
72
73
		@Override
74
		public boolean equals(final @Nullable Object o) {
75
			if (this == o) {
76
				return true;
77
			}
78
79
			if (o == null || this.getClass() != o.getClass()) {
80
				return false;
81
			}
82
83
			if (!super.equals(o)) {
84
				return false;
85
			}
86
87
			MetricsRegistry.AggregatedMetricSampler that = (MetricsRegistry.AggregatedMetricSampler)o;
88
			return this.delegates.equals(that.delegates);
89
		}
90
91
		@Override
92
		public int hashCode() {
93
			return Objects.hash(super.hashCode(), this.delegates);
94
		}
95
	}
96
}
97

Decompiled Minecraft 26.3 source of MetricsRegistry (net/minecraft/util/profiling/metrics/MetricsRegistry.java) with Mojang official mappings, stored verbatim on the site.