|
| 1 | +import java.io.InputStream; |
| 2 | +import java.net.HttpURLConnection; |
| 3 | +import java.net.URL; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Paths; |
| 6 | +import java.util.concurrent.CountDownLatch; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +public class Profile { |
| 12 | + private static final int NUM_THREADS = 8; |
| 13 | + |
| 14 | + private static byte[] JSON_BYTES; |
| 15 | + private static int JSON_LEN; |
| 16 | + |
| 17 | + static { |
| 18 | + try { |
| 19 | + var JSON = Files.readAllLines(Paths.get("./request.json")).stream() |
| 20 | + .collect(Collectors.joining("")); |
| 21 | + JSON_BYTES = JSON.getBytes("utf-8"); |
| 22 | + JSON_LEN = JSON_BYTES.length; |
| 23 | + } catch (Exception e) { |
| 24 | + e.printStackTrace(); |
| 25 | + System.exit(1); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private static int numRequests; |
| 30 | + private static String urlToCall; |
| 31 | + |
| 32 | + private static ExecutorService threadPool = Executors.newFixedThreadPool(NUM_THREADS); |
| 33 | + |
| 34 | + public static void main(String[] args) throws Exception { |
| 35 | + numRequests = Integer.parseInt(args[0]); |
| 36 | + urlToCall = args[1]; |
| 37 | + |
| 38 | + var cdl = new CountDownLatch(numRequests); |
| 39 | + |
| 40 | + var start = System.currentTimeMillis(); |
| 41 | + |
| 42 | + for (int i = 0; i < numRequests; i++) { |
| 43 | + threadPool.execute(() -> { |
| 44 | + performHttpRequest(); |
| 45 | + cdl.countDown(); |
| 46 | + }); |
| 47 | + } |
| 48 | + cdl.await(); |
| 49 | + |
| 50 | + System.out.println((System.currentTimeMillis() - start) / 1000.0); |
| 51 | + |
| 52 | + threadPool.shutdown(); |
| 53 | + } |
| 54 | + |
| 55 | + private static void performHttpRequest() { |
| 56 | + try { |
| 57 | + var url = new URL(urlToCall); |
| 58 | + var connection = (HttpURLConnection) url.openConnection(); |
| 59 | + connection.setRequestMethod("POST"); |
| 60 | + connection.setRequestProperty("Content-Type", "application/json"); // Set Content-Type header |
| 61 | + connection.setDoOutput(true); |
| 62 | + |
| 63 | + try (var os = connection.getOutputStream()) { |
| 64 | + os.write(JSON_BYTES, 0, JSON_LEN); |
| 65 | + } |
| 66 | + |
| 67 | + var ret = connection.getResponseCode(); |
| 68 | + if (ret != 200) { |
| 69 | + try (InputStream errorStream = connection.getErrorStream()) { |
| 70 | + byte[] buffer = new byte[1024]; |
| 71 | + int bytesRead; |
| 72 | + while ((bytesRead = errorStream.read(buffer)) != -1) { |
| 73 | + System.err.write(buffer, 0, bytesRead); |
| 74 | + } |
| 75 | + } |
| 76 | + try (InputStream errorStream = connection.getInputStream()) { |
| 77 | + byte[] buffer = new byte[1024]; |
| 78 | + int bytesRead; |
| 79 | + while ((bytesRead = errorStream.read(buffer)) != -1) { |
| 80 | + System.err.write(buffer, 0, bytesRead); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + connection.disconnect(); |
| 85 | + } catch (Exception e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments