๐ ๐ Python ์ฑ๋ฅํ ์คํธ (cProfile, TimeIt)
์ค๋์ Python Performance Test๐๐ ์ ๋ํด ์๊ธฐ๋ฅผ ํด๋ณผ๊นํฉ๋๋ค. ์ฑ๋ฅ ํ ์คํธ๋ ๋ถํ์ํฉ์ ํฌํจํ ์ฌ๋ฌ ์ํฉ์ ๋ง๋ค์ด ์๋น์ค์ ์ฑ๋ฅ์ ์ธก์ ํ๊ณ , ๊ธฐ๋ํ๋ ๊ฒฐ๊ณผ๊ฐ ๋์ถ๋๋์ง ํ์ธํ๋ ํ ์คํธ์ ๋๋ค JAVA์์๋ nGrinder, Jmeter๋ฑ์ ํตํด์ ์ฑ๋ฅ์ ์ธก์ ํ ์ ์๋๊ฑด ์ด๋ฏธ ์ ๋ช ํ์ฃ . ํ์ด์ฌ๋ ์ด๋ฐ ์ฑ๋ฅํ ์คํธ๋ฅผ ํ ์ ์๋๋ก ์ฌ๋ฌ๊ฐ์ง๋ค์ ์ ๊ณตํ๊ณ ์์ด์. ์ค๋์ ์ฑ๋ฅํ ์คํธ์ ์ ์ฉํ cProfile๊ณผ Timelt์ ์๊ฐํ๊ฒ ์ต๋๋ค. ๐ cProfile (https://docs.python.org/3/library/profile.html) cProfile์ ์ด๋ฆ์์ ์์ ์๋ฏ์ด ํ๋กํ์ผ๋ง์ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. ํ๋กํ์ผ๋ง์ ํตํด์, ํจ์์ ํธ์ถ, ๋ฐํ, ์์ธ๋ฅผ ์ถ์ ํ๊ณ , ๊ฐ ํจ์ ํธ์ถ๊ณผ ์ด๋ฒคํธ๋ฅผ ์ถ์ ํฉ๋๋ค. ํ์ด์ฌ์ ํ์ค๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก cProfile, profile์ ์ ๊ณตํฉ๋๋ค. ์ด๋ฅผ ์ด์ฉํด์ ์ฝ๋์ ๋ณ๋ชฉ์ด ๋๋ ๊ตฌ๊ฐ์ด ์ด๋์ธ์ง ํ์ธ ํ ์ ์์ต๋๋ค. (* ํ ์คํธ ์ด๋ฏธ์ง ์ฐธ์กฐ, ํด๋ฆญํ์ฌ ์๋ณธ์ด๋ฏธ์ง๋ก ๋ณด๋๊ฒ ํธํฉ๋๋ค.) โถ๏ธ ncalls : ํจ์์ ํธ์ถ ์ โถ๏ธ tottime : ํด๋น ํจ์ ์คํ์ ์ฌ์ฉ๋ ์ ์ฒด ์๊ฐ ((์๋ธ ํจ์ ํธ์ถ์ ๋ ์๊ฐ์ ์ ์ธ) โถ๏ธ tottime percall : tottime์ ncalls๋ก ๋๋ ๊ฐ โถ๏ธcumtime : ์ด ํจ์์ ๋ชจ๋ ์๋ธ ํจ์์์ ์์๋ ๋์ ์๊ฐ (ํธ์ถ์์ ์ข ๋ฃ๊น์ง). โถ๏ธ cumtime percall : cumtime์ percall(cumtime/number) โถ๏ธ filename:lineno(functon) : ํ์ผ๋ช :์ค ๋ฒํธ(ํจ์) ๐ TimeIt (https://docs.python.org/3/library/timeit.html) ์์ ์ฝ๋ ์กฐ๊ฐ์ ํ ์คํธํ ๋, ํ๋ก์ ํธ์ ์ ์ฒด ์คํ์๊ฐ์ด ์๋ ํน์ ํจ์๋ ์ฝ๋์ ์คํ์๊ฐ์ ํน์ ํ๋๋ฐ ์ ์ฉํฉ๋๋ค. (* ํ ์คํธ ์ด๋ฏธ์ง ์ฐธ์กฐ, ํด๋ฆญํ์ฌ ์๋ณธ์ด๋ฏธ์ง๋ก ๋ณด๋๊ฒ ํธํฉ๋๋ค.) โ timeit.timeit(stmt='pass', setup='pass', timer=, number=1000000, globals=None) โถ๏ธ stmt: ์ธก์ ํ ํจ์๋ ์ฝ๋ โถ๏ธ setup: setup ๋งค๊ฐ๋ณ์๋ ์ํํ ์ฝ๋์ ํ์ํ ๋ด์ฉ์ ์ ์ํ๋ ๋ถ๋ถ โถ๏ธ number: ํจ์/์ฝ๋ ๋ฐ๋ณต ์คํ ํ์ โถ๏ธ timer: Timer ์ธ์คํด์ค โถ๏ธ repeat: ํ์ด๋จธ ๋ฐ๋ณต ํ์ (๊ธฐ๋ณธ๊ฐ 5) โถ๏ธ globals: ์ฝ๋๋ฅผ ์คํํ ๋ค์ ์คํ์ด์ค๋ฅผ ์ง์ ํฉ๋๋ค. JAVA์ ๋นํด์ ์ฑ๋ฅ์ธก์ ํ๋ ๋ฐฉ์์ด ๋ค์ ๋ ๊ฒ์ ๋๋์ด ๋ค๊ธด ํ์ง๋ง, ๊ฝค๋ ์ ์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด๋ ์ถฉ๋ถํ ์ ํ์ฉํ์ จ์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค. ๐ Reference https://whitewing4139.tistory.com/156 https://wikidocs.net/133019 https://velog.io/@hamdoe/Python-cProfile%EA%B3%BC-Pstat%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%B4-%ED%94%84%EB%A1%9C%ED%8C%8C%EC%9D%BC%EB%A7%81-%ED%95%98%EA%B8%B0 https://stackoverflow.com/questions/44890932/using-the-globals-argument-of-timeit-timeit