Skip to content

Commit

Permalink
[test] RedisLuaFcfsService 부하 테스트 작성 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Aug 5, 2024
1 parent c379252 commit 2a5cc33
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ void loadTestParticipate() throws InterruptedException {

ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
CountDownLatch latch = new CountDownLatch(numberOfUsers);
String userIdPrefix = "user"; // 사용자 ID 접두사

// 테스트 시작 시간
long startTime = System.currentTimeMillis();
Expand All @@ -71,7 +70,7 @@ void loadTestParticipate() throws InterruptedException {
final int index = i;
executorService.execute(() -> {
try {
boolean result = redisLockFcfsService.participate(1L, userIdPrefix + index);
boolean result = redisLockFcfsService.participate(1L, "user" + index);
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package hyundai.softeer.orange.event.fcfs;
import hyundai.softeer.orange.event.fcfs.service.RedisLuaFcfsService;
import hyundai.softeer.orange.event.fcfs.util.FcfsUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
@SpringBootTest
class RedisLuaFcfsServiceLoadTest {

@Autowired
RedisLuaFcfsService redisLuaFcfsService;

@Autowired
StringRedisTemplate stringRedisTemplate;

@Autowired
RedisTemplate<String, Integer> numberRedisTemplate;

@Autowired
RedisTemplate<String, Boolean> booleanRedisTemplate;

Long eventSequence = 1L; // 테스트할 이벤트 시퀀스

@BeforeEach
void setUp() {
// 초기화
stringRedisTemplate.getConnectionFactory().getConnection().flushAll();
numberRedisTemplate.getConnectionFactory().getConnection().flushAll();
booleanRedisTemplate.getConnectionFactory().getConnection().flushAll();

// 테스트할 이벤트 정보 저장
numberRedisTemplate.opsForValue().set(FcfsUtil.keyFormatting(eventSequence.toString()), 100);
stringRedisTemplate.opsForValue().set(FcfsUtil.startTimeFormatting(eventSequence.toString()), "2021-08-01T00:00:00");
}

@Test
void participateTest() throws InterruptedException {
int numberOfThreads = 200; // 스레드 수
int numberOfUsers = 1000; // 동시 참여 사용자 수

ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);

long startTime = System.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(numberOfUsers);
for (int i = 0; i < numberOfUsers; i++) {
final int index = i;
executorService.execute(() -> {
try {
boolean result = redisLuaFcfsService.participate(1L, "user" + index);
} catch (Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
});
}

latch.await();

long endTime = System.currentTimeMillis();
log.info("Total time: {} ms", endTime - startTime);

executorService.shutdown();
Long count = stringRedisTemplate.opsForZSet().zCard(FcfsUtil.winnerFormatting(eventSequence.toString()));
assertThat(count).isEqualTo(100);
}
}

0 comments on commit 2a5cc33

Please sign in to comment.