[ํฐ๋] ํฐ๋ ์๋ฒ API ์ฑ๋ฅ๊ฐ์ ๊ธฐ(2)
์๋
ํ์ธ์ ํฐํ์ ๋ฐฑ์๋ ๊ฐ๋ฐ์ ๊น์ ์ ์
๋๋ค :)
์ ๋ฒ ๊ธ์ ์ด์ด์ ์ค๋์ ํ
์คํธ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๊ณ , ํฐ๋ ์ฑ์์ ๋ฐ์ํ๋ N+1 ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ฌ ์ฑ๋ฅ์ ์ธก์ ํ๋ ค ํฉ๋๋ค.
๐ฉ๐ป ํ ์คํธ ์คํฌ๋ฆฝํธ ์์ฑ
1. ngrinder์ ๋จผ์ ์คํํฉ๋๋ค.
java -Djava.io.tmpdir=/Users/kimyujeong/.ngrinder/lib -jar ngrinder-controller-3.5.8.war —port=8300
2. ๋ถํ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํฉ๋๋ค.
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.ngrinder.http.HTTPRequest
import org.ngrinder.http.HTTPRequestControl
import org.ngrinder.http.HTTPResponse
import org.ngrinder.http.cookie.Cookie
import org.ngrinder.http.cookie.CookieManager
import groovy.json.JsonSlurper
import java.util.Random
import groovy.json.JsonOutput
/**
* A simple example using the HTTP plugin that shows the retrieval of a single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test1 // accessToken ์ป๊ธฐ
public static GTest test2 //
public static HTTPRequest request
public static Map<String, String> headers = [:]
public static Map<String, Object> params = [:]
public static List<Cookie> cookies = []
@BeforeProcess
public static void beforeProcess() {
HTTPRequestControl.setConnectionTimeout(300000)
test1 = new GTest(1, "POST /login/extraInfo/{id} with accountId")
test2 = new GTest(2, "GET /participate/challenge/list with token")
request = new HTTPRequest()
grinder.logger.info("before process.")
}
@BeforeThread
public void beforeThread() {
test1.record(this, "test1")
test2.record(this, "test2")
grinder.statistics.delayReports = true
grinder.logger.info("before thread.")
}
@Before
public void before() {
request.setHeaders(headers)
CookieManager.addCookies(cookies)
grinder.logger.info("before. init headers and cookies")
}
private String accessToken
@Test
public void test1() {
List<Integer> idList = [...]
Random random = new Random()
int randomIndex = random.nextInt(idList.size())
int id = idList[randomIndex]
def slurper = new JsonSlurper()
def toJSON = { slurper.parseText(it) }
HTTPResponse response = request.POST("http://{ip์ฃผ์}/login/extraInfo/" + id)
def body = response.getBody(toJSON);
accessToken = body.result.accessToken
if (response.statusCode == 301 || response.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", response.statusCode)
} else {
assertThat(response.statusCode, is(200))
}
}
@Test
public void test2() {
headers["X-ACCESS-TOKEN"] = accessToken
request.setHeaders(headers)
HTTPResponse response = request.GET("http://{ip์ฃผ์}/participate/challenge/list")
if (response.statusCode == 301 || response.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", response.statusCode)
} else {
assertThat(response.statusCode, is(200))
}
}
}
groovy์ธ์ด๋ ์๋ฐ ์ธ์ด์ ๋น์ทํด์ ๊ตฌ๊ธ๋งํ๋ฉด์ ์์ฑํ๋ฉด ์ฝ๊ฒ ์์ฑํ ์ ์์ต๋๋ค!
ํ์๋ ํํ๋ฉด์์ (1)'์ฌ์ฉ์๊ฐ ์ฐธ์ฌํ๊ณ ์๋ ์ฑ๋ฆฐ์ง ๋ฆฌ์คํธ ์กฐํ' API์ ๋ํด์ ํ ์คํธ๋ฅผ ์งํํ์์ต๋๋ค.
QA๋ฅผ ์งํํ๋ฉด์ ํํ๋ฉด์ (1) api์์ ์ง์ฐ์ด ๋ฐ์ํ ๊ฒ์ ํ์ธํ์์ต๋๋ค. ์ฌ์ฉ์๊ฐ ์ฑ์ ์ฌ์ฉํ๋ ๋ฐ๋ ๋ฌธ์ ๊ฐ ์์ง๋ง, ์ฌ์ฉ์ฑ์ ํฅ์์ํค๊ธฐ ์ํด ์ฟผ๋ฆฌ๋ฅผ ๊ฐ์ ํด๋ณด๊ณ , ์ด์ ๊ณผ ๋น๊ตํด๋ณด๋ ค ํฉ๋๋ค.
๋จผ์ , ํ ์คํธ ์คํฌ๋ฆฝํธ์ ๋ํด์ ๊ฐ๋คํ ์ค๋ช ํด๋ณด๊ฒ ์ต๋๋ค.
์ฌ์ฉ์๋ฅผ ๊ตฌ๋ถํ๊ธฐ ์ํด, '/login/extraInfo/{id} ' ์๋ํฌ์ธํธ๋ก JWT ํ ํฐ์ ๋ฐ์์์ accessToken ์ ์ญ๋ณ์์ ์ ์ฅํ์ต๋๋ค.
์ด ๊ฐ์ ํตํด ์์ผ๋ก '/participate/challenge/list'์์ ์ฌ์ฉ์๊ฐ ์ฐธ์ฌํ ๋ฆฌ์คํธ๋ฅผ ์กฐํํ ๊ฒ์ ๋๋ค..
test1์ idList๋ ์ฌ์ฉ์์ ๊ณ ์ id๊ฐ์ ๋ฆฌ์คํธ๋ก ์ ์ฅํ์ต๋๋ค. ํฐ๋์์ ์ฌ์ฉํ๋ '/login/extraInfo/{id}' URL์ ์นด์นด์ค ๋ก๊ทธ์ธ์ ๋ง์น ์ฌ์ฉ์๊ฐ ์ถ๊ฐ ์ ๋ณด๋ฅผ ์ ๋ ฅํ๊ธฐ ์ํด ์ฌ์ฉํ๋ URL์ ๋๋ค. ๋ฐ๋ผ์ ์ฌ์ฉ์์ ๊ณ ์ id๊ฐ์ด ํ์ํ๋ฉฐ, ์ด๋ฅผ idList์ ์ผ์ผํ ์ ์ฅํ์์ต๋๋ค. ์ด 50๋ช ์ ์ฌ์ฉ์์ ๊ฐ์ ์ ์ฅํ์๋๋ฐ, ์ฌ์ฉ์๊ฐ ๋ ๋ง์์ง๋ค๋ฉด ์ด ๋ฐฉ๋ฒ์ ์ ์ ํ์ง ์์ ์ ์์ต๋๋ค.
๐ฉ๐ป N+1 ๋ฌธ์ ๋?
JPA๋ฅผ ์ฌ์ฉํด ๋ดค๋ค๋ฉด, N+1๋ฌธ์ ์ ๋ํด ๋ค์ด๋ดค์ ๊ฒ์ ๋๋ค.
๋๋ถ๋ถ์ ๊ฒฝ์ฐ x๋์ผ ์ฐ๊ด๊ด๊ณ์์ ์ฆ์ ๋ก๋ฉ์ผ๋ก ์ค์ ํ๋ฉด ํญ์ ํ ์ด๋ธ์์ ์กฐ์ธ์ด ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ์ฑ๋ฅ ํ๋์ด ์ด๋ ค์์ง๋๋ค. ๋ฐ๋ผ์ ์ฐ๊ด๊ด๊ณ๋ฅผ ์ง์ฐ ๋ก๋ฉ์ผ๋ก ์ค์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๊ธฐ๋ณธ๊ฐ์ด ์ฆ์ ๋ก๋ฉ์ด๊ธฐ ๋๋ฌธ์, ํ์์ ๋ฐ๋ผ ์ง์ฐ ๋ก๋ฉ์ผ๋ก ์์ ํด์ผ ํฉ๋๋ค.
@Getter
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class ParticipateChallenge {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "account_id")
private Account account;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "challenge_id")
private Challenge challenge;
...
}
์ฌ์ฉ์๊ฐ ์ฐธ์ฌํ๊ณ ์๋ ์ฑ๋ฆฐ์ง ๋ฆฌ์คํธ ์ ๋ณด๋ฅผ ์กฐํํ๊ธฐ ์ํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
public List<ParticipateChallengeRes> getParticipateChallengeListByAccountId(CustomUserDetails customUserDetails) {
Account account = accountRepository.findByEmailAndStatus(customUserDetails.getEmail(), Status.VALID)
.orElseThrow(() -> new CustomException(CustomExceptionStatus.ACCOUNT_NOT_FOUND));
List<ParticipateChallenge> list = participateChallengeRepository.findByAccountId(account.getId());
List<ParticipateChallengeRes> collect = list.stream().map(pc -> new ParticipateChallengeRes(pc.getChallenge().getId(), pc.getChallenge().getTitle()))
.collect(Collectors.toList());
return collect;
}
ParticipateChallenge ๊ฐ์ฒด๋ฅผ ์กฐํํ ๋, Challenge ๊ฐ์ฒด๋ Proxy ๊ฐ์ฒด๋ก ๋ฐํ๋ฉ๋๋ค.
์ค์ ๋ก Challenge์ ์ ๋ชฉ์ ๊ฐ์ ธ์ค๋ pc.getChallenge().getTitle() ์์ ์์, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์กฐํ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ง์ฐ ๋ก๋ฉ(FetchType.LAZY)๋ ๋ก๋ฉ๋๋ ์์ ์ LAZY ๋ก๋ฉ ์ค์ ์ด ๋์ด์๋ Challenge ์ํฐํฐ๋ฅผ ํ๋ก์ ๊ฐ์ฒด๋ก ๊ฐ์ ธ์ต๋๋ค.
์ดํ์ ์ค์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ์์ ์ ์ด๊ธฐํ๊ฐ ์ด๋ฃจ์ด์ง๊ณ , ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฟผ๋ฆฌ๊ฐ ๋๊ฐ๋๋ค.
- getChallenge()์ผ๋ก Challenge๋ฅผ ์กฐํํ๋ฉด ํ๋ก์ ๊ฐ์ฒด๊ฐ ์กฐํ๋๋ค.
- getChallenge().getTitle()์ผ๋ก ์ฑ๋ฆฐ์ง์ ํ๋์ ์ ๊ทผํ ๋ ์ฟผ๋ฆฌ๊ฐ ๋๊ฐ๋ค.
์ฆ, ParticipateChallenge๋ฅผ ์กฐํํ ๋๋ ๋จผ์ Proxy๋ก ๋ Challenge ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ค๊ฒ ๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ Challenge์ ์ค์ ๋ฐ์ดํฐ๊ฐ ํ์ํ ์์ ์ ์ด๊ธฐํ๋๋ฉฐ, ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฟผ๋ฆฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ด๋ก ์ธํด, ์ฌ์ฉ์๊ฐ ์ฐธ์ฌํ๊ณ ์๋ ์ฑ๋ฆฐ์ง์ ๊ฐ์์ ๋ฐ๋ผ ์ด 3๊ฐ์์ 5๊ฐ๊น์ง์ ์ฟผ๋ฆฌ๊ฐ ๋ฐ์ํ ์ ์๋ ์ํฉ์ ๋๋ค.
ParticipateChallenge ์ ์ฅ์์ Challenge๋ ์ง์ฐ๋ก๋ฉ์ผ๋ก ํ๋ก์ ๊ฐ์ฒด๋ก ์กฐํ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ฌธ์ ๋ API ์คํ์์ Challenge ํ๋ก์๋ฅผ ์ด๊ธฐํํ ๋๋ง๋ค, ์กฐํ๋ ParticipateChallenge๋งํผ ์ถ๊ฐ์ ์ธ ์ฟผ๋ฆฌ๊ฐ ๋ฐ์ํ๋ค๋ ๊ฒ์ ๋๋ค. ์ด๊ฒ์ด ๋ฐ๋ก N+1 ๋ฌธ์ ์ด๋ฉฐ ์กฐํ๋ ๋ฐ์ดํฐ๊ฐ ๋ง์์๋ก ์ฑ๋ฅ์ ์ํฅ์ ๋ฏธ์น ์ ์์ต๋๋ค.
๐ฉ๐ป ํด๊ฒฐ๋ฐฉ๋ฒ
๋ค์๊ณผ ๊ฐ์ด @Query๋ฅผ ํตํด fetch join์ ์ฌ์ฉํ์ฌ ํด๊ฒฐํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
@Query(value = "select p from ParticipateChallenge p join fetch p.challenge where p.account = :account")
List<ParticipateChallenge> findAllFetchJoin(Account account);
์ ์ฟผ๋ฆฌ๋ฅผ ์คํํ๋ฉด ํ๋ฒ์ ์ฟผ๋ฆฌ๋ก ParticipateChallenge์ Challenge๋ฅผ ํจ๊ป ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
๐ฉ๐ป ์ฑ๋ฅ ์ธก์
๊ทธ๋ ๋ค๋ฉด ์ผ๋ง๋งํผ์ ์ฑ๋ฅ ํฅ์์ ์ด๋ค๋ผ ์ ์์๊น์?
์์ฑํ ํ ์คํธ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ฌ ์ด์์๋ฒ์ ์ฟผ๋ฆฌ ๊ฐ์ ์ ๊ณผ ํ๋ฅผ ๋น๊ตํด๋ณด๊ฒ ์ต๋๋ค.
Agent๋ 1, Vuser per aget๋ 50์ผ๋ก ์ค์ ํ์ฌ ํ ์คํธ๋ฅผ ์งํํ์ต๋๋ค.
TPS(Transaction Per Second)๋ ์๋ฒ๊ฐ ์ด๋น ์ฒ๋ฆฌ ํ ์ ์๋ ์์ฒญ์ ๊ฐ์๋ฅผ ๋ํ๋ ๋๋ค.
์ฟผ๋ฆฌ ๊ฐ์ ์ ๊ณผ ํ๋ฅผ ๋น๊ตํ์ ๋ 88์์ 95๋ก, ์ฝ 8% ์์นํ์ต๋๋ค.
๐ฉ๐ป ๋ง์น๋ฉฐ
์๋ฒ ์ฑ๋ฅ๊ฐ์ ๊ธฐ(2)์ ๋ํ ๊ธ์ ์ฝ์ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค!
์ด ๊ธ์ ํตํด API ์ฟผ๋ฆฌ์ ์ฑ๋ฅ์ ํฅ์์ํค๊ณ N+1 ๋ฌธ์ ๋ฅผ ํด๊ฒฐํจ์ผ๋ก์จ ์ฑ๋ฅ ๊ฐ์ ์ ์ค๋ฌด์ ์ธ ๋ฅ๋ ฅ์ ํฅ์์ํฌ ์ ์๋ ์๋ฏธ์๋ ๊ฒฝํ์ด์์ต๋๋ค.
๋ค์ ๊ธ์๋ ๊ฐ๋ฐ์ ์งํํ๋ฉด์, ํฐ๋์์ ๊ณ ๋ฏผํ๋ ๋ถ๋ถ์ ํ์ด๋ด๋ ๊ธ๋ก ์ฐพ์์ค๊ฒ ์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค ๐
โฌ๏ธ ์ง๊ธ ๊ตฌ๊ธ ํ๋ ์ด ์คํ ์ด์์ ํฐ๋ ๋ค์ด ๋ฐ๊ธฐ
https://play.google.com/store/apps/details?id=com.team7.tikkle&hl=ko-KR
๐ฉ Contact : uuuuujeong00000@naver.com
๐ฒ SNS : https://www.instagram.com/may_u_j__
'๐ฉโ๐ป TECH' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TECH] ํด๋ฆฐ์ํคํ ์ฒ๋ฅผ ํฅํด์ ๐จ (0) | 2023.12.08 |
---|---|
[TECH] ์๋๋ก์ด๋ ๋ค์ํ ์ํคํ ์ณ ํจํด(MVC, MVP, MVVM) (0) | 2023.10.19 |
[TECH] ๊ฐ์ธ์ ๋ณด์ ๊ฐ๋ช ์ ๋ณด ๊ทธ๋ฆฌ๊ณ ์ต๋ช ์ ๋ณด๋? (1) | 2023.10.11 |
[TECH] ์๋ฒ API ์ฑ๋ฅ๊ฐ์ ๊ธฐ(1) (0) | 2023.09.27 |
[TECH] Kotlin Coroutine์ ์จ์ผํ๋ ์ด์ (0) | 2023.09.22 |