简体   繁体   English

如何测量从客户端发送数据到服务器所花费的时间

[英]How to measure time taken for sending data from client to server

I am sending data from the client to a server.我正在将数据从客户端发送到服务器。 I want to accurately calculate the time taken for the data to reach the destination.我想准确计算数据到达目的地所需的时间。 The client and server are on different PCs, so I think it is unwise to use the system time as they are on different systems.客户端和服务器在不同的PC上,所以我认为使用系统时间是不明智的,因为它们在不同的系统上。

Please advise the most accurate method for measuring the time taken for the data to reach the destination.请告知测量数据到达目的地所用时间的最准确方法。

close will block until all data is sent, so if you start time at write , and end after close , you will have the send time. close将阻塞,直到所有数据都发送完毕,因此如果您从write开始时间,并在 close 之后close ,您将拥有发送时间。 For UDP, the data will have been transmitted, and for TCP it will have been acked.对于 UDP,数据将已传输,对于 TCP,数据已被确认。 If you are sending to a Mars rover, then the time will be round trip, but normally the ack time is small.如果你发送到火星车,那么时间将是往返,但通常确认时间很短。

start_time = someTimeFunc();
char *foo = buf;
while (size) {
    ssize_t sent = send(sock,foo,size,flags);
    if (sent <= 0)
        fail;
    foo += sent;
    size -= sent;
}
close(sock);
end_time = someTimeFunc();

When an app "sends" data, the data is not actually transmitted right away.当应用程序“发送”数据时,数据实际上并没有立即传输。 It is put in a queue in the OS kernel, and then control is returned to the app.它被放入操作系统 kernel 中的队列中,然后将控制权返回给应用程序。 The kernel will transmit the queued data in the background on its own time. kernel 将在自己的时间在后台传输排队的数据。 There is no way to know when the kernel actually transmits, or how long the data takes to arrive as the destination.无法知道 kernel 何时实际传输,或者数据到达目的地需要多长时间。 And even then, the data is put into another kernel queue at the receiving end, waiting for the receiving app to extract the data from that queue.即使这样,数据也会被放入接收端的另一个 kernel 队列中,等待接收应用程序从该队列中提取数据。

So, the only way to do what you are asking for is to have the receiver explicitly send an acknowledgement back to the sender, specifying when the data was actually read by the receiver.因此,完成您要求的唯一方法是让接收方显式向发送方发送确认,指定接收方实际读取数据的时间 The sender and receiver will have to agree on a time scale, like UTC, and also have their clocks synced to a common source.发送方和接收方必须在时间尺度上达成一致,例如 UTC,并且还必须将它们的时钟同步到一个公共源。 However, this will NOT be a true reflection of the transmission time, but it will likely be close if both sender and receiver are handling their respective socket I/O's in a timely manner.然而,这并不是传输时间的真实反映,但如果发送者和接收者都及时处理它们各自的套接字 I/O,它可能会关闭

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

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