📗
smiley book
  • Smiley Books
  • AI
    • Readme
    • openai-whisper
      • 샘플 실행해보기
      • GPU 서버 준비하기
      • API로 whisper를 외부에 오픈하기
      • 프롬프트 지원
      • 실시간 message chat
      • 화면 이쁘게 만들기와 로그인
      • 파이썬 가상환경
      • 실시간 voice chat
      • fine tunning(미세 조정) 으로 성능 올리기
      • app에서 api를 호출하여 실시간으로 텍스트로 바꿔보기
    • ollama - llm을 쉽게 내컴에서 실행
      • ollama webui
      • ollama docker
    • stable diffusion
      • SDXL - text to image
      • SD-webui
    • ChatGPT
      • 답변이 끊길때
      • 역할을 정하자
      • 구체적인 질문
      • 결과형태를 지정
      • 프롬프트를 여러줄로 사용하자.
      • 마틴 파울러 글 번역본
    • Prompt Engineering
    • Auto-GPT
    • Gemini
      • google ai studio
      • gemini-api
      • embedding guide
    • Huggingface
      • huggingface 사용법
      • huggingface nlp 공부중
    • kaggle
      • download dataset
    • langchain
      • langchain을 공부하며 정리
      • basic
      • slackbot
      • rag
      • document-loader
      • website-loader
      • confluence
      • memory
      • function-call
      • langsmith
      • agent-toolkit
  • Ansible
    • templates vs files and jinja2
    • dynamic inventory
    • limit 옵션 강제하기
    • limit 사용후 gather_fact 문제
  • AWS
    • AWS CLI
    • EKS
      • cluster manage
      • ALB Controller
      • external-dns
      • fargate
    • ECR
    • S3
    • Certificate Manager
  • Azure
    • Azure AD OAuth Client Flow
  • Container
    • Registry
    • 빌드시에 env값 설정하기
  • DB
    • PXC
      • Operator
      • PMM
      • 삭제
      • GTID
      • Cross Site Replication
    • Mssql
    • Mysql
  • dotnet
    • Thread Pool
    • Connection Pool
    • Thread Pool2
  • Devops
    • Recommendation
  • GIT
    • Basic
    • Submodule
  • GitHub
    • Repository
    • GitHub Action
    • GitHub PR
    • Self Hosted Runner
    • GitHub Webhook
  • GitLab
    • CI/CD
    • CI/CD Advance
    • Ssl renew
    • CI/CD Pass env to other job
  • Go Lang
    • 개발 환경 구축
    • multi os binary build
    • kubectl같은 cli만들기
    • azure ad cli
    • embed static file
    • go study
      • pointer
      • module and package
      • string
      • struct
      • goroutine
  • Kubernetes
    • Kubernetes는 무엇인가
    • Tools
    • Install with kubespray
    • Kubernetes hardening guidance
    • 11 ways not to get hacked
    • ArgoCD
      • Install
      • CLI
      • Repository
      • Apps
      • AWS ALB 사용
      • Notification slack
      • Backup / DR
      • Ingress
      • 2021-11-16 Github error
      • Server Config
      • auth0 인증 추가(oauth,OIDC)
    • Extension
      • Longhorn pvc
      • External dns
      • Ingress nginx
      • Cert Manager
      • Kube prometheus
    • Helm
      • Subchart
      • Tip
    • Loki
    • Persistent Volume
    • TIP
      • Job
      • Pod
      • Log
  • KAFKA
    • raft
  • KVM
    • kvm cpu model
  • Linux
    • DNS Bind9
      • Cert-Manager
      • Certbot
      • Dynamic Update
      • Log
    • Export and variable
    • Grep 사용법
  • Modeling
    • C4 model introduce
    • Mermaid
    • reference
  • Monitoring
    • Readme
    • 0. What is Monitoring
    • 1. install prometheus and grafana
    • 2. grafana provisioning
    • 3. grafana dashboard
    • 4. grafana portable dashboard
    • 5. prometheus ui
    • 6. prometheus oauth2
    • Prometheus
      • Metric type
      • basic
      • rate vs irate
      • k8s-prometheus
    • Grafana
      • Expolorer
    • Node Exporter
      • advance
      • textfile collector
  • Motivation
    • 3 Simple Rule
  • OPENNEBULA
    • Install(ansible)
    • Install
    • Tip
    • Windows vm
  • Reading
    • comfort zone
    • 배려
    • elon musk 6 rule for insane productivity
    • Feynman Technique
    • how to interview - elon musk
    • 경청
    • Readme
  • Redis
    • Install
    • Master-slave Architecture
    • Sentinel
    • Redis Cluster
    • Client programming c#
  • SEO
    • Readme
  • Security
    • criminalip.io
      • criminalip.io
  • Stock
    • robinhood-python
  • Terraform
    • moved block
    • output
  • vault
    • Readme
  • VS Code
    • dev container
    • dev container on remote server
  • Old fashione trend
    • curity
    • MAAS
      • Install maas
      • Manage maas
      • Tip
Powered by GitBook
On this page
  • Source code
  • locust
  • source code
  • 문제 발생
  • Threadpool 설정 변경
  • 결론
  • 더 중요한 결론
  • 더 더 중요한 결론

Was this helpful?

  1. dotnet

Thread Pool2

PreviousConnection PoolNextRecommendation

Last updated 1 year ago

Was this helpful?

순간적으로 많은 요청이 들어오는 경우 Thread Pool이 어떻게 동작하는지 알아보자.

Source code

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를 지원하므로 모두 그걸 사용하면 좋을거같다.

https://github.com/teamsmiley/gitbook-sample/tree/main/ThreadTest
alt text
alt text
alt text
alt text