Thread Pool2
์๊ฐ์ ์ผ๋ก ๋ง์ ์์ฒญ์ด ๋ค์ด์ค๋ ๊ฒฝ์ฐ Thread Pool์ด ์ด๋ป๊ฒ ๋์ํ๋์ง ์์๋ณด์.
Source code
https://github.com/teamsmiley/gitbook-sample/tree/main/ThreadTest
locust
locust -f locust.py --host http://localhost --users 1000 --spawn-rate 1000source code
Console.WriteLine($"The number of processors on this computer is {Environment.ProcessorCount}.");
var maxWorkerThreads = 0;
var maxCompletionPortThreads = 0;
ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
Console.WriteLine("Maximum worker threads: {0}", maxWorkerThreads);
var availableWorkerThreads = 0;
var completionPortThreads = 0;
ThreadPool.GetAvailableThreads(out availableWorkerThreads, out completionPortThreads);
Console.WriteLine($"Available Worker threads: {availableWorkerThreads}", availableWorkerThreads);
var usedWorkerThread = maxWorkerThreads - availableWorkerThreads;
Console.WriteLine($"Used worker threads: {usedWorkerThread}");
int minWorker, minIOC;
// Get the current settings.
ThreadPool.GetMinThreads(out minWorker, out minIOC);
Console.WriteLine("Minimum worker threads: {0}", minWorker);
// Thread.Sleep(1000 * 10); // Sleep for 10 seconds
Console.WriteLine("Thread completed. go back to the pool.");
return usedWorkerThread;์์ฒญ์ ํด๋ณด์.
1000๊ฐ์ ์์ฒญ์ ๊ณ์ ๋ณด๋ด๋ Threadpool queue๋ ๋ง์ด ๋์ด๋์ง ์๋๋ค.

์๋๋ฉด ์์ฒญ์ ์ฒ๋ฆฌํ ๋ฐ๋ก ๋ค์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ด๋ค.
์ด์ 10์ด๋ฅผ ๋๊ธฐํด๋ณด์.
Thread.Sleep(1000 * 10); // Sleep for 10 seconds์ด๋ถ๋ถ์ ์ฃผ์์ ์ ๊ฑฐํ๊ณ ๋ค์ ์คํ

๋ชจ๋ ์์ฒญ์ด ํ๋ก ๋ค์ด๊ฐ๊ณ Thread๊ฐ ํ๋์ฉ ์์ฑ์ด ๋๋ค. 10์ดํ์ ์์ฒญ ์ฒ๋ฆฌ ์๋ฃ๋ Thread๋ ๋ค์ pool๋ก ๋ค์ด๊ฐ์๋ง์ ์์ ์ ํ๋ฏ๋ก queue๊ฐ ์ด๋์ ๋ ๋์ด๋ํ ์ํ๋ฅผ ์ ์งํ๋ค.
๋ฌธ์ ๋ฐ์
2000๊ฐ๊ฐ ํ์ ๋ค์ด๊ฐ๊ณ ์ด๋น 1-2๊ฐ์ฉ์ด Thread๊ฐ ์์ฑ์ด ๋๊ณ ์๋ค. ์ด๊ฒ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค.
Thread ์์ฑ๊ฐ์ง๋ง 1000์ด๋ฅผ ๊ธฐ๋ค๋ ค์ผํ ์๋ ์๋ค.
Threadpool ์ค์ ๋ณ๊ฒฝ
์ด์ Threadpool์ min์ค์ ์ ๋ณ๊ฒฝํด๋ณด์.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ThreadPoolMinThreads>1000</ThreadPoolMinThreads> <!-- ์ด๊ฑฐ ์ถ๊ฐ -->
</PropertyGroup>
</Project>๋ค์ ์คํํด๋ณด์.

minThreadworker๊ฐ 1000๊ฐ๋ก ์ค์ ๋์๋ค
ํ ์คํธํด๋ณด์.

1000๊ฐ์ ์ฐ๋ ๋๊ฐ ์์ผ๋ ํ์ ๋ค์ด๊ฐ์ง ์๊ณ 1์ด์ ํ๊ฐ์ฉ ์์ฑ์ด ๋์ง ์๊ณ ๋ชจ๋ ์๋น์ค์ ์ฌ์ฉํ ์ ์๋ค.
๊ฒฐ๋ก
๊ฐ์๊ธฐ ์์ฒญ์ด ๋ง์๊ฒฝ์ฐ๋ ๋ชจ๋ํฐ๋ง์ ํด๊ฐ๋ฉด์ queue๊ฐ 60๊ฐ ์ด์(๋ชจ๋ ์์ฑ๊น์ง ๋ชจ๋ 60์ด๊ฐ ๊ฑธ๋ฆผ) ์ด์์ด ๋๋ฉด minThread๋ฅผ ๋๋ฆฌ๋๊ฒ์ด ์ข๋ค.
๋ ์ค์ํ ๊ฒฐ๋ก
์ฌ๊ธฐ์๋ cpu count๋ ๋ณ ๋ฌธ์ ๊ฐ ์๋๋ค.
1๊ฐ์ cpu๋ 1๊ฐ์ minThread๋ฅผ ๊ฐ์ง๊ณ ์๊ณ 12๊ฐ์ cpu๋ 12๊ฐ์ minThread๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
์์ฒญ์ด ๋ง์ผ๋ฉด 1๊ฐ๋ 12๊ฐ๋ ํฐ ์๋ฏธ๋ ์๋ค๊ณ ๋ณด์ฌ์ง๋ค.
๋ชจ๋ํฐ๋ง์ ์ํด๋ณด์.
docker exec -it threadtest-web-1 /tools/dotnet-counters monitor --process-id 1๋ ๋ ์ค์ํ ๊ฒฐ๋ก
web api์์ ์์ฒญ์ ๋ฐ์ผ๋ฉด ๋ชจ๋ ๊ฑธ async๋ก ์ฒ๋ฆฌํด์ผํ๋ค. ํนํ db query๋ async๋ก ์ฒ๋ฆฌํด์ผํ๋ค. entity framework์์๋ async method๋ฅผ ์ง์ํ๋ฏ๋ก ๋ชจ๋ ๊ทธ๊ฑธ ์ฌ์ฉํ๋ฉด ์ข์๊ฑฐ๊ฐ๋ค.
Last updated
Was this helpful?