
@Service @RequiredArgsConstructor public class SearchService { private final SearchRepository searchRepository; private final RedisTemplate<String, String> redisTemplate; ... public void searchWithRedis(String keyword) { // Redis 명령어에서 'ZINCRBY search_keyword_ranking 1 [keyword]'와 동일 // ZINCRBY 명령어를 사용하여 해당 keyword의 score를 1 증가시킴 // (만약 해당 키워드가 존재하지 않으면 새로 추가하고 score를 1로 설정함) redisTemplate.opsForZSet() .incrementScore("search_keyword_ranking", keyword, 1.0); } public List<String> getTop10KeywordsWithRedis() { // Redis 명령어에서 'ZRANGE [key] [start index] [end index] REV'와 동일 // REV 옵션을 사용하여 Score가 높은 순(내림차순)으로 0~9번 인덱스 데이터를 조회 Set<String> topKeywords = redisTemplate.opsForZSet() .reverseRange("search_keyword_ranking", 0, 9); // Set을 List로 변환하여 반환 (null일 경우 빈 리스트 반환) return new ArrayList<>(topKeywords); } }
@RestController @RequiredArgsConstructor @RequestMapping("/search") public class SearchController { private final SearchService searchService; ... @GetMapping("/redis") public void searchWithRedis(@RequestParam String keyword) { searchService.searchWithRedis(keyword); } @GetMapping("/top10/redis") public List<String> getTop10KeywordsWithRedis() { return searchService.getTop10KeywordsWithRedis(); } }

script_3-1.js)와 코드가 대부분 똑같고, 새로 만든 API에 맞게 주소만 변경해주었다. import http from 'k6/http'; import {check} from 'k6'; import {randomItem} from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; export const options = { // 가상 유저(VUs) 100명으로 설정 vus: 100, // 테스트를 10초 동안 진행 duration: '10s', }; // 검색어 리스트 정의 const keywords = [ 'spring', 'java', 'redis', 'mysql', 'jpa', 'k6', 'performance', 'test', 'load', 'stress', 'docker', 'kubernetes', 'aws', 'cloud', 'microservices', 'python', 'javascript', 'react', 'vue', 'angular' ]; export default function () { // 1. 검색 API 호출 // 랜덤한 키워드 선택 const keyword = randomItem(keywords); // 검색 요청 const searchRes = http.get(`http://localhost:8080/search/redis?keyword=${keyword}`); check(searchRes, { 'search status is 200': (r) => r.status === 200, }); // 2. 인기 검색어 조회 API 호출 const top10Res = http.get('http://localhost:8080/search/top10/redis'); check(top10Res, { 'top10 status is 200': (r) => r.status === 200, }); }
$ k6 run scripts/script_3-2.js

