简体   繁体   English

Linux套接字编程测试

[英]Linux socket programming testing

I am new to socket programming and I have written a code for the server using the epoll. 我是套接字编程的新手,并且已经使用epoll为服务器编写了代码。 Basically it would wait to accept for any client connection and it would send data to the clients if the data is available from other sources. 基本上,它将等待接受任何客户端连接,并且如果可以从其他来源获得数据,它将向客户端发送数据。

I need to test on how is the performance of the server when there are multiple requests for the connection and when the server is sending data to multiple clients. 我需要测试当有多个连接请求以及服务器向多个客户端发送数据时服务器的性能如何。 The question I have is how could I simulate a concurrent requests of the connection from a number of clients for the server? 我的问题是如何模拟服务器的多个客户端的连接并发请求? Do I create multiple threads to request or multiple processes or some other ways to test it? 是否创建多个线程来请求或使用多个进程或其他方式对其进行测试?

Thanks. 谢谢。

I usually create a client program simulating one session with the server, find as many linux boxes nearby and spin up a thousand of them on each machine from a command line like: 我通常会创建一个客户端程序来模拟与服务器的一次会话,在附近找到尽可能多的linux盒,然后通过类似以下命令的命令行在每台计算机上旋转上千个linux盒:

 for i in {0..1000} ; ./myprogram serverip & done

In some cases the protocol is text based and the interaction or test is simple, so I don't have to write myprogram but just use netcat , as in running nc serverip < input >/dev/null 在某些情况下,协议是基于文本的,并且交互或测试很简单,因此我不必编写myprogram而只需使用netcat ,就像在运行nc serverip < input >/dev/null

If all you need is to test streaming data to your client, I'd start with netcat and work my way from there. 如果您只需要测试流式传输到客户端的数据,那么我将从netcat开始,然后从那里开始。

This approach is ok for my needs, I usually don't need to test more concurrency than a handful of thousand clients. 这种方法可以满足我的需求,通常我不需要测试比数千名客户更多的并发性。 If you need more scalability, you'll likely have to write the client simulator using io multiplexing (epoll) as well, having each instance simulate as much interactions as you can - and you'll have to do more controlled tests to find limits of your client . 如果需要更高的可伸缩性,则可能还必须使用io多路复用(epoll)编写客户端模拟器,让每个实例都尽可能多地模拟交互-并且您必须进行更多的受控测试来找到限制你的客户。

Don't mix performance testing with functional testing though. 但是不要将性能测试与功能测试混在一起。 While you might get awsome performance in a test environment, a live environment can be very different. 尽管您在测试环境中可能会获得出色的性能,但实际环境可能会大不相同。 eg clients will misbehave, they will disconnect at the most inapproriate of times. 例如,客户会行为不端,他们会在最不合适的时间断开连接。 They might send you malicious data. 他们可能会向您发送恶意数据。 They might be slow, leading to buildup of internal queues on the server or leving you with thousands of seemingly idle connections ( killall -STOP nc while in the middle of sending data to test clients might show you interresting things) 它们可能很慢,导致服务器上内部队列的建立,或者为您提供了数千个看似空闲的连接(在向测试客户端发送数据的过程中, killall -STOP nc可能会显示出您的困扰)

You can do it using two threads one to send request and other to receive response. 您可以使用两个线程来做到这一点,一个线程发送请求,另一个线程接收响应。

To simulate multiple clients you can create multiple sub-interface using ifconfig command in a script or using ioctl in your c program. 要模拟多个客户端,可以在脚本中使用ifconfig命令或在c程序中使用ioctl创建多个子接口。

For sending data from client you can create array of multiple sockets and bind them to different sub interface IPs and loop through them for sending data and use select or poll for receiving data from server. 为了从客户端发送数据,您可以创建多个套接字的数组,并将它们绑定到不同的子接口IP,并在它们之间循环以发送数据,并使用select或poll从服务器接收数据。

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

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