-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor] 동시성 테스트 스레드 카운트 톰켓 스레드 수치 동결 #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 4 commits
57d3e85
48e8f7b
635c976
edd84bc
9be059f
100b566
2eaae6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,61 @@ | ||
| package com.daedan.festabook.global.lock; | ||
|
|
||
| import com.daedan.festabook.support.ConcurrencyTestResult; | ||
| import io.restassured.response.Response; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class ConcurrencyTestHelper { | ||
|
|
||
| private ConcurrencyTestHelper() { | ||
| } | ||
| @Value("${server.tomcat.threads.max}") | ||
| private int tomcatThreadCount; | ||
|
|
||
| public static void test(int requestCount, Runnable... requests) { | ||
| @SafeVarargs | ||
| public final ConcurrencyTestResult execute(Callable<Response>... requests) { | ||
| validateRequests(requests); | ||
| ConcurrencyTestResult result = new ConcurrencyTestResult(); | ||
|
|
||
| try (ExecutorService threadPool = Executors.newFixedThreadPool(requestCount)) { | ||
| try (ExecutorService executorService = Executors.newFixedThreadPool(tomcatThreadCount)) { | ||
| CountDownLatch startLatch = new CountDownLatch(1); | ||
| CountDownLatch endLatch = new CountDownLatch(requestCount); | ||
| CountDownLatch endLatch = new CountDownLatch(tomcatThreadCount); | ||
|
|
||
| for (int i = 0; i < requestCount; i++) { | ||
| for (int i = 0; i < tomcatThreadCount; i++) { | ||
| int currentCount = i % requests.length; | ||
| threadPool.submit(() -> { | ||
| executorService.submit(() -> { | ||
| try { | ||
| startLatch.await(); | ||
| requests[currentCount].run(); | ||
| } catch (InterruptedException ignore) { | ||
| Response response = requests[currentCount].call(); | ||
|
|
||
| HttpStatus httpStatus = HttpStatus.valueOf(response.getStatusCode()); | ||
| result.recordStatusCode(httpStatus); | ||
| } catch (Exception e) { | ||
| log.error("동시성 테스트 실행 중 예외 발생", e); | ||
| result.recordStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); | ||
| } finally { | ||
| endLatch.countDown(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| startLatch.countDown(); | ||
|
|
||
| try { | ||
| endLatch.await(); | ||
| } catch (InterruptedException ignore) { | ||
| } | ||
| endLatch.await(); | ||
| } catch (InterruptedException ignore) { | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static void validateRequests(Runnable[] requests) { | ||
| if (requests.length == 0) { | ||
| throw new IllegalArgumentException("실행할 api 인자는 최소 1개 이상이어야 합니다."); | ||
| @SafeVarargs | ||
| private void validateRequests(Callable<Response>... requests) { | ||
| if (requests == null || requests.length == 0) { | ||
| throw new IllegalArgumentException("실행할 API 인자는 최소 1개 이상이어야 합니다."); | ||
| } | ||
| } | ||
| } |
taek2222 marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.daedan.festabook.support; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public class ConcurrencyTestResult { | ||
|
|
||
| private final Map<HttpStatus, Integer> statusCodeCounts = new ConcurrentHashMap<>(); | ||
| private final AtomicLong requestCount = new AtomicLong(); | ||
|
|
||
| public void recordStatusCode(HttpStatus status) { | ||
| statusCodeCounts.merge(status, 1, Integer::sum); | ||
| requestCount.incrementAndGet(); | ||
| } | ||
taek2222 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public int getStatusCodeCount(HttpStatus status) { | ||
| return statusCodeCounts.getOrDefault(status, 0); | ||
| } | ||
|
|
||
| public int getSuccessCount() { | ||
| return statusCodeCounts.entrySet().stream() | ||
| .filter(entry -> entry.getKey().is2xxSuccessful()) | ||
| .mapToInt(Map.Entry::getValue) | ||
| .sum(); | ||
| } | ||
|
||
|
|
||
| public long getRequestCount() { | ||
| return requestCount.get(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,8 @@ secret: | |
| fcm: | ||
| topic: | ||
| festival-prefix: example-festival | ||
|
|
||
| server: | ||
| tomcat: | ||
| threads: | ||
| max: 15 | ||
Uh oh!
There was an error while loading. Please reload this page.