|
| 1 | +package com.commercetools.sync.benchmark; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 6 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 7 | + |
| 8 | +import javax.annotation.Nonnull; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.charset.Charset; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.Iterator; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.Spliterator; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import static java.util.Optional.ofNullable; |
| 22 | +import static java.util.Spliterators.spliteratorUnknownSize; |
| 23 | +import static java.util.stream.StreamSupport.stream; |
| 24 | + |
| 25 | +public class BenchmarkUtils { |
| 26 | + private static final String BENCHMARK_RESULTS_FILE_NAME = "benchmarks.json"; |
| 27 | + private static final String BENCHMARK_RESULTS_FILE_DIR = ofNullable(System.getenv("CI_BUILD_DIR")) |
| 28 | + .map(path -> path + "/tmp_git_dir/benchmarks/").orElse(""); |
| 29 | + private static final String BENCHMARK_RESULTS_FILE_PATH = BENCHMARK_RESULTS_FILE_DIR + BENCHMARK_RESULTS_FILE_NAME; |
| 30 | + private static final Charset UTF8_CHARSET = StandardCharsets.UTF_8; |
| 31 | + private static final String EXECUTION_TIMES = "executionTimes"; |
| 32 | + private static final String AVERAGE = "average"; |
| 33 | + private static final String DIFF = "diff"; |
| 34 | + |
| 35 | + static final String PRODUCT_SYNC = "productSync"; |
| 36 | + static final String INVENTORY_SYNC = "inventorySync"; |
| 37 | + static final String CATEGORY_SYNC = "categorySync"; |
| 38 | + static final String CREATES_ONLY = "createsOnly"; |
| 39 | + static final String UPDATES_ONLY = "updatesOnly"; |
| 40 | + static final String CREATES_AND_UPDATES = "mix"; |
| 41 | + static final int THRESHOLD = 120000; //120 seconds in milliseconds |
| 42 | + static final int NUMBER_OF_RESOURCE_UNDER_TEST = 10000; |
| 43 | + |
| 44 | + |
| 45 | + static void saveNewResult(@Nonnull final String version, |
| 46 | + @Nonnull final String sync, |
| 47 | + @Nonnull final String benchmark, |
| 48 | + final double newResult) throws IOException { |
| 49 | + |
| 50 | + final JsonNode rootNode = new ObjectMapper().readTree(getFileContent(BENCHMARK_RESULTS_FILE_PATH)); |
| 51 | + final JsonNode withNewResult = addNewResult(rootNode, version, sync, benchmark, newResult); |
| 52 | + writeToFile(withNewResult.toString(), BENCHMARK_RESULTS_FILE_PATH); |
| 53 | + } |
| 54 | + |
| 55 | + private static JsonNode addNewResult(@Nonnull final JsonNode originalRoot, |
| 56 | + @Nonnull final String version, |
| 57 | + @Nonnull final String sync, |
| 58 | + @Nonnull final String benchmark, |
| 59 | + final double newResult) { |
| 60 | + ObjectNode rootNode = (ObjectNode) originalRoot; |
| 61 | + ObjectNode versionNode = (ObjectNode) rootNode.get(version); |
| 62 | + |
| 63 | + // If version doesn't exist yet, create a new JSON object for the new version. |
| 64 | + if (versionNode == null) { |
| 65 | + rootNode = createVersionNode(rootNode, version); |
| 66 | + versionNode = (ObjectNode) rootNode.get(version); |
| 67 | + } |
| 68 | + |
| 69 | + final ObjectNode syncNode = (ObjectNode) versionNode.get(sync); |
| 70 | + final ObjectNode benchmarkNode = (ObjectNode) syncNode.get(benchmark); |
| 71 | + |
| 72 | + // Get current list of execution times for the specified benchmark of the specified sync module |
| 73 | + // of the specified version. |
| 74 | + final List<JsonNode> results = toList(benchmarkNode.get(EXECUTION_TIMES).elements()); |
| 75 | + |
| 76 | + // Add new result. |
| 77 | + results.add(JsonNodeFactory.instance.numberNode(newResult)); |
| 78 | + benchmarkNode.set(EXECUTION_TIMES, JsonNodeFactory.instance.arrayNode().addAll(results)); |
| 79 | + |
| 80 | + // Compute new average and add to JSON Object |
| 81 | + final double averageResult = calculateAvg(results); |
| 82 | + benchmarkNode.set(AVERAGE, JsonNodeFactory.instance.numberNode(averageResult)); |
| 83 | + |
| 84 | + // Compute new diff from the last version. |
| 85 | + final double diff = calculateDiff(rootNode, version, sync, benchmark, averageResult); |
| 86 | + benchmarkNode.set(DIFF, JsonNodeFactory.instance.numberNode(diff)); |
| 87 | + |
| 88 | + final JsonNode newSyncNode = syncNode.set(benchmark, benchmarkNode); |
| 89 | + final JsonNode newVersionNode = versionNode.set(sync, newSyncNode); |
| 90 | + final JsonNode newRoot = rootNode.set(version, newVersionNode); |
| 91 | + return newRoot; |
| 92 | + } |
| 93 | + |
| 94 | + @Nonnull |
| 95 | + private static <T> List<T> toList(@Nonnull final Iterator<T> iterator) { |
| 96 | + return toStream(iterator).collect(Collectors.toList()); |
| 97 | + } |
| 98 | + |
| 99 | + @Nonnull |
| 100 | + private static <T> Stream<T> toStream(@Nonnull final Iterator<T> iterator) { |
| 101 | + return stream(spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); |
| 102 | + } |
| 103 | + |
| 104 | + private static double calculateAvg(@Nonnull final List<JsonNode> results) { |
| 105 | + return results.stream().mapToDouble(JsonNode::asLong).average().orElse(0); |
| 106 | + } |
| 107 | + |
| 108 | + private static ObjectNode createVersionNode(@Nonnull final ObjectNode rootNode, @Nonnull final String version) { |
| 109 | + final ObjectNode newVersionNode = createSyncNode( |
| 110 | + createSyncNode(createSyncNode(JsonNodeFactory.instance.objectNode(), |
| 111 | + PRODUCT_SYNC), INVENTORY_SYNC), CATEGORY_SYNC); |
| 112 | + return (ObjectNode) rootNode.set(version, newVersionNode); |
| 113 | + } |
| 114 | + |
| 115 | + private static ObjectNode createSyncNode(@Nonnull final ObjectNode versionNode, |
| 116 | + @Nonnull final String sync) { |
| 117 | + final ObjectNode newSyncNode = createBenchmarkNode( |
| 118 | + createBenchmarkNode( |
| 119 | + createBenchmarkNode(JsonNodeFactory.instance.objectNode(), CREATES_ONLY), UPDATES_ONLY), |
| 120 | + CREATES_AND_UPDATES); |
| 121 | + return (ObjectNode) versionNode.set(sync, newSyncNode); |
| 122 | + } |
| 123 | + |
| 124 | + private static ObjectNode createBenchmarkNode(@Nonnull final ObjectNode syncNode, |
| 125 | + @Nonnull final String benchmark) { |
| 126 | + final ObjectNode newBenchmarkNode = JsonNodeFactory.instance.objectNode(); |
| 127 | + newBenchmarkNode.set(EXECUTION_TIMES, JsonNodeFactory.instance.arrayNode()); |
| 128 | + newBenchmarkNode.set(AVERAGE, JsonNodeFactory.instance.numberNode(0)); |
| 129 | + newBenchmarkNode.set(DIFF, JsonNodeFactory.instance.numberNode(0)); |
| 130 | + |
| 131 | + syncNode.set(benchmark, newBenchmarkNode); |
| 132 | + return syncNode; |
| 133 | + } |
| 134 | + |
| 135 | + static double calculateDiff(@Nonnull final String version, |
| 136 | + @Nonnull final String sync, |
| 137 | + @Nonnull final String benchmark, |
| 138 | + final double average) throws IOException { |
| 139 | + final JsonNode rootNode = new ObjectMapper().readTree(getFileContent(BENCHMARK_RESULTS_FILE_PATH)); |
| 140 | + return calculateDiff(rootNode, version, sync, benchmark, average); |
| 141 | + } |
| 142 | + |
| 143 | + private static double calculateDiff(@Nonnull final JsonNode originalRoot, |
| 144 | + @Nonnull final String version, |
| 145 | + @Nonnull final String sync, |
| 146 | + @Nonnull final String benchmark, |
| 147 | + final double average) { |
| 148 | + return getLatestVersionName(originalRoot, version) |
| 149 | + .map(latestVersionName -> originalRoot.get(latestVersionName) |
| 150 | + .get(sync) |
| 151 | + .get(benchmark) |
| 152 | + .get(AVERAGE)) |
| 153 | + .map(latestAverageNode -> average - latestAverageNode.asDouble()) |
| 154 | + // if there is no latest version - the current average is the diff. |
| 155 | + .orElse(average); |
| 156 | + } |
| 157 | + |
| 158 | + private static Optional<String> getLatestVersionName(@Nonnull final JsonNode originalRoot, |
| 159 | + @Nonnull final String currentVersionName) { |
| 160 | + return toStream(originalRoot.fieldNames()).reduce((firstVersion, secondVersion) -> |
| 161 | + !currentVersionName.equals(secondVersion) ? secondVersion : firstVersion); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + private static String getFileContent(@Nonnull final String path) throws IOException { |
| 166 | + final byte[] fileBytes = Files.readAllBytes(Paths.get(path)); |
| 167 | + return new String(fileBytes, UTF8_CHARSET); |
| 168 | + } |
| 169 | + |
| 170 | + private static void writeToFile(@Nonnull final String content, @Nonnull final String path) throws IOException { |
| 171 | + Files.write(Paths.get(path), content.getBytes(UTF8_CHARSET)); |
| 172 | + } |
| 173 | +} |
0 commit comments