Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize performance

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to do performance optimization", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to do performance optimization"!

Analysis of problems

Two questions are asked in the title, one is whether performance optimization has been done, and the other is from what aspects to do performance optimization. For the first question, we can directly answer yes or no. I believe that friends want to do their own performance optimization work, if the answer has been done, then the second question, how should we answer it?

First of all, for the second question, the question is what will be done. There is no fixed answer to this question. You can answer this question according to your past experience, but before you answer this question, you need to comb through your thoughts and make what you want to express clearly.

Next, let's talk about the second question: what will you do to optimize performance?

We can answer from several aspects of performance optimization. Next, let's talk about how performance optimization can be done.

What are the aspects of performance optimization?

Here, combined with the summary of my usual work, I summarize the performance optimization into the following figure.

In other words, we can answer from the aspects of data aggregation optimization, resource conflict optimization, algorithm optimization, JVM optimization, reuse optimization, computing optimization and fast implementation. Next, we will explain each point.

Data aggregation optimization

Data aggregation optimization is mainly aimed at the optimization of data integration and transmission. For example, the data we query from the database is aggregated by the program and then returned to the client, instead of the client calling multiple interfaces to get the data respectively.

Another example: we use Nginx in the project, generally will turn on GZIP compression, so that the transmission of data is more compact, at the same time, so that the amount of data transmission is smaller.

Careful friends will find that our optimization of data aggregation is mainly to make the amount of data transmitted smaller. Therefore, when we use SQL statements to query the data in the database, we try to query those fields that are needed, and ignore the fields that are not needed to avoid select * in the SQL statement.

Resource conflict optimization

In our usual work, especially in high concurrency scenarios, lock conflicts often occur, and lock conflicts are a typical scenario of resource conflicts.

With regard to locks, we can think of row locks, table locks, synchronized and Lock in Java. If it corresponds to the operating system level, there will be CPU command-level locks, JVM instruction-level locks, operating system internal locks, and so on.

Here, friends need to pay attention to one thing: only in concurrent scenarios, the problem of resource conflict will occur. In other words, only one request can get the requested resource at the same time, and the way to resolve the conflict is to lock it.

Algorithm optimization

In a large-scale Internet project, distributed and micro-service technologies are often involved, in which a large number of data structures and algorithms are also used, and the optimization of algorithms can significantly improve the performance of the system. A good implementation, compared with a poor implementation, there is a huge difference in the improvement of system performance.

For example, as an implementation of List, LinkedList and ArrayList differ by several orders of magnitude in random access performance; for example, CopyOnWriteList uses write-time replication, which can significantly reduce lock conflicts in scenarios with more reads and less writes. And when to use synchronization, when is thread-safe, but also has higher requirements for our coding ability.

Therefore, we need to accumulate a lot of knowledge about data structures and algorithms in the usual working process.

JVM optimization

JVM tuning, needless to say, is a standard skill that every Java engineer must master. All Java programs are ultimately run in JVM, and optimization of JVM can also improve the performance of Java programs. However, it should be noted that if the parameters are not set properly when optimizing JVM, it may cause serious problems such as memory overflow.

At present, the widely used garbage collector is G1, and memory can be reclaimed efficiently through few parameter configuration. The CMS garbage collector has been removed in Java 14 and should be avoided if possible because its GC time is uncontrollable.

Reuse optimization

Reuse optimization, this can be known by the name, to put it bluntly, it can be reused. It is estimated that many friends have the experience that when writing code, a lot of repetitive code can be abstracted into a common method. In this way, you don't have to write repetitive logic code every time. This is code-level reuse.

If it is at the data level, we can use buffering and caching to reuse data.

Here, friends need to pay attention to a knowledge point: buffering is mainly for write operations, caching is mainly for read operations.

Another typical scenario of reuse optimization is pooling technology, such as database connection pool, thread pool, and so on.

Calculation and optimization

For computational optimization, we can explain it from the following small aspects.

Parallel computing

It is not difficult to understand that multiple calculations are carried out at the same time. Here, parallel computing can be divided into multi-computer parallel computing, multi-process parallel computing and multi-thread parallel computing.

Multi-computer parallel computing: a large computing task is divided into N small computing tasks and distributed to different machines for processing. A typical scenario is Hadoop's MapReduce extremes.

Multi-process computing: for example, the NIO model adopted by Nginx adopts the strategy of process scheduling. Master processes schedule Worker processes, and Worker handles specific requests.

Multithreaded computing: for multithreaded computing, it is also a computing method that we usually have the most contact with. We can use multithreading technology to split complex logical computing into small computing tasks and distribute them to different threads for execution.

Synchronous mutation step

The difference between synchronous and asynchronous is that synchronous needs to wait for the return result, while asynchronous does not need to wait for the return result. If we do not need to wait for the result data to be returned in the business program, we can optimize synchronous calls to asynchronous calls, thus improving the performance of our system.

Lazy loading

The most typical scenario is lazy loading in Spring, where a bean instance is created only when you get bean for the first time.

Fast implementation

For rapid implementation, it not only includes that we need to use the relevant program framework to quickly develop the business we want, but also requires us to use some high-performance components as far as possible in technology selection. For example, in network development, try to choose Netty, combined with lightweight data transmission, do not use technologies such as WebService.

Many companies like to use the adapter pattern to abstract their own components on top of some existing open source components, so that they can switch between the underlying components without feeling the upper application.

At this point, I believe you have a deeper understanding of "how to do performance optimization", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report