|
| 1 | +package com.amazon.esdk.benchmark.model; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public final class TestResult { |
| 8 | + |
| 9 | + @JsonProperty("language") |
| 10 | + public final String language = "java"; |
| 11 | + |
| 12 | + @JsonProperty("test_name") |
| 13 | + public String testName; |
| 14 | + |
| 15 | + @JsonProperty("data_size") |
| 16 | + public int dataSize; |
| 17 | + |
| 18 | + @JsonProperty("concurrency") |
| 19 | + public int concurrency = 1; |
| 20 | + |
| 21 | + @JsonProperty("put_latency_ms") |
| 22 | + public double putLatencyMs; |
| 23 | + |
| 24 | + @JsonProperty("get_latency_ms") |
| 25 | + public double getLatencyMs; |
| 26 | + |
| 27 | + @JsonProperty("end_to_end_latency_ms") |
| 28 | + public double endToEndLatencyMs; |
| 29 | + |
| 30 | + @JsonProperty("ops_per_second") |
| 31 | + public double opsPerSecond; |
| 32 | + |
| 33 | + @JsonProperty("bytes_per_second") |
| 34 | + public double bytesPerSecond; |
| 35 | + |
| 36 | + @JsonProperty("peak_memory_mb") |
| 37 | + public double peakMemoryMb; |
| 38 | + |
| 39 | + @JsonProperty("memory_efficiency_ratio") |
| 40 | + public double memoryEfficiencyRatio; |
| 41 | + |
| 42 | + @JsonProperty("p50_latency") |
| 43 | + public double p50Latency; |
| 44 | + |
| 45 | + @JsonProperty("p95_latency") |
| 46 | + public double p95Latency; |
| 47 | + |
| 48 | + @JsonProperty("p99_latency") |
| 49 | + public double p99Latency; |
| 50 | + |
| 51 | + @JsonProperty("timestamp") |
| 52 | + public String timestamp = ""; |
| 53 | + |
| 54 | + @JsonProperty("java_version") |
| 55 | + public String javaVersion = ""; |
| 56 | + |
| 57 | + @JsonProperty("cpu_count") |
| 58 | + public int cpuCount; |
| 59 | + |
| 60 | + @JsonProperty("total_memory_gb") |
| 61 | + public double totalMemoryGb; |
| 62 | + |
| 63 | + @JsonProperty("iterations") |
| 64 | + public int iterations; |
| 65 | + |
| 66 | + public static TestResult createThroughputResult( |
| 67 | + final List<Double> putLatencies, |
| 68 | + final List<Double> getLatencies, |
| 69 | + final List<Double> totalLatencies, |
| 70 | + final int dataSize, |
| 71 | + final int cpuCount, |
| 72 | + final double totalMemoryMB |
| 73 | + ) { |
| 74 | + final double avgTotalLatency = totalLatencies |
| 75 | + .stream() |
| 76 | + .mapToDouble(Double::doubleValue) |
| 77 | + .average() |
| 78 | + .orElse(0.0); |
| 79 | + final double opsPerSecond = avgTotalLatency > 0 |
| 80 | + ? 1000.0 / avgTotalLatency |
| 81 | + : 0.0; |
| 82 | + |
| 83 | + Collections.sort(totalLatencies); |
| 84 | + |
| 85 | + final var result = new TestResult(); |
| 86 | + result.testName = "throughput"; |
| 87 | + result.dataSize = dataSize; |
| 88 | + result.concurrency = 1; |
| 89 | + result.opsPerSecond = opsPerSecond; |
| 90 | + result.bytesPerSecond = opsPerSecond * dataSize; |
| 91 | + result.endToEndLatencyMs = avgTotalLatency; |
| 92 | + result.p50Latency = calculatePercentile(totalLatencies, 50); |
| 93 | + result.p95Latency = calculatePercentile(totalLatencies, 95); |
| 94 | + result.p99Latency = calculatePercentile(totalLatencies, 99); |
| 95 | + result.putLatencyMs = putLatencies |
| 96 | + .stream() |
| 97 | + .mapToDouble(Double::doubleValue) |
| 98 | + .average() |
| 99 | + .orElse(0.0); |
| 100 | + result.getLatencyMs = getLatencies |
| 101 | + .stream() |
| 102 | + .mapToDouble(Double::doubleValue) |
| 103 | + .average() |
| 104 | + .orElse(0.0); |
| 105 | + result.iterations = putLatencies.size(); |
| 106 | + result.timestamp = java.time.LocalDateTime |
| 107 | + .now() |
| 108 | + .format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| 109 | + result.javaVersion = System.getProperty("java.version"); |
| 110 | + result.cpuCount = cpuCount; |
| 111 | + result.totalMemoryGb = totalMemoryMB / 1024.0; |
| 112 | + |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + public static TestResult createMemoryResult( |
| 117 | + final double peakMemoryMb, |
| 118 | + final double avgMemoryMb, |
| 119 | + final int dataSize, |
| 120 | + final int cpuCount, |
| 121 | + final double totalMemoryMB |
| 122 | + ) { |
| 123 | + final double memoryEfficiency = peakMemoryMb > 0 |
| 124 | + ? dataSize / (peakMemoryMb * 1024 * 1024) |
| 125 | + : 0.0; |
| 126 | + |
| 127 | + final var result = new TestResult(); |
| 128 | + result.testName = "memory"; |
| 129 | + result.dataSize = dataSize; |
| 130 | + result.concurrency = 1; |
| 131 | + result.peakMemoryMb = peakMemoryMb; |
| 132 | + result.memoryEfficiencyRatio = memoryEfficiency; |
| 133 | + result.timestamp = java.time.LocalDateTime |
| 134 | + .now() |
| 135 | + .format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| 136 | + result.javaVersion = System.getProperty("java.version"); |
| 137 | + result.cpuCount = cpuCount; |
| 138 | + result.totalMemoryGb = totalMemoryMB / 1024.0; |
| 139 | + |
| 140 | + return result; |
| 141 | + } |
| 142 | + |
| 143 | + public static TestResult createConcurrentResult( |
| 144 | + final List<Double> allTimes, |
| 145 | + final int totalOps, |
| 146 | + final int dataSize, |
| 147 | + final int concurrency, |
| 148 | + final int cpuCount, |
| 149 | + final double totalMemoryMB |
| 150 | + ) { |
| 151 | + final double avgLatency = allTimes |
| 152 | + .stream() |
| 153 | + .mapToDouble(Double::doubleValue) |
| 154 | + .average() |
| 155 | + .orElse(0.0); |
| 156 | + final double totalTimeSeconds = allTimes |
| 157 | + .stream() |
| 158 | + .mapToDouble(Double::doubleValue) |
| 159 | + .sum() / 1000.0; |
| 160 | + final double opsPerSecond = totalTimeSeconds > 0 ? totalOps / totalTimeSeconds : 0.0; |
| 161 | + |
| 162 | + final var result = new TestResult(); |
| 163 | + result.testName = "concurrent"; |
| 164 | + result.dataSize = dataSize; |
| 165 | + result.concurrency = concurrency; |
| 166 | + result.opsPerSecond = opsPerSecond; |
| 167 | + result.bytesPerSecond = opsPerSecond * dataSize; |
| 168 | + result.endToEndLatencyMs = avgLatency; |
| 169 | + result.iterations = totalOps; |
| 170 | + result.timestamp = java.time.LocalDateTime |
| 171 | + .now() |
| 172 | + .format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| 173 | + result.javaVersion = System.getProperty("java.version"); |
| 174 | + result.cpuCount = cpuCount; |
| 175 | + result.totalMemoryGb = totalMemoryMB / 1024.0; |
| 176 | + |
| 177 | + return result; |
| 178 | + } |
| 179 | + |
| 180 | + private static double calculatePercentile( |
| 181 | + final List<Double> values, |
| 182 | + final int percentile |
| 183 | + ) { |
| 184 | + if (values.isEmpty()) return 0.0; |
| 185 | + |
| 186 | + final int index = (int) Math.ceil((percentile / 100.0) * values.size()) - 1; |
| 187 | + final int clampedIndex = Math.max(0, Math.min(index, values.size() - 1)); |
| 188 | + return values.get(clampedIndex); |
| 189 | + } |
| 190 | +} |
0 commit comments