When making technical support for customers, we often see many customers' abnormalities at all. After eliminating these abnormalities, the code running speed has been greatly improved compared to before. This allows us to have a guess that using abnormalities in the code will bring significant performance overhead. Because abnormalities are an important part of the wrong situation, it is impossible to abandon, so we need to measure the impact of abnormal treatment on performance. We can see the effect of abnormal treatment on performance through an experiment.
experiment
My experiment is based on a simple simple code that randomly throws. From a scientific perspective, this is not a completely accurate measurement, and at the same time, I don't know how to do the operating code in the running code. But in any case, this code should allow us to understand some basic situations.
The result is very interesting: the cost of throwing and capture abnormalities seems to be extremely low. In my example, it is about 0.02 milliseconds. Unless you really throw too many abnormalities (we mean 100,000 or more), this is basically ignored. Although these results show that abnormal processing itself does not affect code performance, it has not solved the following problem: Who is responsible for the huge impact on performance?
I obviously missed any important issues.
After thinking about it again, I realized that I missed an important part of abnormal treatment. I didn't consider what you did when abnormalities happened. In most cases you are likely not just to capture abnormalities! The problem is here: Generally, you will try to supplement the problem and let the application still play with the end user. So what I missed is: "" "" supplementary code "" to handle abnormalities. Depending on the different code, performance loss may become quite significant. In some cases, this may mean focusing on the connection to the server, and in other cases, it may mean using the default rollback solution, and the solution provided by this solution will definitely bring very bad performance. This seems to give a good explanation for the behavior we see in many cases.
However, I don't think that the analysis is everything here, but I feel that something else has been missed here.
Stack trace
I am still curious about this issue, and for this reason, I monitored how the performance changes when collecting the Strack TraCE.
This is the case that happens often: to write down the abnormal and its stack trajectory, try to find out where the problem is.
For this reason, I modified the code and collected the abnormal Strack Trace. This has changed the situation significantly. The performance effect on the collection of abnormal Strack Trace is 10 times higher than simply capturing and throwing abnormality. Therefore, although Strack Trace helps understand where problems occur (it may help understand why problems occur), there are performance losses. Since we are not talking about a Strack Trace, the impact here is often very great. In most cases, we must throw and capture abnormalities at multiple levels. Let's look at a simple example: the web service client is connected to the server. First of all, there is an abnormality in the Java library level. Since then, there will be abnormal clients at the framework level, and there may be abnormal business logic calls at the application level in the future. Until now, a total of three Strack Traces must be collected. In most cases, you can see these strack trace from log files or application output, and writing these long Strack Trace often also also also have performance effects.
in conclusion
First of all, it is not good to discard abnormal abnormalities because of the impact of performance. Unusual helps to provide a consistent way to solve the operating problems and help write clean code. But we should track the abnormal quantity thrown in the code, which may cause significant performance effects. Therefore, Oneapm has to track the abnormalities thrown by default -in many cases, people will be surprised by the abnormalities in the code and the performance loss when solving these abnormalities. Secondly, although it is very beneficial to use, you should also avoid capturing too much Strack Trace. Abnormalities should be designed for abnormal conditions, and this principle should be kept in mind when using. Of course, in case you don't want to follow good programming habits, the Java language will let you know that doing so can make your program run faster and encourage you to do that.
The above is all the contents of this article. I hope everyone can like it.