-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] RedisLuaFcfsService 부하 테스트 작성 (#29)
- Loading branch information
Showing
2 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/java/hyundai/softeer/orange/event/fcfs/RedisLuaFcfsServiceLoadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |