【Android】APP network optimization study notes

Network optimization reasons

Performing web optimization is important for mobile applications for the following reasons:

user experience:

Network connectivity is one of the core functions of mobile applications. Through network optimization, the loading speed and response speed of the application can be improved, the waiting time of the user can be reduced, and a smoother and more efficient user experience can be provided. A fast and stable network connection can increase user satisfaction and improve application competitiveness.

Save traffic costs:

When using a mobile data connection on a mobile device, network traffic may be limited and users may be charged for data usage. Through network optimization, the network traffic consumed by the application in the background or foreground can be reduced, thereby reducing the data fee for the user to use the application and providing a more economical network experience.

Save battery consumption:

Network connectivity is one of the major power drains for mobile devices. Through network optimization, the frequent use of network connections by applications can be reduced, and the battery consumption of devices in continuous networked state can be reduced. This will extend the battery life of the device, allowing the user to use the device for a longer period of time.

To adapt to unstable network environment:

Mobile devices often face network instability, such as network delays and packet loss. Through network optimization, some strategies can be adopted to deal with unstable network environments, such as retry mechanism, request coalescing, data compression, etc., to ensure that applications can provide stable and reliable services under various network conditions.

Comply with specifications and restrictions:

Mobile device platforms, such as Android and iOS, typically have some network usage norms and restrictions. For example, an app may need to obey background network restrictions, maximum number of requests, data usage policies, etc. Performing network optimization can ensure that applications comply with the specifications of the corresponding platform and avoid restrictions or penalties.

Network Optimization

The process that a normal network request needs to go through is as follows:

1. DNS resolution, request DNS server, obtain the IP address corresponding to the domain name;
2. Establish connection with the server, including TCP three-way handshake, security protocol synchronization process;
3. Connection establishment is complete, send and receive data, and decode data.

There are three obvious optimization points here:
1. Use the IP address directly and remove the DNS resolution step;
2. Do not re-establish the connection for each request, reuse the connection or always use the same connection (long connection);
3. Compress data , to reduce the size of the transmitted data.

DNS optimization

DNS (Domain Name System), its role is that 根据域名查出IP地址it is the premise of the HTTP protocol, only after the domain name is correctly resolved into an IP address, the subsequent HTTP process can proceed.

The complete DNS resolution process is very long. It will be fetched from the local system cache first. If not, it will be fetched from the nearest DNS server. If not, it will be fetched from the main domain name server. Each layer has a cache. Each layer of cache has an expiration time.

The traditional DNS resolution mechanism has several disadvantages:

1.缓存时间设置得长,域名更新不及时,设置得短,大量 DNS 解析请求影响请求速度

2. 域名劫持,容易被中间人攻击,或被运营商劫持, to resolve the domain name to a third-party IP address, according to statistics, the hijacking rate will reach 7%;

3.DNS 解析过程不受控制,无法保证解析到最快的IP

4.一次请求只能解析一个域名

In order to solve these problems, there is one HTTPDNS. The principle is very simple. It is to do the domain name resolution work by yourself, and get the IP address corresponding to the domain name through the HTTP request background, so as to directly solve all the above problems.

insert image description here

The detailed principle of HTTPDNS (HTTP-based Domain Name System) is as follows:

客户端发起请求:
When a client needs to resolve a domain name, it sends one HTTP请求to the HTTPDNS server. The request URLcontains what needs to be parsed 域名信息, generally use the GET or POST method, and specify the relevant parameters in the request header.

DNS解析请求:
HTTPDNS服务器After receiving the client's request, it will operate 域名解析. It first 检查本地的缓存checks to see if there is a resolution result for the domain name. If there is, and the cache has not expired (judged according to the TTL value), the IP address is obtained directly from the cache and returned to the client.

缓存更新:
If it is in the cache of the HTTPDNS server 没有该域名的解析结果,或者缓存已过期, it will initiate a real DNS解析request. This request can be initiated to an authoritative DNS server or other trusted DNS resolution service providers. After the server resolves it 域名对应的IP地址, it will 结果缓存and set an appropriate TTL value.

HTTP响应返回:
The HTTPDNS server sends the resolved data to the client IP地址in the form of an HTTP response . 返回The response usually contains the parsing result, TTL value and other relevant information. The client extracts the IP address by parsing the HTTP response content for subsequent network communication.

客户端更新解析结果:
After the client receives the HTTP response, it will extract it 解析得到的IP地址and perform corresponding processing, such as communicating with the server. The client can also cache the analysis result as needed, and use the cached data in the next request, so as to reduce the dependence on the HTTPDNS server.

The summary of the benefits of HTTPDNS is:

1.Local DNS hijacking: Since HttpDns is passed IP 直接请求 HTTP 获取服务器 A 记录地址, there is no process of asking the local operator for domain resolution, so fundamentally 避免了劫持问题.

2. DNS 解析由自己控制, to ensure that the nearest IP address is returned according to the user's location, or the fastest IP is used according to the client's speed test results;

3. One request is ok 解析多个域名.

connection optimization

The main optimization idea here is to multiplex connections, instead of re-establishing connections every time a request is made, how to multiplex connections more efficiently can be said to be the most important point in network request speed optimization.

【keep-alive】: There is a keep-alive in the HTTP protocol, HTTP1.1默认开启to a certain extent 缓解了每次请求都要进行TCP三次握手建立连接的耗时. The principle is 请求完成后不立即释放连接,而是放入连接池中that if there is another request to be sent at this time, the domain name and port of the request are the same, and the connection in the connection pool is directly used to send and receive data, reducing the time-consuming connection establishment. In fact, both the client and the browser have enabled keep-alive by default. For the same domain name, there will no longer be a connection establishment every time a request is sent, and the pure short connection no longer exists.

However 有 keep-alive 的连接一次只能发送接收一个请求, new requests cannot be accepted until the previous request has been processed. If multiple requests are initiated at the same time, there are two situations:

1. If 串行you send a request, you can 一直复用一个连接, but 速度很慢each request must wait for the previous request to complete before sending.
2. If 并行you send a request, then only 每个请求都要进行tcp三次握手建立新的连接.

For the problem of parallel requests, a new generation protocol HTTP2is proposed 多路复用to solve it. The multiplexing mechanism of HTTP2 is the same 复用连接, but the connection it multiplexes 支持同时处理多条请求, 所有请求都可以并发在这条连接上进行also solves the problem caused by the need to establish multiple connections for concurrent requests mentioned above.

Multiplexing is encapsulated in the connection 传输的数据one by one stream. Each stream has an identifier. The sending and receiving of the stream can be done 乱序. It does not depend on the order, so there will be no blocking problem. The receiving end can use the identifier of the stream To distinguish which request belongs to, and then proceed 数据拼接to get the final data.

Android's open source network library OKhttp默认就会开启 keep-alive, and it also supports HTTP2 in versions above Okhttp3.

data compression

The impact of data on request speed is divided into two aspects, one is 压缩率and the other is 解压序列化反序列化的速度. At present, the two most popular data formats are jsonand protobuf, json is a string, and protobuf is binary. Even after being compressed with various compression algorithms, protobuf will still be smaller than json. Protobuf has advantages in terms of data volume, and protobuf also has some advantages in serialization speed .

link: protobuf

In addition to choosing different serialization methods (data formats), Http can encode the content (that is, the body part), and gzipsuch encoding can be used to achieve the purpose of compression.

in will automatically do it for OKhttpus .BridgeInterceptor开启gzip解压的支持

other

1. Usewebp代替png/jpg

2. 不同网络的不同图片下发, such as (for the original picture is 300x300 picture):

2/3G uses low-resolution pictures: use 100X100 pictures;

If the signal strength of 4G is determined to be strong, use a 300X300 picture, if it is medium, use a 200x200 picture, and if the signal strength is weak, use a 100x100 picture;

WiFi network: directly send 300X300 pictures

3、http开启缓存 / 首页数据加入缓存

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/132028073