|
| 1 | +package org.opensearch.security; |
| 2 | + |
| 3 | +import static org.opensearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; |
| 4 | +import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; |
| 5 | +import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; |
| 6 | + |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.lang.management.GarbageCollectorMXBean; |
| 10 | +import java.lang.management.ManagementFactory; |
| 11 | +import java.lang.management.MemoryPoolMXBean; |
| 12 | +import java.lang.management.MemoryUsage; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.CompletableFuture; |
| 17 | +import java.util.concurrent.ForkJoinPool; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.function.Supplier; |
| 20 | +import java.util.stream.Collectors; |
| 21 | +import java.util.stream.IntStream; |
| 22 | +import java.util.zip.GZIPOutputStream; |
| 23 | + |
| 24 | +import org.apache.hc.client5.http.classic.methods.HttpPost; |
| 25 | +import org.apache.hc.core5.http.ContentType; |
| 26 | +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; |
| 27 | +import org.apache.hc.core5.http.message.BasicHeader; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.ClassRule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.opensearch.action.index.IndexRequest; |
| 33 | +import org.opensearch.client.Client; |
| 34 | +import org.opensearch.test.framework.TestSecurityConfig; |
| 35 | +import org.opensearch.test.framework.TestSecurityConfig.User; |
| 36 | +import org.opensearch.test.framework.cluster.ClusterManager; |
| 37 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 38 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 39 | + |
| 40 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 41 | + |
| 42 | +@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) |
| 43 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) |
| 44 | +public class ResourceFocusedTests { |
| 45 | + private static final User ADMIN_USER = new User("admin").roles(ALL_ACCESS); |
| 46 | + private static final User LIMITED_USER = new User("limited_user").roles( |
| 47 | + new TestSecurityConfig.Role("limited-role").clusterPermissions( |
| 48 | + "indices:data/read/mget", |
| 49 | + "indices:data/read/msearch", |
| 50 | + "indices:data/read/scroll", |
| 51 | + "cluster:monitor/state", |
| 52 | + "cluster:monitor/health" |
| 53 | + ) |
| 54 | + .indexPermissions( |
| 55 | + "indices:data/read/search", |
| 56 | + "indices:data/read/mget*", |
| 57 | + "indices:monitor/settings/get", |
| 58 | + "indices:monitor/stats" |
| 59 | + ) |
| 60 | + .on("*") |
| 61 | + ); |
| 62 | + |
| 63 | + @ClassRule |
| 64 | + public static LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.THREE_CLUSTER_MANAGERS) |
| 65 | + .authc(AUTHC_HTTPBASIC_INTERNAL) |
| 66 | + .users(ADMIN_USER, LIMITED_USER) |
| 67 | + .anonymousAuth(false) |
| 68 | + .doNotFailOnForbidden(true) |
| 69 | + .build(); |
| 70 | + |
| 71 | + @BeforeClass |
| 72 | + public static void createTestData() { |
| 73 | + try (Client client = cluster.getInternalNodeClient()) { |
| 74 | + client.index(new IndexRequest().setRefreshPolicy(IMMEDIATE).index("document").source(Map.of("foo", "bar", "abc", "xyz"))) |
| 75 | + .actionGet(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testUnauthenticatedFewBig() { |
| 81 | + // Tweaks: |
| 82 | + final RequestBodySize size = RequestBodySize.XLarge; |
| 83 | + final String requestPath = "/*/_search"; |
| 84 | + final int parrallelism = 5; |
| 85 | + final int totalNumberOfRequests = 100; |
| 86 | + final boolean statsPrinter = false; |
| 87 | + |
| 88 | + runResourceTest(size, requestPath, parrallelism, totalNumberOfRequests, statsPrinter); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testUnauthenticatedManyMedium() { |
| 93 | + // Tweaks: |
| 94 | + final RequestBodySize size = RequestBodySize.Medium; |
| 95 | + final String requestPath = "/*/_search"; |
| 96 | + final int parrallelism = 20; |
| 97 | + final int totalNumberOfRequests = 10_000; |
| 98 | + final boolean statsPrinter = false; |
| 99 | + |
| 100 | + runResourceTest(size, requestPath, parrallelism, totalNumberOfRequests, statsPrinter); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testUnauthenticatedTonsSmall() { |
| 105 | + // Tweaks: |
| 106 | + final RequestBodySize size = RequestBodySize.Small; |
| 107 | + final String requestPath = "/*/_search"; |
| 108 | + final int parrallelism = 100; |
| 109 | + final int totalNumberOfRequests = 1_000_000; |
| 110 | + final boolean statsPrinter = false; |
| 111 | + |
| 112 | + runResourceTest(size, requestPath, parrallelism, totalNumberOfRequests, statsPrinter); |
| 113 | + } |
| 114 | + |
| 115 | + private Long runResourceTest( |
| 116 | + final RequestBodySize size, |
| 117 | + final String requestPath, |
| 118 | + final int parrallelism, |
| 119 | + final int totalNumberOfRequests, |
| 120 | + final boolean statsPrinter |
| 121 | + ) { |
| 122 | + final byte[] compressedRequestBody = createCompressedRequestBody(size); |
| 123 | + try (final TestRestClient client = cluster.getRestClient(new BasicHeader("Content-Encoding", "gzip"))) { |
| 124 | + |
| 125 | + if (statsPrinter) { |
| 126 | + printStats(); |
| 127 | + } |
| 128 | + final HttpPost post = new HttpPost(client.getHttpServerUri() + requestPath); |
| 129 | + post.setEntity(new ByteArrayEntity(compressedRequestBody, ContentType.APPLICATION_JSON)); |
| 130 | + |
| 131 | + final ForkJoinPool forkJoinPool = new ForkJoinPool(parrallelism); |
| 132 | + |
| 133 | + final List<CompletableFuture<Void>> waitingOn = IntStream.rangeClosed(1, totalNumberOfRequests) |
| 134 | + .boxed() |
| 135 | + .map(i -> CompletableFuture.runAsync(() -> client.executeRequest(post), forkJoinPool)) |
| 136 | + .collect(Collectors.toList()); |
| 137 | + Supplier<Long> getCount = () -> waitingOn.stream().filter(cf -> cf.isDone() && !cf.isCompletedExceptionally()).count(); |
| 138 | + |
| 139 | + CompletableFuture<Void> statPrinter = statsPrinter ? CompletableFuture.runAsync(() -> { |
| 140 | + while (true) { |
| 141 | + printStats(); |
| 142 | + System.out.println(" & Succesful completions: " + getCount.get()); |
| 143 | + try { |
| 144 | + Thread.sleep(500); |
| 145 | + } catch (Exception e) { |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + }, forkJoinPool) : CompletableFuture.completedFuture(null); |
| 150 | + |
| 151 | + final CompletableFuture<Void> allOfThem = CompletableFuture.allOf(waitingOn.toArray(new CompletableFuture[0])); |
| 152 | + |
| 153 | + try { |
| 154 | + allOfThem.get(30, TimeUnit.SECONDS); |
| 155 | + statPrinter.cancel(true); |
| 156 | + } catch (final Exception e) { |
| 157 | + // Ignored |
| 158 | + } |
| 159 | + |
| 160 | + if (statsPrinter) { |
| 161 | + printStats(); |
| 162 | + System.out.println(" & Succesful completions: " + getCount.get()); |
| 163 | + } |
| 164 | + return getCount.get(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + static enum RequestBodySize { |
| 169 | + Small(1), |
| 170 | + Medium(1_000), |
| 171 | + XLarge(1_000_000); |
| 172 | + |
| 173 | + public final int elementCount; |
| 174 | + |
| 175 | + private RequestBodySize(final int elementCount) { |
| 176 | + this.elementCount = elementCount; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private byte[] createCompressedRequestBody(final RequestBodySize size) { |
| 181 | + final int repeatCount = size.elementCount; |
| 182 | + final String prefix = "{ \"items\": ["; |
| 183 | + final String repeatedElement = IntStream.range(0, 20) |
| 184 | + .mapToObj(n -> ('a' + n) + "") |
| 185 | + .map(n -> '"' + n + '"' + ": 123") |
| 186 | + .collect(Collectors.joining(",", "{", "}")); |
| 187 | + final String postfix = "]}"; |
| 188 | + long uncompressedBytesSize = 0; |
| 189 | + |
| 190 | + try ( |
| 191 | + final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 192 | + final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream) |
| 193 | + ) { |
| 194 | + |
| 195 | + final byte[] prefixBytes = prefix.getBytes(StandardCharsets.UTF_8); |
| 196 | + final byte[] repeatedElementBytes = repeatedElement.getBytes(StandardCharsets.UTF_8); |
| 197 | + final byte[] postfixBytes = postfix.getBytes(StandardCharsets.UTF_8); |
| 198 | + |
| 199 | + gzipOutputStream.write(prefixBytes); |
| 200 | + uncompressedBytesSize = uncompressedBytesSize + prefixBytes.length; |
| 201 | + for (int i = 0; i < repeatCount; i++) { |
| 202 | + gzipOutputStream.write(repeatedElementBytes); |
| 203 | + uncompressedBytesSize = uncompressedBytesSize + repeatedElementBytes.length; |
| 204 | + } |
| 205 | + gzipOutputStream.write(postfixBytes); |
| 206 | + uncompressedBytesSize = uncompressedBytesSize + postfixBytes.length; |
| 207 | + gzipOutputStream.finish(); |
| 208 | + |
| 209 | + final byte[] compressedRequestBody = byteArrayOutputStream.toByteArray(); |
| 210 | + System.out.println( |
| 211 | + "^^^" |
| 212 | + + String.format( |
| 213 | + "Original size was %,d bytes, compressed to %,d bytes, ratio %,.2f", |
| 214 | + uncompressedBytesSize, |
| 215 | + compressedRequestBody.length, |
| 216 | + ((double) uncompressedBytesSize / compressedRequestBody.length) |
| 217 | + ) |
| 218 | + ); |
| 219 | + return compressedRequestBody; |
| 220 | + } catch (final IOException ioe) { |
| 221 | + throw new RuntimeException(ioe); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private void printStats() { |
| 226 | + System.out.println("** Stats "); |
| 227 | + printMemory(); |
| 228 | + printMemoryPools(); |
| 229 | + printGCPools(); |
| 230 | + } |
| 231 | + |
| 232 | + private void printMemory() { |
| 233 | + final Runtime runtime = Runtime.getRuntime(); |
| 234 | + |
| 235 | + final long totalMemory = runtime.totalMemory(); // Total allocated memory |
| 236 | + final long freeMemory = runtime.freeMemory(); // Amount of free memory |
| 237 | + final long usedMemory = totalMemory - freeMemory; // Amount of used memory |
| 238 | + |
| 239 | + System.out.println(" Memory Total: " + totalMemory + " Free:" + freeMemory + " Used:" + usedMemory); |
| 240 | + } |
| 241 | + |
| 242 | + private void printMemoryPools() { |
| 243 | + List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans(); |
| 244 | + for (MemoryPoolMXBean memoryPool : memoryPools) { |
| 245 | + MemoryUsage usage = memoryPool.getUsage(); |
| 246 | + System.out.println(" " + memoryPool.getName() + " USED: " + usage.getUsed() + " MAX: " + usage.getMax()); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + private void printGCPools() { |
| 251 | + List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans(); |
| 252 | + for (GarbageCollectorMXBean garbageCollector : garbageCollectors) { |
| 253 | + System.out.println(" " + garbageCollector.getName() + " COLLECTION TIME: " + garbageCollector.getCollectionTime()); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | +} |
0 commit comments