我应该使用线程还是任务-多客户端模拟 [英] Should I use Threads or Tasks - Multiple Client Simulation

查看:90
本文介绍了我应该使用线程还是任务-多客户端模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个客户端模拟程序,其中所有模拟的客户端都针对服务器运行一些预定义的例程-服务器是一台具有四个实例的Azure服务器.

I am writing a client simulation program in which all simulated client runs some predefined routine against server - which is a web server running in azure with four instances.

所有模拟的客户端连接到服务器后都运行相同的例程.

All simulated client run the same routine after getting connected to server.

我想随时使用我的程序模拟300到800个客户端.

At any time I would like to simulate 300 to 800 clients using my program.

我的问题是: 我应该创建N个客户端类实例并在N个不同的线程中运行它们吗?或

My question is: Should I create N instances of client class and run them in N different threads? OR

我应该使用任务库来做这些事情吗?

Should I use Task Library to do the things?

推荐答案

您当然不应创建800个线程.

You certainly should not create 800 threads.

让我们退后一步.您有一个称为服务器"的设备,该设备从客户端"接收请求",然后将响应"发送回这些客户端.假设请求是邮局发送的纸件,而答复是装有书的盒子,邮局也发送了邮件.

Let's take a step back here. You have a device called a "server" which takes in "requests" from "clients" and sends out "responses" back to those clients. Let's suppose that the requests are pieces of paper delivered by the post office, and the responses are boxes containing books, also delivered by the post office.

您希望模拟 800个客户端以测试服务器.

You wish to simulate 800 clients in order to test the server.

让我们假设一个线程是一个人,一个处理器是一个椅子.一个人只能坐在椅子上做工作.

Let's suppose that a thread is a person and a processor is a chair. A person can only do work while sitting in the chair.

创建800个线程等同于外出并雇用800人,并支付每个线程的费用将一封信发送到服务器.但是您只有四把椅子,所以这800人必须轮流使用椅子.

Creating 800 threads is the equivalent of going out and hiring 800 people, and paying each of them to send a letter to the server. But you only have four chairs, so those 800 people have to all take turns using the chairs.

那将是现实生活中的荒谬解决方案.线程像人一样,非常昂贵.您应该尽量减少创建的线程数.

That would be a ludicrous solution in real life. Threads, like people, are insanely expensive. You should be minimizing the number of threads you create.

那么,您应该改为通过任务工厂创建800个任务,让TPL为您并行化它们吗?

So, should you instead create 800 tasks via the task factory and let the TPL parallelize them for you?

不,您也不应该这样做. TPL有一群人(线程)可供借鉴,它会尝试安排事情,以使工资单上的人员不超过可供他们坐在的椅子.但是您的任务不是由椅子决定的"- -人们将坐在椅子上,将请求发送到服务器,然后在等待响应返回时离开椅子. 在等待的同时,TPL现在必须雇用更多的人来完成其他任务.

No, you should not do that either. The TPL has a pool of people (threads) to draw from, and it tries to arrange things so that there are no more people on the payroll than there are chairs for them to sit in. But your task is not "chair bound" -- the people are going to sit in the chair, send the request to the server, and then get out of the chair while they wait for the response to come back. While they are waiting, the TPL now has to hire more people to service the additional tasks.

命中Web服务器受I/O约束; 您只应为受CPU约束的任务创建线程池任务.

Hitting a web server is I/O bound; you should only create thread-pooled tasks for tasks that are CPU bound.

正确的解决方案是雇用两个人.

The right solution is to hire two people.

一个人("I/O完成线程")除了在邮箱中丢弃请求并检查传入的程序包外什么都不做.另一个人(即模拟"人)将得出模拟800个客户的正确时间表".模拟人员制定时间表,然后入睡.当需要将另一个请求发送到服务器时,她会醒来.当她醒来时,她告诉I/O完成线程将这封信放到邮箱中,并在收到响应时将她唤醒.然后她重新进入睡眠状态,直到是时候发送另一个请求或响应了.需要验证.

One person -- the "I/O completion thread" -- does nothing but drop requests in the mailbox and check for incoming packages. The other person -- the "simulation" person -- works out what the right "schedule" is for simulating 800 clients. The simulation person works out the schedule, and then goes to sleep. She wakes up when it is time to send another request to the server. When she wakes up, she tells the I/O completion thread to drop this letter in the mailbox, and wake her up when the response comes in. She then goes back to sleep until either it is time to send another request, or a response comes in that needs to be verified.

您应该做的是(1)获得C#5的beta版,并使用async/await创建将请求发送到服务器的任务,然后将控制权交还给消息循环,直到可以发送该消息为止另一个请求或响应进入.或者,如果您不想使用C#5,则应创建任务完成源,并设置具有正确延续性的任务.

What you should do is either (1) get the beta version of C# 5 and use async/await to create tasks that send requests to the server, and then yield control back to the message loop until either it is time to send another request or a response comes in. Or, if you don't want to use C# 5, you should create a Task Completion Source, and set up tasks that have the right continuations.

简而言之:处理许多并行I/O任务的正确方法是创建很少数量的线程,每个线程一次执行的工作量非常少.让I/O完成线程处理I/O的详细信息. 您不需要雇用800个人就可以模拟发送800个字母.雇用两个人们,一个可以观看邮箱,一个可以写信.

In short: the right way to handle many parallel I/O tasks is to create a very small number of threads, each of which does a very small amount of work at a time. Let the I/O completion thread handle the details of the I/O. You do not need to hire 800 people in order to simulate sending 800 letters. Hire two people, one to watch the mailbox and one to write the letters.

这篇关于我应该使用线程还是任务-多客户端模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆