| 1 | package net.minecraft.util.profiling.jfr.stats;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import java.time.Duration;
|
| 5 | import java.util.Comparator;
|
| 6 | import java.util.List;
|
| 7 |
|
| 8 | public final class IoSummary<T> {
|
| 9 | private final IoSummary.CountAndSize totalCountAndSize;
|
| 10 | private final List<Pair<T, IoSummary.CountAndSize>> largestSizeContributors;
|
| 11 | private final Duration recordingDuration;
|
| 12 |
|
| 13 | public IoSummary(final Duration recordingDuration, final List<Pair<T, IoSummary.CountAndSize>> packetStats) {
|
| 14 | this.recordingDuration = recordingDuration;
|
| 15 | this.totalCountAndSize = packetStats.stream().map(Pair::getSecond).reduce(new IoSummary.CountAndSize(0L, 0L), IoSummary.CountAndSize::add);
|
| 16 | this.largestSizeContributors = packetStats.stream().sorted(Comparator.comparing(Pair::getSecond, IoSummary.CountAndSize.SIZE_THEN_COUNT)).limit(10L).toList();
|
| 17 | }
|
| 18 |
|
| 19 | public double getCountsPerSecond() {
|
| 20 | return (double)this.totalCountAndSize.totalCount / this.recordingDuration.getSeconds();
|
| 21 | }
|
| 22 |
|
| 23 | public double getSizePerSecond() {
|
| 24 | return (double)this.totalCountAndSize.totalSize / this.recordingDuration.getSeconds();
|
| 25 | }
|
| 26 |
|
| 27 | public long getTotalCount() {
|
| 28 | return this.totalCountAndSize.totalCount;
|
| 29 | }
|
| 30 |
|
| 31 | public long getTotalSize() {
|
| 32 | return this.totalCountAndSize.totalSize;
|
| 33 | }
|
| 34 |
|
| 35 | public List<Pair<T, IoSummary.CountAndSize>> largestSizeContributors() {
|
| 36 | return this.largestSizeContributors;
|
| 37 | }
|
| 38 |
|
| 39 | public record CountAndSize(long totalCount, long totalSize) {
|
| 40 | private static final Comparator<IoSummary.CountAndSize> SIZE_THEN_COUNT = Comparator.comparing(IoSummary.CountAndSize::totalSize)
|
| 41 | .thenComparing(IoSummary.CountAndSize::totalCount)
|
| 42 | .reversed();
|
| 43 |
|
| 44 | public IoSummary.CountAndSize add(final IoSummary.CountAndSize that) {
|
| 45 | return new IoSummary.CountAndSize(this.totalCount + that.totalCount, this.totalSize + that.totalSize);
|
| 46 | }
|
| 47 |
|
| 48 | public float averageSize() {
|
| 49 | return (float)this.totalSize / (float)this.totalCount;
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
| 53 |
|