简体   繁体   English

异步调用100线程的压力测试

[英]Stress testin with Asynchronously Calling 100 thread

I just want to Call 100 thread asynchronously with C#(you can think each thread one user) and I want to get serialization time avarage of my code.Should I use Thread Pool for this? 我只想用C#异步调用100个线程(您可以认为每个线程一个用户),并且想获取我的代码的序列化时间。我应该使用线程池吗? Thx 谢谢

I just want to Call 100 thread asynchronously 我只想异步调用100个线程

What do you exactly mean? 你到底是什么意思

If you need to execute 100 tasks asynchronously -ThreadPool is the best solution. 如果您需要异步执行100个任务,则-ThreadPool是最佳解决方案。 You don't care about threads, just queue work items into pool, and operating system split items amongst available threads. 您无需关心线程,只需将工作项排队到池中,并在可用线程之间进行操作系统拆分。 You should realize that all your 100 tasks could be processed just with 3 or 5(for example) threads. 您应该意识到,仅用3个或5个(例如)线程即可处理所有100个任务。

If you need to create 100 threads and delegate each task to separate thread - ThreadPool isn't solution, because ThreadPool doesn't create new threads. 如果您需要创建100个线程并将每个任务委托给单独的线程-ThreadPool不是解决方案,因为ThreadPool不会创建新线程。 You should create them manually. 您应该手动创建它们。

// Create 100 threads
var threads = new List<Thread>(); 
for (int p = 0; p < 100; p++) 
{ 
    threads.Add(new Thread(() => MyTask())); 
} 
// Start them all
foreach (var thread in threads)
{
    thread.Start(); 
}
// Wait for completion
foreach (var thread in threads) 
{
    thread.Join(); 
}

You definitely can use ThreadPool for your task, but you should be aware of how ThreadPool manages threads. 您绝对可以将ThreadPool用于您的任务,但是您应该了解ThreadPool如何管理线程。 If you try to start 100 threads in a way like this: 如果您尝试以这种方式启动100个线程:

for (int i = 0; i < 100; i++)
{
    ThreaPool.QueueUserWorkItem(state => UnitOfWork());
}

then there is no any guaranty that all 100 threads will execute in separate threads simultaneously. 那么就没有保证所有100个线程将同时在单独的线程中执行。 ThreadPool queues tasks, and execute them using free threads in ThreadPool, hence it can queue all your tasks one after another and execute them one after another. ThreadPool将任务排队,并使用ThreadPool中的空闲线程执行任务,因此它可以将所有任务一个接一个地排队,然后一个接一个地执行。 I don't say that it will do so, but hypothetically it can be. 我并不是说会这样做,但可以假设是这样。

But I think you shouldn't bother about it. 但我认为您不应该为此而烦恼。 Try to use ThreadPool first, set max treads limit to 100 (or above) with ThreadPool.SetMaxThreads(100) and queue requests as I wrote above. 尝试首先使用ThreadPool,使用ThreadPool.SetMaxThreads(100)将最大踩踏限制设置为100(或更高),并按我在上文中所述将请求排队。

And if after that you would like something more complicated you can use the Thread class. 如果之后您想要更复杂的东西,可以使用Thread类。 Create 100 instances of the Thread class, start them all and use. 创建Thread类的100个实例,全部启动并使用。 If you would use Thread class you can control lifetime of each thread, ie wait for threads completion without using any additional tricks. 如果您将使用Thread类,则可以控制每个线程的生存期,即无需使用任何其他技巧即可等待线程完成。

如果将MaxThreads设置为100或更高,则可能是最佳解决方案。

ThreadPool.SetMaxThreads(100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM