The demand for network speed measurement is relatively common, for example, common mobile phone housekeeper, 360 Wi-Fi and other apps have network speed measurement function. Recently, due to business requirements, network speed measurement is also involved in the content, and I have not updated my blog for a long time, so I write a simple summary of network speed measurement.

What is network speed measurement?

In fact, when I heard the requirement of network speed measurement, the first thing that came to my mind was ping. In the group discussion, some students said that ping is ok, right? What is the connection and difference between Ping and Internet speed?

  • Ping: Packet Internet Groper is used to test whether the network is connected and the response time. Usually, the client also has a function called network diagnosis, in fact, this ping is used

  • Network speed: As the name implies, that is, the meaning of network speed. Slightly professional is the maximum amount of data that can be transmitted per second when uploading and downloading. The size of the data amount is generally measured by KB and M.

Look at the above explanation, in fact, you understand. Ping measures point-to-point network latency, while network speed refers to the amount of data transferred per unit of time between points. This data can be subdivided into upload data and download data, theoretically they are the same speed, but the operators will do something in between, so that the upstream broadband is generally less than the downstream broadband.

We usually use tools like Shadowrocket to flip the wall. This software displays your Wi-Fi and 4G traffic consumption, as well as how much uplink and downlink traffic you are getting per second. This principle is actually very simple, you can directly call the system interface to obtain network card traffic data. The upstream and downstream traffic per second only represents the current traffic consumption, not the current network speed. The network speed should be the maximum amount of data that can be transmitted per unit of time after the bandwidth is fully utilized

Common network velocimetry schemes

Through investigation, it is found that there are only two common network speed measurement schemes at present:

  • Option 1: Upload and download data packets, use TotalSize/TotalTime to calculate the true upload and download rate

  • Scheme 2: to calculate by reading nic data, read the overall traffic consumption T1 in the last second, and then read the current traffic consumption T2, so T2-T1 can actually be represented as a current network speed. The data can also be used to distinguish between cellular networks and Wi-Fi networks, as well as between upstream and downstream traffic.

The two schemes have their own advantages and disadvantages, and the corresponding scheme can be selected on appropriate occasions

  • The first scheme is more accurate. At this time, the data is actually downloaded or uploaded, which makes full use of the current bandwidth and the calculated network speed is close to the real network speed. But on a cellular network, it consumes a small amount of user traffic.

  • The second method is more similar to the first method when downloading and uploading things. However, if there is no App in use in the current system and it is in a static state, in fact, the current read traffic value is relatively small, which cannot reflect the network speed, but can reflect the real-time traffic consumption status.

For two kinds of schemes, I wrote a simple Demo on https://github.com/joy0304/Joy-Demo/tree/master/LDSpeedTest, this is just a rough Demo, not distinguish wi-fi and cellular networks, At the same time, only the downlink network speed is calculated. If it is to be used in the project, it can be used online only after improvement.

extension

In fact, the above mentioned real download or upload data for speed measurement, this is only a single TCP connection, if multiple TCP connections, certainly a use of bandwidth than a single TCP connection is more efficient, the value of network speed measured is also more accurate.

At the same time, due to the differences between TCP and UDP protocols, TCP and UDP speed measurement results are also different. You can select an appropriate scheme according to service requirements. For network speed measurement, the industry has a lot of powerful tools, if you are interested in iPERF and other tools.