| 1 | package net.minecraft.client.telemetry.events;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import com.google.common.base.Ticker;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.OptionalLong;
|
| 10 | import java.util.concurrent.TimeUnit;
|
| 11 | import java.util.function.Function;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.telemetry.TelemetryEventSender;
|
| 15 | import net.minecraft.client.telemetry.TelemetryEventType;
|
| 16 | import net.minecraft.client.telemetry.TelemetryProperty;
|
| 17 | import org.slf4j.Logger;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class GameLoadTimesEvent {
|
| 21 | public static final GameLoadTimesEvent INSTANCE = new GameLoadTimesEvent(Ticker.systemTicker());
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private final Ticker timeSource;
|
| 24 | private final Map<TelemetryProperty<GameLoadTimesEvent.Measurement>, Stopwatch> measurements = new HashMap<>();
|
| 25 | private OptionalLong bootstrapTime = OptionalLong.empty();
|
| 26 |
|
| 27 | protected GameLoadTimesEvent(final Ticker timeSource) {
|
| 28 | this.timeSource = timeSource;
|
| 29 | }
|
| 30 |
|
| 31 | public synchronized void beginStep(final TelemetryProperty<GameLoadTimesEvent.Measurement> property) {
|
| 32 | this.beginStep(property, p -> Stopwatch.createStarted(this.timeSource));
|
| 33 | }
|
| 34 |
|
| 35 | public synchronized void beginStep(final TelemetryProperty<GameLoadTimesEvent.Measurement> property, final Stopwatch measurement) {
|
| 36 | this.beginStep(property, p -> measurement);
|
| 37 | }
|
| 38 |
|
| 39 | private synchronized void beginStep(
|
| 40 | final TelemetryProperty<GameLoadTimesEvent.Measurement> property, final Function<TelemetryProperty<GameLoadTimesEvent.Measurement>, Stopwatch> measurement
|
| 41 | ) {
|
| 42 | this.measurements.computeIfAbsent(property, measurement);
|
| 43 | }
|
| 44 |
|
| 45 | public synchronized void endStep(final TelemetryProperty<GameLoadTimesEvent.Measurement> property) {
|
| 46 | Stopwatch stepMeasurement = this.measurements.get(property);
|
| 47 | if (stepMeasurement == null) {
|
| 48 | LOGGER.warn("Attempted to end step for {} before starting it", property.id());
|
| 49 | } else {
|
| 50 | if (stepMeasurement.isRunning()) {
|
| 51 | stepMeasurement.stop();
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | public void send(final TelemetryEventSender eventSender) {
|
| 57 | eventSender.send(TelemetryEventType.GAME_LOAD_TIMES, properties -> {
|
| 58 | synchronized (this) {
|
| 59 | this.measurements.forEach((key, stepMeasurement) -> {
|
| 60 | if (!stepMeasurement.isRunning()) {
|
| 61 | long elapsed = stepMeasurement.elapsed(TimeUnit.MILLISECONDS);
|
| 62 | properties.put((TelemetryProperty<GameLoadTimesEvent.Measurement>)key, new GameLoadTimesEvent.Measurement((int)elapsed));
|
| 63 | } else {
|
| 64 | LOGGER.warn("Measurement {} was discarded since it was still ongoing when the event {} was sent.", key.id(), TelemetryEventType.GAME_LOAD_TIMES.id());
|
| 65 | }
|
| 66 | });
|
| 67 | this.bootstrapTime.ifPresent(duration -> properties.put(TelemetryProperty.LOAD_TIME_BOOTSTRAP_MS, new GameLoadTimesEvent.Measurement((int)duration)));
|
| 68 | this.measurements.clear();
|
| 69 | }
|
| 70 | });
|
| 71 | }
|
| 72 |
|
| 73 | public synchronized void setBootstrapTime(final long duration) {
|
| 74 | this.bootstrapTime = OptionalLong.of(duration);
|
| 75 | }
|
| 76 |
|
| 77 | @Environment(EnvType.CLIENT)
|
| 78 | public record Measurement(int millis) {
|
| 79 | public static final Codec<GameLoadTimesEvent.Measurement> CODEC = Codec.INT.xmap(GameLoadTimesEvent.Measurement::new, o -> o.millis);
|
| 80 | }
|
| 81 | }
|
| 82 |
|