Skip to content

Commit be2ae00

Browse files
feat(18458): add eventSize metrics
Signed-off-by: mxtartaglia <[email protected]>
1 parent 8c97a12 commit be2ae00

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/preconsensus/CommonPcesWriter.java

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static com.swirlds.common.units.DataUnit.UNIT_MEGABYTES;
66
import static com.swirlds.logging.legacy.LogMarker.EXCEPTION;
77

8+
import com.hedera.hapi.platform.event.GossipEvent;
89
import com.swirlds.common.context.PlatformContext;
910
import com.swirlds.common.utility.LongRunningAverage;
1011
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -251,6 +252,7 @@ private void pruneOldFiles() {
251252
*/
252253
public boolean prepareOutputStream(@NonNull final PlatformEvent eventToWrite) throws IOException {
253254
boolean fileClosed = false;
255+
fileManager.updateEventSizeMetric(GossipEvent.PROTOBUF.measureRecord(eventToWrite.getGossipEvent()));
254256
if (currentMutableFile != null) {
255257
final boolean fileCanContainEvent = currentMutableFile.canContain(fileType.selectIndicator(eventToWrite));
256258
final boolean fileIsFull =

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/preconsensus/PcesFileManager.java

+7
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,11 @@ private void updateFileSizeMetrics() {
300300
.set(((double) totalFileByteCount) / files.getFileCount() * UnitConstants.BYTES_TO_MEBIBYTES);
301301
}
302302
}
303+
304+
/**
305+
* Update metrics with the event size.
306+
*/
307+
public void updateEventSizeMetric(final int i) {
308+
metrics.updateAvgEventSize(i);
309+
}
303310
}

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/preconsensus/PcesMetrics.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
import com.swirlds.metrics.api.DoubleGauge;
77
import com.swirlds.metrics.api.LongGauge;
88
import com.swirlds.metrics.api.Metrics;
9+
import edu.umd.cs.findbugs.annotations.NonNull;
10+
import java.util.Objects;
911

1012
/**
1113
* Metrics for preconsensus events.
1214
*/
1315
public class PcesMetrics {
1416

1517
private static final String CATEGORY = "platform";
18+
private final Metrics metrics;
1619

1720
private static final LongGauge.Config PRECONSENSUS_EVENT_FILE_COUNT_CONFIG = new LongGauge.Config(
1821
CATEGORY, "preconsensusEventFileCount")
@@ -68,12 +71,16 @@ public class PcesMetrics {
6871
.withDescription("The age of the oldest preconsensus event file, in seconds.");
6972
private final LongGauge preconsensusEventFileOldestSeconds;
7073

74+
static final RunningAverageMetric.Config AVG_EVENT_SIZE =
75+
new RunningAverageMetric.Config(CATEGORY, "avgEventSize").withDescription("The average size of an event");
76+
7177
/**
7278
* Construct preconsensus event metrics.
7379
*
7480
* @param metrics the metrics manager for the platform
7581
*/
76-
public PcesMetrics(final Metrics metrics) {
82+
public PcesMetrics(final @NonNull Metrics metrics) {
83+
this.metrics = Objects.requireNonNull(metrics);
7784
preconsensusEventFileCount = metrics.getOrCreate(PRECONSENSUS_EVENT_FILE_COUNT_CONFIG);
7885
preconsensusEventFileAverageSizeMB = metrics.getOrCreate(PRECONSENSUS_EVENT_FILE_AVERAGE_SIZE_MB_CONFIG);
7986
preconsensusEventFileTotalSizeGB = metrics.getOrCreate(PRECONSENSUS_EVENT_FILE_TOTAL_SIZE_GB_CONFIG);
@@ -149,4 +156,11 @@ public LongGauge getPreconsensusEventFileYoungestIdentifier() {
149156
public LongGauge getPreconsensusEventFileOldestSeconds() {
150157
return preconsensusEventFileOldestSeconds;
151158
}
159+
160+
/**
161+
* Updates the event size
162+
*/
163+
public void updateAvgEventSize(final int i) {
164+
metrics.getOrCreate(AVG_EVENT_SIZE).update(i);
165+
}
152166
}

platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/event/preconsensus/CommonPcesWriterTest.java

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.swirlds.platform.event.preconsensus;
33

4+
import static org.hiero.base.utility.test.fixtures.RandomUtils.getRandomPrintSeed;
45
import static org.junit.jupiter.api.Assertions.assertEquals;
56
import static org.junit.jupiter.api.Assertions.assertFalse;
67
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -11,14 +12,12 @@
1112
import static org.mockito.Mockito.verify;
1213
import static org.mockito.Mockito.when;
1314

14-
import com.hedera.hapi.platform.event.EventDescriptor;
15-
import com.hedera.pbj.runtime.io.buffer.Bytes;
1615
import com.swirlds.common.context.PlatformContext;
1716
import com.swirlds.config.api.Configuration;
1817
import com.swirlds.config.extensions.test.fixtures.TestConfigBuilder;
18+
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
1919
import java.io.IOException;
20-
import org.hiero.consensus.model.crypto.DigestType;
21-
import org.hiero.consensus.model.event.EventDescriptorWrapper;
20+
import java.util.Random;
2221
import org.hiero.consensus.model.event.PlatformEvent;
2322
import org.hiero.consensus.model.hashgraph.EventWindow;
2423
import org.junit.jupiter.api.BeforeEach;
@@ -30,13 +29,24 @@ class CommonPcesWriterTest {
3029
private PcesFileManager fileManager;
3130
private CommonPcesWriter commonPcesWriter;
3231
private PcesMutableFile pcesMutableFile;
32+
private PlatformEvent event;
3333

3434
@BeforeEach
3535
void setUp() throws Exception {
3636
final Configuration configuration = new TestConfigBuilder().getOrCreateConfig();
3737
final PlatformContext platformContext = mock(PlatformContext.class);
3838
when(platformContext.getConfiguration()).thenReturn(configuration);
39-
39+
final Random random = getRandomPrintSeed();
40+
event = new TestingEventBuilder(random)
41+
.setAppTransactionCount(3)
42+
.setSystemTransactionCount(1)
43+
.setSelfParent(
44+
new TestingEventBuilder(random).setBirthRound(150).build())
45+
.setOtherParent(
46+
new TestingEventBuilder(random).setBirthRound(150).build())
47+
.overrideOtherParentGeneration(150)
48+
.setBirthRound(150)
49+
.build();
4050
fileManager = mock(PcesFileManager.class);
4151
final PcesFile pcesFile = mock(PcesFile.class);
4252
when(fileManager.getNextFileDescriptor(anyLong(), anyLong())).thenReturn(pcesFile);
@@ -63,7 +73,7 @@ void testBeginStreamingNewEventsAlreadyStreaming() {
6373
@Test
6474
void testRegisterDiscontinuity() throws IOException {
6575
commonPcesWriter.beginStreamingNewEvents();
66-
commonPcesWriter.prepareOutputStream(mock(PlatformEvent.class));
76+
commonPcesWriter.prepareOutputStream(event);
6777
commonPcesWriter.registerDiscontinuity(10L);
6878

6979
// Verify file closing and file manager interactions
@@ -92,22 +102,14 @@ void testSetMinimumAncientIdentifierToStore() throws IOException {
92102

93103
@Test
94104
void testPrepareOutputStreamCreatesNewFile() throws IOException {
95-
PlatformEvent mockEvent = mock(PlatformEvent.class);
96-
when(mockEvent.getDescriptor())
97-
.thenReturn(new EventDescriptorWrapper(EventDescriptor.newBuilder()
98-
.birthRound(150)
99-
.generation(150)
100-
.hash(Bytes.wrap(new byte[DigestType.SHA_384.digestLength()]))
101-
.build()));
102-
103-
boolean fileClosed = commonPcesWriter.prepareOutputStream(mockEvent);
105+
boolean fileClosed = commonPcesWriter.prepareOutputStream(event);
104106
assertFalse(fileClosed, "A new file should have been created but not closed.");
105107
}
106108

107109
@Test
108110
void testCloseCurrentMutableFile() throws IOException {
109111
commonPcesWriter.beginStreamingNewEvents();
110-
commonPcesWriter.prepareOutputStream(mock(PlatformEvent.class));
112+
commonPcesWriter.prepareOutputStream(event);
111113
commonPcesWriter.closeCurrentMutableFile();
112114
verify(pcesMutableFile, times(1)).close();
113115
}

0 commit comments

Comments
 (0)