|
18 | 18 | import java.time.ZonedDateTime; |
19 | 19 | import java.util.ArrayList; |
20 | 20 | import java.util.List; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.CompletionStage; |
21 | 23 | import javax.annotation.Nonnull; |
22 | 24 | import org.junit.jupiter.api.AfterAll; |
23 | 25 | import org.junit.jupiter.api.BeforeEach; |
24 | | -import org.junit.jupiter.api.Disabled; |
25 | 26 | import org.junit.jupiter.api.Test; |
26 | 27 |
|
27 | 28 | class InventorySyncBenchmark { |
28 | 29 |
|
29 | 30 | private static final int INVENTORY_BENCHMARKS_CREATE_ACTION_THRESHOLD = |
30 | 31 | 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) |
31 | 36 |
|
32 | 37 | @BeforeEach |
33 | 38 | void setup() { |
@@ -89,19 +94,128 @@ void sync_NewInventories_ShouldCreateInventories() throws IOException { |
89 | 94 | } |
90 | 95 | } |
91 | 96 |
|
92 | | - @Disabled |
93 | 97 | @Test |
94 | 98 | 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 | + } |
97 | 155 | } |
98 | 156 |
|
99 | | - @Disabled |
100 | 157 | @Test |
101 | 158 | 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 | + } |
105 | 219 | } |
106 | 220 |
|
107 | 221 | @Nonnull |
|
0 commit comments