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 1000

source 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