Andorid performance optimization (8) network request optimization

1 Introduction

Today, the mobile Internet is booming, and almost all of the apps on our phones need to be connected. The enhancement of mobile phone hardware and the low price, as well as the reduction and acceleration of the operator's traffic costs, make users' sensitivity to network request waiting and traffic consumption also decrease. Even so, the network problems in the development of an App itself cannot be ignored. For example, when there is always insufficient traffic, in a scenario where the network status is not good, and when the mobile phone has insufficient power, an excellent App can be obvious See the advantages, so the network optimization is also an indispensable optimization item in our App optimization.

2 The process of a network request

Research shows that any delay of more than one second will interrupt the user's state of mind and bring a poor experience. As mentioned in the Google development documentation, how to render a page in one second, as shown in the following figure, here is a good analysis of the steps to be taken to render a page in a network request.

The first is DNS resolution IP, 200 ms

The second is the TCP three-way handshake connection, 200 ms

The third is the client HTTP request server and the server responds, 200 ms

The fourth is that the server itself takes some time to respond, 200 ms

Fifth, the client receives the content to parse html, css, js, etc. and render, 200 ms

The above is an HTTP request. If it is an HTTPS request, a TLS handshake will be added after the TCP three-way handshake connection, so that the handshake time will be longer.

3 Network request optimization tips

3.1 DNS resolution optimization

DNS pre-resolution

If we are doing web page development, we can consider DNS pre-resolving. Pre-resolving is actually resolving the existence of different domain names in the current Web page, and pre-resolving the DNS into IP before the user click behavior has occurred.

Replace the domain name directly with the IP address

According to specific business requirements, you can use the direct IP connection to replace the domain name access method, or you have to use IP to try to access, if the access fails, switch back to the domain name, so as to achieve faster network requests. However, this behavior is generally not recommended because it is inflexible and the maintenance cost is extremely high.

3.2 Reduce the amount of data transmission

Consider using Protocol Buffers

Protocol Buffers is a data description language developed by Google. Similar to Json / XML, it can serialize structured data and can be used for data storage and communication protocols. It is smaller than the currently used Json data volume, but to use it, the client needs to reference a parsing library of several hundred K.

Consider using FlatBuffers

FlatBuffers is also a cross-platform serialization tool developed by Google. It provides C ++ / Java / Go / C # interface support. This is a serialization class library that focuses on performance and resource usage. Compared to Protocol Buffers, which are more suitable for mobile devices, FlatBuffers provide higher performance and lower resource requirements. The functions of FlatBuffers are very similar to Protocol Buffers. Their biggest difference is that before using specific data, FlatBuffers do not need to parse / unpack the process. At the same time, when used in engineering, FlatBuffers is much more convenient than Protocol Buffers, and only needs to include two or three header files.

Use zip for data compression

After we have decided which data description language to use, such as the most commonly used Json, we can remove the spaces and use zip to pack and compress it. Such compression can greatly reduce the original data volume. The size of the access speed can be improved. For example, the author once tried to format a Json with more than one hundred kilos, and after removing spaces, it became only about 50K, and then compressed and compressed by zip, it became only 5K.

Use Gzip compression

The HTTP protocol supports Gzip encoding, which is a technology used to improve the performance of WEB applications. It is used to reduce the amount of data transferred and reduce the size of data transferred. Gzip is high-pressure and can compress files to smaller sizes. But not all browsers support gzip. If the client supports gzip compression, the requested resource is compressed and returned to the client when responding. The browser parses it in its own way. In the http response header, we can see the content -encoding: gzip, this refers to the compression method used by the server gzip.

Image Compression

Using origin, guetzli, or tinypng tools can compress the image with a high degree of reduction.

Use WebP format

The same photo, using WebP format can greatly reduce the size of small pictures and save traffic, you can consider using WebP picture format instead of JPEG or PNG picture format. The most important thing is that the image quality has not changed after using WebP.

3.3 Reduce network requests or pre-requests

Merge the interface of customized network requests to reduce the number of requests as much as possible, and merge the requests that can be merged as much as possible.

For example, some reporting logic can store the data that needs to be reported multiple times locally, and choose a suitable time to upload it at one time.

Make some important data requests in advance when the phone is idle to enhance the user experience.

3.4 Reasonable request, reduce retry and avoid polling

1 According to the specific business requirements when in the background, strictly limit whether the App needs to continue to really need data transmission when the App is in the background, and try to avoid invalid data transmission.

2 It is necessary to avoid unrestricted cyclic retry connection when the network request fails. You can set a maximum number of reconnections, end the reconnection after exceeding the number of times, or wait for a long time before trying to connect again. And before reconnecting, make sure that the network is in normal condition, otherwise, in addition to not retrying successfully, an additional cancellation is added. When the request on some pages fails, the result of the request can be thrown to the user to let the user determine whether to re-request according to their own situation.

3 Normal service functions should also avoid polling to initiate network requests to the server. To determine the frequency of data transmission, to avoid redundant and repeated data transmission. Or use push to replace as much as possible.

3.5 Using cache

For data such as pictures and files, you can use the memory cache plus disk cache strategy mechanism. In Android, it is typical to use LruCache to implement memory caching and DiskLruCache to implement disk caching. Or use a more mature open source framework for effective network request and cache processing.

3.6 Processing for different network types

Before requesting a picture, you can first obtain the current network type. If it is Wifi, enjoy the advanced experience brought by high-definition pictures. If it is a 4G network, choose a standard and clear picture to deliver. Next, consider sending pictures with poor definition to achieve priority for normal use.

4 Use tools to monitor the network

4.1 Charles

Charles is a powerful network monitoring tool. As long as the mobile phone and computer are connected to the same network and some corresponding proxy settings are set on them, the request data of the mobile phone can be captured through the computer. The operation steps are as follows:

The first step is to open the Proxy Settings window.

The second step is to check the Enable transparent HTTP proxying option and set the port number, which is 8888 by default.

The third step is to enter the IP address on the computer and the port number just set in the Wifi selection of the mobile phone, and then confirm.

The fourth step, after the mobile phone operation is completed, the following dialog box will pop up on the computer, select "Allow" to allow.

4.2 Network Profiler

The Network Profiler that comes with Android Stuido 3.0 can display real-time network activities on the timeline, including the data sent and received and the current number of connections. This makes it easy for you to see how and when the application transfers data, and optimize the underlying code accordingly. As shown on the official website:

https://developer.android.google.cn/studio/images/profile/networkprofiler_2x.png

4.3 Set virtual machine network parameters to simulate weak network

The user's environment is unpredictably complex, so in addition to normal network optimization, we also need to consider the App's performance under weak network conditions. Through the " Cellular " item of the virtual machine, a network parameter can be set to simulate a weak network for corresponding testing. As shown below:

Among them, Network type is the type of network; Signal strength is the signal strength; Voice status is the voice status; Data status is the data status. They can be selected as needed.

 

 

 

Published 106 original articles · praised 37 · 80,000 views

Guess you like

Origin blog.csdn.net/lyz_zyx/article/details/88324777