Skip to content

Commit d4ec809

Browse files
authored
Add benchmarks for inventorysync updates and mixed (#1131)
1 parent 2a34a9a commit d4ec809

File tree

1 file changed

+122
-8
lines changed

1 file changed

+122
-8
lines changed

src/benchmark/java/com/commercetools/sync/benchmark/InventorySyncBenchmark.java

Lines changed: 122 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@
1818
import java.time.ZonedDateTime;
1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.concurrent.CompletableFuture;
22+
import java.util.concurrent.CompletionStage;
2123
import javax.annotation.Nonnull;
2224
import org.junit.jupiter.api.AfterAll;
2325
import org.junit.jupiter.api.BeforeEach;
24-
import org.junit.jupiter.api.Disabled;
2526
import org.junit.jupiter.api.Test;
2627

2728
class InventorySyncBenchmark {
2829

2930
private static final int INVENTORY_BENCHMARKS_CREATE_ACTION_THRESHOLD =
3031
18_000; // (based on history of benchmarks; highest was ~9 seconds)
32+
private static final int INVENTORY_BENCHMARKS_UPDATE_ACTION_THRESHOLD =
33+
18_000; // (based on history of benchmarks; highest was ~9 seconds)
34+
private static final int INVENTORY_BENCHMARKS_CREATE_AND_UPDATE_ACTION_THRESHOLD =
35+
18_000; // (based on history of benchmarks; highest was ~9 seconds)
3136

3237
@BeforeEach
3338
void setup() {
@@ -89,19 +94,128 @@ void sync_NewInventories_ShouldCreateInventories() throws IOException {
8994
}
9095
}
9196

92-
@Disabled
9397
@Test
9498
void sync_ExistingInventories_ShouldUpdateInventories() throws IOException {
95-
// TODO: SHOULD BE IMPLEMENTED.
96-
BenchmarkUtils.saveNewResult(BenchmarkUtils.INVENTORY_SYNC, BenchmarkUtils.UPDATES_ONLY, 50000);
99+
// preparation
100+
final List<InventoryEntryDraft> inventoryEntryDrafts =
101+
buildInventoryDrafts(BenchmarkUtils.NUMBER_OF_RESOURCE_UNDER_TEST);
102+
// Create drafts to target project with different quantity
103+
CompletableFuture.allOf(
104+
inventoryEntryDrafts.stream()
105+
.map(InventoryEntryDraftBuilder::of)
106+
.map(builder -> builder.quantityOnStock(0L))
107+
.map(InventoryEntryDraftBuilder::build)
108+
.map(draft -> CTP_TARGET_CLIENT.inventory().create(draft).execute())
109+
.map(CompletionStage::toCompletableFuture)
110+
.toArray(CompletableFuture[]::new))
111+
.join();
112+
113+
final InventorySyncOptions inventorySyncOptions =
114+
InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
115+
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
116+
117+
// benchmark
118+
final long beforeSync = System.currentTimeMillis();
119+
final InventorySyncStatistics inventorySyncStatistics =
120+
inventorySync.sync(inventoryEntryDrafts).toCompletableFuture().join();
121+
final long totalTime = System.currentTimeMillis() - beforeSync;
122+
123+
// assert on threshold
124+
assertThat(totalTime)
125+
.withFailMessage(
126+
String.format(
127+
BenchmarkUtils.THRESHOLD_EXCEEDED_ERROR,
128+
totalTime,
129+
INVENTORY_BENCHMARKS_UPDATE_ACTION_THRESHOLD))
130+
.isLessThan(INVENTORY_BENCHMARKS_UPDATE_ACTION_THRESHOLD);
131+
132+
// Assert actual state of CTP project (total number of existing inventories)
133+
final Long totalNumberOfInventories =
134+
CTP_TARGET_CLIENT
135+
.inventory()
136+
.get()
137+
.execute()
138+
.thenApply(ApiHttpResponse::getBody)
139+
.thenApply(InventoryPagedQueryResponse::getTotal)
140+
.toCompletableFuture()
141+
.join();
142+
assertThat(totalNumberOfInventories).isEqualTo(BenchmarkUtils.NUMBER_OF_RESOURCE_UNDER_TEST);
143+
144+
// Assert on sync statistics
145+
assertThat(inventorySyncStatistics)
146+
.hasValues(
147+
BenchmarkUtils.NUMBER_OF_RESOURCE_UNDER_TEST,
148+
0,
149+
BenchmarkUtils.NUMBER_OF_RESOURCE_UNDER_TEST,
150+
0);
151+
if (BenchmarkUtils.SUBMIT_BENCHMARK_RESULT) {
152+
BenchmarkUtils.saveNewResult(
153+
BenchmarkUtils.INVENTORY_SYNC, BenchmarkUtils.UPDATES_ONLY, totalTime);
154+
}
97155
}
98156

99-
@Disabled
100157
@Test
101158
void sync_WithSomeExistingInventories_ShouldSyncInventories() throws IOException {
102-
// TODO: SHOULD BE IMPLEMENTED.
103-
BenchmarkUtils.saveNewResult(
104-
BenchmarkUtils.INVENTORY_SYNC, BenchmarkUtils.CREATES_AND_UPDATES, 30000);
159+
// preparation
160+
final List<InventoryEntryDraft> inventoryEntryDrafts =
161+
buildInventoryDrafts(BenchmarkUtils.NUMBER_OF_RESOURCE_UNDER_TEST);
162+
final int halfNumberOfDrafts = inventoryEntryDrafts.size() / 2;
163+
final List<InventoryEntryDraft> firstHalf = inventoryEntryDrafts.subList(0, halfNumberOfDrafts);
164+
165+
// Create first half of drafts to target project with different quantity
166+
CompletableFuture.allOf(
167+
firstHalf.stream()
168+
.map(InventoryEntryDraftBuilder::of)
169+
.map(builder -> builder.quantityOnStock(0L))
170+
.map(InventoryEntryDraftBuilder::build)
171+
.map(draft -> CTP_TARGET_CLIENT.inventory().post(draft).execute())
172+
.map(CompletionStage::toCompletableFuture)
173+
.toArray(CompletableFuture[]::new))
174+
.join();
175+
176+
final InventorySyncOptions inventorySyncOptions =
177+
InventorySyncOptionsBuilder.of(CTP_TARGET_CLIENT).build();
178+
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
179+
180+
// benchmark
181+
final long beforeSync = System.currentTimeMillis();
182+
final InventorySyncStatistics inventorySyncStatistics =
183+
inventorySync.sync(inventoryEntryDrafts).toCompletableFuture().join();
184+
final long totalTime = System.currentTimeMillis() - beforeSync;
185+
186+
// assert on threshold
187+
assertThat(totalTime)
188+
.withFailMessage(
189+
String.format(
190+
BenchmarkUtils.THRESHOLD_EXCEEDED_ERROR,
191+
totalTime,
192+
INVENTORY_BENCHMARKS_CREATE_AND_UPDATE_ACTION_THRESHOLD))
193+
.isLessThan(INVENTORY_BENCHMARKS_CREATE_AND_UPDATE_ACTION_THRESHOLD);
194+
195+
// Assert actual state of CTP project (total number of existing inventories)
196+
final Long totalNumberOfInventories =
197+
CTP_TARGET_CLIENT
198+
.inventory()
199+
.get()
200+
.execute()
201+
.thenApply(ApiHttpResponse::getBody)
202+
.thenApply(InventoryPagedQueryResponse::getTotal)
203+
.toCompletableFuture()
204+
.join();
205+
206+
assertThat(totalNumberOfInventories).isEqualTo(BenchmarkUtils.NUMBER_OF_RESOURCE_UNDER_TEST);
207+
208+
// Assert on sync statistics
209+
assertThat(inventorySyncStatistics)
210+
.hasValues(
211+
BenchmarkUtils.NUMBER_OF_RESOURCE_UNDER_TEST,
212+
halfNumberOfDrafts,
213+
halfNumberOfDrafts,
214+
0);
215+
if (BenchmarkUtils.SUBMIT_BENCHMARK_RESULT) {
216+
BenchmarkUtils.saveNewResult(
217+
BenchmarkUtils.INVENTORY_SYNC, BenchmarkUtils.CREATES_AND_UPDATES, totalTime);
218+
}
105219
}
106220

107221
@Nonnull

0 commit comments

Comments
 (0)