Skip to content

Perf test #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id "io.github.gradle-nexus.publish-plugin" version "1.3.0"
id "org.owasp.dependencycheck" version "9.0.9"
id "me.champeau.jmh" version "0.7.2" apply false
id 'signing'
}

Expand Down Expand Up @@ -30,6 +31,7 @@ subprojects {
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'org.owasp.dependencycheck'
apply plugin: 'me.champeau.jmh'

repositories {
mavenCentral()
Expand Down Expand Up @@ -57,6 +59,9 @@ subprojects {

dependencies {
implementation 'io.prometheus:simpleclient_dropwizard:0.16.0'

jmh 'org.openjdk.jmh:jmh-core:1.37'
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.37'
}

jar {
Expand All @@ -79,6 +84,14 @@ subprojects {
withSourcesJar()
}

jmh {
jmhTimeout = "1m"
iterations = 3
fork = 2
warmupIterations = 3
warmupForks = 2
}

// conditionals for publications
tasks.withType(PublishToMavenRepository).configureEach {
onlyIf {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.exactpro.cradle.cassandra.dao.testevents;

import com.exactpro.cradle.BookId;
import com.exactpro.cradle.Direction;
import com.exactpro.cradle.PageId;
import com.exactpro.cradle.messages.StoredMessageId;
import com.exactpro.cradle.testevents.StoredTestEventId;
import com.exactpro.cradle.testevents.TestEventBatchToStore;
import com.exactpro.cradle.testevents.TestEventBatchToStoreBuilder;
import com.exactpro.cradle.testevents.TestEventSingleToStore;
import com.exactpro.cradle.testevents.TestEventSingleToStoreBuilder;
import com.exactpro.cradle.utils.CompressException;
import com.exactpro.cradle.utils.CompressionType;
import com.exactpro.cradle.utils.CradleStorageException;
import org.apache.commons.lang3.RandomStringUtils;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.io.IOException;
import java.time.Instant;
import java.util.UUID;

import static com.exactpro.cradle.CoreStorageSettings.DEFAULT_BOOK_REFRESH_INTERVAL_MILLIS;
import static com.exactpro.cradle.CradleStorage.DEFAULT_MAX_TEST_EVENT_BATCH_SIZE;
import static com.exactpro.cradle.cassandra.CassandraStorageSettings.DEFAULT_MAX_UNCOMPRESSED_MESSAGE_BATCH_SIZE;
import static org.openjdk.jmh.annotations.Mode.Throughput;

@State(Scope.Benchmark)
public class TestEventEntityUtilsBenchmark {
private static final BookId BOOK_ID = new BookId("benchmark-book");
private static final PageId PAGE_ID = new PageId(BOOK_ID, Instant.now(), "benchmark-page");
private static final String SCOPE = "benchmark-scope";
private static final String SESSION_ALIAS_PREFIX = "benchmark-alias-";
private static final String EVENT_NAME_PREFIX = "benchmark-event-";
private static final int CONTENT_SIZE = 500;
private static final int EVENT_NUMBER = 100;
private static final int SESSION_ALIAS_NUMBER = 5;
private static final int MESSAGES_PER_DIRECTION = 2;
@State(Scope.Thread)
public static class EventBatchState {
private TestEventBatchToStore batch;
@Setup
public void init() throws CradleStorageException {
StoredTestEventId parentId = new StoredTestEventId(BOOK_ID, SCOPE, Instant.now(), UUID.randomUUID().toString());
TestEventBatchToStoreBuilder batchBuilder = TestEventBatchToStore.builder(DEFAULT_MAX_TEST_EVENT_BATCH_SIZE, DEFAULT_BOOK_REFRESH_INTERVAL_MILLIS)
.id(BOOK_ID, SCOPE, Instant.now(), UUID.randomUUID().toString())
.parentId(parentId);

int seqCounter = 0;
for (int eventIndex = 0; eventIndex < EVENT_NUMBER; eventIndex++) {
TestEventSingleToStoreBuilder eventBuilder = TestEventSingleToStore.builder(DEFAULT_BOOK_REFRESH_INTERVAL_MILLIS)
.id(BOOK_ID, SCOPE, Instant.now(), UUID.randomUUID().toString())
.parentId(parentId)
.name(EVENT_NAME_PREFIX + eventIndex)
.content(RandomStringUtils.random(CONTENT_SIZE, true, true).getBytes());

for (int aliasIndex = 0; aliasIndex < SESSION_ALIAS_NUMBER; aliasIndex++) {
for (Direction direction : Direction.values()) {
for (int msgIndex = 0; msgIndex < MESSAGES_PER_DIRECTION; msgIndex++) {
eventBuilder.message(new StoredMessageId(BOOK_ID, SESSION_ALIAS_PREFIX + aliasIndex, direction, Instant.now(), ++seqCounter));
}
}
}
batchBuilder.addTestEvent(eventBuilder.build());
}
batch = batchBuilder.build();
}
}

@Benchmark
@BenchmarkMode({Throughput})
public void benchmarkSerializeBatchLinkedMessageIds(EventBatchState state) throws IOException, CompressException {
TestEventEntityUtils.toSerializedEntity(state.batch, PAGE_ID, CompressionType.LZ4, DEFAULT_MAX_UNCOMPRESSED_MESSAGE_BATCH_SIZE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ protected void doStoreTestEvent(TestEventToStore event, PageInfo page) throws IO
PageId pageId = page.getId();
try
{
eventsWorker.storeEvent(event, pageId);
eventsWorker.storeEvent(event, pageId).get();
eventsWorker.storeScope(event).get();
eventsWorker.storePageScope(event, pageId).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class CassandraStorageSettings extends CoreStorageSettings {
public static final int DEFAULT_COUNTER_PERSISTENCE_INTERVAL_MS = 1000;
public static final long DEFAULT_EVENT_BATCH_DURATION_MILLIS = 5_000;
public static final long DEFAULT_TIMEOUT = 5000;
public static final CompressionType DEFAULT_COMPRESSION_TYPE = CompressionType.ZLIB;
public static final CompressionType DEFAULT_COMPRESSION_TYPE = CompressionType.LZ4;

//we need to use Instant.EPOCH instead of Instant.MIN.
//when cassandra driver tries to convert Instant.MIN to milliseconds using toEpochMilli() it causes long overflow.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 Exactpro (Exactpro Systems Limited)
* Copyright 2020-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +42,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.zip.DataFormatException;
Expand All @@ -56,7 +57,9 @@ public static StoredTestEvent toStoredTestEvent(TestEventEntity testEventEntity,
logger.trace("Creating test event '{}' from entity", eventId);

byte[] content = restoreContent(testEventEntity, eventId);
return testEventEntity.isEventBatch() ? toStoredTestEventBatch(testEventEntity, pageId, eventId, content) : toStoredTestEventSingle(testEventEntity, pageId, eventId, content);
return testEventEntity.isEventBatch()
? toStoredTestEventBatch(testEventEntity, pageId, eventId, content)
: toStoredTestEventSingle(testEventEntity, pageId, eventId, content);
}


Expand Down Expand Up @@ -87,21 +90,21 @@ private static byte[] restoreContent(TestEventEntity testEventEntity, StoredTest
private static Set<StoredMessageId> restoreMessages(TestEventEntity testEventEntity, BookId bookId)
throws IOException {
ByteBuffer messages = testEventEntity.getMessages();
if (messages == null)
return null;
if (messages == null) {
return Collections.emptySet();
}

byte[] result = messages.array();
return TestEventUtils.deserializeLinkedMessageIds(result, bookId);
return TestEventUtils.deserializeLinkedMessageIds(messages, bookId);
}

private static Map<StoredTestEventId, Set<StoredMessageId>> restoreBatchMessages(TestEventEntity testEventEntity, BookId bookId)
private static Map<StoredTestEventId, Set<StoredMessageId>> restoreBatchMessages(TestEventEntity testEventEntity, BookId bookId, String scope)
throws IOException {
ByteBuffer messages = testEventEntity.getMessages();
if (messages == null)
return null;
if (messages == null) {
return Collections.emptyMap();
}

byte[] result = messages.array();
return TestEventUtils.deserializeBatchLinkedMessageIds(result, bookId);
return TestEventUtils.deserializeBatchLinkedMessageIds(messages, bookId, scope);
}


Expand All @@ -117,7 +120,7 @@ private static StoredTestEventBatch toStoredTestEventBatch(TestEventEntity testE
throws IOException, CradleStorageException, CradleIdException
{
Collection<BatchedStoredTestEvent> children = TestEventUtils.deserializeTestEvents(content, eventId);
Map<StoredTestEventId, Set<StoredMessageId>> messages = restoreBatchMessages(testEventEntity, pageId.getBookId());
Map<StoredTestEventId, Set<StoredMessageId>> messages = restoreBatchMessages(testEventEntity, pageId.getBookId(), eventId.getScope());
return new StoredTestEventBatch(eventId, testEventEntity.getName(), testEventEntity.getType(), createParentId(testEventEntity),
children, messages, pageId, null, testEventEntity.getRecDate());
}
Expand All @@ -133,7 +136,7 @@ public static Instant getStartTimestamp(TestEventEntity entity) {
public static SerializedEntity<SerializedEntityMetadata, TestEventEntity> toSerializedEntity(TestEventToStore event,
PageId pageId,
CompressionType compressionType,
int maxUncompressedSize) throws IOException, CompressException {
int maxUncompressedSize) throws CompressException {
TestEventEntity.TestEventEntityBuilder builder = TestEventEntity.builder();

logger.debug("Creating entity from test event '{}'", event.getId());
Expand All @@ -155,7 +158,7 @@ public static SerializedEntity<SerializedEntityMetadata, TestEventEntity> toSeri
builder.setContentSize(content.length);
}

byte[] messages = TestEventUtils.serializeLinkedMessageIds(event);
ByteBuffer messages = TestEventUtils.serializeLinkedMessageIds(event);

StoredTestEventId parentId = event.getParentId();
LocalDateTime start = TimeUtils.toLocalTimestamp(event.getStartTimestamp());
Expand All @@ -172,12 +175,14 @@ public static SerializedEntity<SerializedEntityMetadata, TestEventEntity> toSeri
builder.setName(event.getName());
builder.setType(event.getType());
builder.setParentId(parentId != null ? parentId.toString() : ""); //Empty string for absent parentId allows using index to get root events
if (event.isBatch())
builder.setEventCount(event.asBatch().getTestEventsCount());
if (event.isBatch()) {
builder.setEventCount(event.asBatch().getTestEvents().size());
}
builder.setEndTimestamp(event.getEndTimestamp());

if (messages != null)
builder.setMessages(ByteBuffer.wrap(messages));
if (messages != null) {
builder.setMessages(messages);
}

builder.setCompressed(compressed);
//TODO: this.setLabels(event.getLabels());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.exactpro.cradle.serialization.SerializedEntityMetadata;
import com.exactpro.cradle.testevents.StoredTestEvent;
import com.exactpro.cradle.testevents.StoredTestEventId;
import com.exactpro.cradle.testevents.StoredTestEventIdUtils;
import com.exactpro.cradle.testevents.TestEventFilter;
import com.exactpro.cradle.testevents.TestEventToStore;
import com.exactpro.cradle.utils.CompressException;
Expand All @@ -59,6 +60,7 @@
import java.util.concurrent.CompletionException;
import java.util.zip.DataFormatException;

import static com.exactpro.cradle.testevents.StoredTestEventIdUtils.track;
import static java.util.Objects.requireNonNull;

public class EventsWorker extends Worker {
Expand Down Expand Up @@ -144,40 +146,48 @@ public CompletableFuture<Void> storeEvent(TestEventToStore event, PageId pageId)
TestEventOperator op = getOperators().getTestEventOperator();
BookStatisticsRecordsCaches.EntityKey key = new BookStatisticsRecordsCaches.EntityKey(pageId.getName(), EntityType.EVENT);

track(event, "wait serialize");
return CompletableFuture.supplyAsync(() -> {
try {
return TestEventEntityUtils.toSerializedEntity(event, pageId, settings.getCompressionType(), settings.getMaxUncompressedMessageBatchSize());
try (AutoCloseable ignored = StoredTestEventIdUtils.Statistic.measure("serialize")) {
track(event, "serializing");
var serializedEntity = TestEventEntityUtils.toSerializedEntity(event, pageId, settings.getCompressionType(), settings.getMaxUncompressedMessageBatchSize());
track(event, "serialized");
return serializedEntity;
} catch (Exception e) {
throw new CompletionException(e);
}
}, composingService).thenCompose(serializedEntity -> {
AutoCloseable measure = StoredTestEventIdUtils.Statistic.measure("write");
TestEventEntity entity = serializedEntity.getEntity();
List<SerializedEntityMetadata> meta = serializedEntity.getSerializedEntityData().getSerializedEntityMetadata();

return op.write(entity, writeAttrs)
.thenRun(() -> { try { measure.close(); } catch (Exception e) { throw new RuntimeException(e); }})
.thenAcceptAsync(result -> {
try {
Instant firstTimestamp = meta.get(0).getTimestamp();
Instant lastStartTimestamp = firstTimestamp;
for (SerializedEntityMetadata el : meta) {
if (el.getTimestamp() != null) {
if (firstTimestamp.isAfter(el.getTimestamp())) {
firstTimestamp = el.getTimestamp();
}
if (lastStartTimestamp.isBefore(el.getTimestamp())) {
lastStartTimestamp = el.getTimestamp();
try(AutoCloseable ignored = StoredTestEventIdUtils.Statistic.measure("update-statistics")) {
try {
Instant firstTimestamp = meta.get(0).getTimestamp();
Instant lastStartTimestamp = firstTimestamp;
for (SerializedEntityMetadata el : meta) {
if (el.getTimestamp() != null) {
if (firstTimestamp.isAfter(el.getTimestamp())) {
firstTimestamp = el.getTimestamp();
}
if (lastStartTimestamp.isBefore(el.getTimestamp())) {
lastStartTimestamp = el.getTimestamp();
}
}
}
durationWorker.updateMaxDuration(pageId, entity.getScope(),
Duration.between(firstTimestamp, lastStartTimestamp).toMillis(),
writeAttrs);
} catch (CradleStorageException e) {
logger.error("Exception while updating max duration {}", e.getMessage());
}
durationWorker.updateMaxDuration(pageId, entity.getScope(),
Duration.between(firstTimestamp, lastStartTimestamp).toMillis(),
writeAttrs);
} catch (CradleStorageException e) {
logger.error("Exception while updating max duration {}", e.getMessage());
}

entityStatisticsCollector.updateEntityBatchStatistics(pageId.getBookId(), key, meta);
updateEventWriteMetrics(entity, pageId.getBookId());
entityStatisticsCollector.updateEntityBatchStatistics(pageId.getBookId(), key, meta);
updateEventWriteMetrics(entity, pageId.getBookId());
} catch (Exception e) { throw new RuntimeException(e); }
}, composingService);
});
}
Expand Down
Loading
Loading