Daniel teach you how to develop Java performance tuning standards?

7a88b17893cd4329bf805046e4ee080c


Want your program faster and more stable, but the system is often a variety of bug, not start? Java performance tuning Raiders are coming!

I have a friend, he once told me, they never go through the company's system performance tuning, complete functional test on the line after, the line has not been any performance problems Yeah, why do many systems have Performance tuning it?

At that time I will answer him one, if your company does is 12306, do not do it on-line system performance optimization, try what would happen.

If you, how would you answer it? Today, we chatted about this topic, you wanted to be able to find out with these questions: Why do we have to do performance tuning? When did you start doing? Do performance tuning is not a standard can refer to?

Why do performance tuning?

If you do not go through an online product performance testing, then it is like a ×××, you do not know when the problem occurs, you do not know where the limit it can bear. Some performance problems is time to accumulate slowly produced, at a certain time naturally exploded; and more performance problem is caused by fluctuations in traffic caused by, for example, user activity, or the company's products rose; of course there may be a the product line after the half-dead, has not been much traffic, so it did not cause Fengyun ×××.

Now suppose your system to do an event, product manager or boss tells you expect hundreds of thousands of users visited and asked whether the system can withstand the pressure of the event. If you do not know their own situation the performance of the system, only timidly answered the boss, there may be no problem about it.

So, either do performance tuning, this question is actually very good answer. All system after completion of the development, more or less, there will be a performance problem, we first need to do is to find ways to expose problems, such as stress testing, simulation of possible operating scenarios, etc., and then to solve these through performance tuning problem.

For example, when you query a certain piece of information with a App, you need to wait for more than ten seconds; in buying activity, you can not enter the event page, and so on. You see, the system response is most directly reflects the performance of a reference factor.

If the response that the problem does not appear on the online system, we are not do not do performance tuning out? Give you to tell a story.

Once my former club before system development department came a big God, why call him the great God, because he came to the company in the year, he only had one thing, is to reduce the number of servers to half of the original, performance of the system, but also improves.

好的系统性能调优不仅仅可以提高系统的性能,还能为公司节省资源。这也是我们做性能调优的最直接的目的。

什么时候开始介入调优?

解决了为什么要做性能优化的问题,那么新的问题就来了:如果需要对系统做一次全面的性能监测和优化,我们从什么时候开始介入性能调优呢?是不是越早介入越好?

其实,在项目开发的初期,我们没有必要过于在意性能优化,这样反而会让我们疲于性能优化,不仅不会给系统性能带来提升,还会影响到开发进度,甚至获得相反的效果,给系统带来新的问题。

我们只需要在代码层面保证有效的编码,比如,减少磁盘 I/O 操作、降低竞争锁的使用以及使用高效的算法等等。遇到比较复杂的业务,我们可以充分利用设计模式来优化业务代码。例如,设计商品价格的时候,往往会有很多折扣活动、红包活动,我们可以用装饰模式去设计这个业务。

在系统编码完成之后,我们就可以对系统进行性能测试了。这时候,产品经理一般会提供线上预期数据,我们在提供的参考平台上进行压测,通过性能分析、统计工具来统计各项性能指标,看是否在预期范围之内。

在项目成功上线后,我们还需要根据线上的实际情况,依照日志监控以及性能统计日志,来观测系统性能问题,一旦发现问题,就要对日志进行分析并及时修复问题。

有哪些参考因素可以体现系统的性能?

上面我们讲到了在项目研发的各个阶段性能调优是如何介入的,其中多次讲到了性能指标,那么性能指标到底有哪些呢?

在我们了解性能指标之前,我们先来了解下哪些计算机资源会成为系统的性能瓶颈。

CPU:有的应用需要大量计算,他们会长时间、不间断地占用 CPU 资源,导致其他资源无法争夺到 CPU 而响应缓慢,从而带来系统性能问题。例如,代码递归导致的无限循环,正则表达式引起的回溯,JVM 频繁的 FULL GC,以及多线程编程造成的大量上下文切换等,这些都有可能导致 CPU 资源繁忙。

内存:Java 程序一般通过 JVM 对内存进行分配管理,主要是用 JVM 中的堆内存来存储 Java 创建的对象。系统堆内存的读写速度非常快,所以基本不存在读写性能瓶颈。但是由于内存成本要比磁盘高,相比磁盘,内存的存储空间又非常有限。所以当内存空间被占满,对象无法回收时,就会导致内存溢出、内存泄露等问题。

磁盘 I/O:磁盘相比内存来说,存储空间要大很多,但磁盘 I/O 读写的速度要比内存慢,虽然目前引入的 SSD 固态硬盘已经有所优化,但仍然无法与内存的读写速度相提并论。网络:网络对于系统性能来说,也起着至关重要的作用。如果你购买过云服务,一定经历过,选择网络带宽大小这一环节。带宽过低的话,对于传输数据比较大,或者是并发量比较大的系统,网络就很容易成为性能瓶颈。

异常:Java 应用中,抛出异常需要构建异常栈,对异常进行捕获和处理,这个过程非常消耗系统性能。如果在高并发的情况下引发异常,持续地进行异常处理,那么系统的性能就会明显地受到影响。

数据库:大部分系统都会用到数据库,而数据库的操作往往是涉及到磁盘 I/O 的读写。大量的数据库读写操作,会导致磁盘 I/O 性能瓶颈,进而导致数据库操作的延迟性。对于有大量数据库读写操作的系统来说,数据库的性能优化是整个系统的核心。

锁竞争:在并发编程中,我们经常会需要多个线程,共享读写操作同一个资源,这个时候为了保持数据的原子性(即保证这个共享资源在一个线程写的时候,不被另一个线程修改),我们就会用到锁。锁的使用可能会带来上下文切换,从而给系统带来性能开销。JDK1.6 之后,Java 为了降低锁竞争带来的上下文切换,对 JVM 内部锁已经做了多次优化,例如,新增了偏向锁、自旋锁、轻量级锁、锁粗化、锁消除等。而如何合理地使用锁资源,优化锁资源,就需要你了解更多的操作系统知识、Java 多线程编程基础,积累项目经验,并结合实际场景去处理相关问题。

了解了上面这些基本内容,我们可以得到下面几个指标,来衡量一般系统的性能。

1、响应时间

响应时间是衡量系统性能的重要指标之一,响应时间越短,性能越好,一般一个接口的响应时间是在毫秒级。在系统中,我们可以把响应时间自下而上细分为以下几种:

526550fcbefd4edcb62d4b7f719ce6b9



数据库响应时间:数据库操作所消耗的时间,往往是整个请求链中最耗时的;

服务端响应时间:服务端包括 Nginx 分发的请求所消耗的时间以及服务端程序执行所消耗的时间;

网络响应时间:这是网络传输时,网络硬件需要对传输的请求进行解析等操作所消耗的时间;

客户端响应时间:对于普通的 Web、App 客户端来说,消耗时间是可以忽略不计的,但如果你的客户端嵌入了大量的逻辑处理,消耗的时间就有可能变长,从而成为系统的瓶颈。

2、吞吐量

在测试中,我们往往会比较注重系统接口的 TPS(每秒事务处理量),因为 TPS 体现了接口的性能,TPS 越大,性能越好。在系统中,我们也可以把吞吐量自下而上地分为两种:磁盘吞吐量和网络吞吐量。我们先来看磁盘吞吐量,磁盘性能有两个关键衡量指标。

接下来看网络吞吐量,这个是指网络传输时没有帧丢失的情况下,设备能够接受的最大数据速率。网络吞吐量不仅仅跟带宽有关系,还跟 CPU 的处理能力、网卡、防火墙、外部接口以及 I/O 等等紧密关联。而吞吐量的大小主要由网卡的处理能力、内部程序算法以及带宽大小决定。

3、计算机资源分配使用率

通常由 CPU 占用率、内存使用率、磁盘 I/O、网络 I/O 来表示资源使用率。这几个参数好比一个木桶,如果其中任何一块木板出现短板,任何一项分配不合理,对整个系统性能的影响都是毁灭性的。

4、负载承受能力

当系统压力上升时,你可以观察,系统响应时间的上升曲线是否平缓。这项指标能直观地反馈给你,系统所能承受的负载压力极限。例如,当你对系统进行压测时,系统的响应时间会随着系统并发数的增加而延长,直到系统无法处理这么多请求,抛出大量错误时,就到了极限。

总结

通过今天的学习,我们知道性能调优可以使系统稳定,用户体验更佳,甚至在比较大的系统中,还能帮公司节约资源。

But in the beginning of the project, we do not need to intervene prematurely optimize performance, just in time to ensure its outstanding coding, efficient, and good programming.

Upon completion of the project, we can test the system, we can use the following performance indicators as a standard performance tuning, response time, throughput, resource allocation computer usage, load-bearing capacity.

Looking back at my own experience of the project, there is electricity supplier systems, payment systems and billing system to recharge the game, the user level is the million level, and to bear all kinds of large-scale buying activities, the performance of the system so I'm very demanding. In addition to determining the performance of the system by observing the above indicators is good or bad, also need to update iteration, to fully protect the stability of the system.

Here, you extend a method, that is, before the iterative version of the system performance indicators as a reference standard by automating performance testing, system performance after checking whether there is an abnormal version of the iterative development, where not just compare the throughput, response time , such as a direct indicator of the load capacity, but also CPU usage of system resources, changes in several indirect indicators of memory usage, disk I / O, network I / O and so on.


Guess you like

Origin blog.51cto.com/13689432/2400717