# Tuning

Java 性能调优主要从 [Java 编程](/notes/java/tuning/programming.md)、多线程、JVM、设计模式、数据库等多个方面来介绍。

不过在调优之前先需要了解[性能指标](/notes/java/tuning.md#xing-neng-zhi-biao)、[性能测试](/notes/java/tuning.md#xing-neng-ce-shi)、[常用手段](/notes/java/tuning.md#xing-neng-you-hua)等，见下文。

## 性能指标

### 性能参考因素

* CPU：计算型引用、FullGC、无限循环、多线程大量上下文切换都可能造成 CPU 繁忙。
* 内存：
* 磁盘 I/O：
* 异常：构建异常栈非常消耗资源。
* 数据库：
* 锁竞争：

### 指标

* 响应时间：
  * 数据库响应时间
  * 服务端响应时间
  * 网络响应时间
  * 客户端响应时间
* 吞吐量
  * 磁盘吞吐量
    * IOPS：随机读写，如 OLTP 数据库、小文件存储等。
    * 吞吐量：顺序读写，如视频点播等。
  * 网络吞吐量：涉及 CPU、网卡、宽带等。
* 计算机资源使用率
  * CPU 占用率
  * 内存使用率
  * 磁盘 I/O 使用率
  * 网络 I/O 使用率
* 系统负载

{% hint style="info" %}
系统负载 vs CPU 利用率。系统负载表示正在运行或等待的进程或线程数，CPU 利用率表示单位时间内实时占用 CPU 的百分比。如计算密集型，CPU 利用率会很高，但是系统负载可能为 0.1；I/O 密集型，CPU 利用率可能很低，但是系统负载很高，因为很多线程可能在阻塞。
{% endhint %}

{% hint style="info" %}
TPS vs QPS。TPS（transaction per second），QPS（query per second）。一个事务可能包含多个请求，若一个用户操作只有一个请求，那么 TPS 和 QPS 就没有区别。
{% endhint %}

## 性能测试

* 微基准测试
* 宏基准测试

性能测试注意的问题：

* 热身
* 结果不稳定

## 性能优化

* 代码
* 设计
* 算法
* 时间换空间
* 空间换时间
* 参数调优

## 兜底策略

* 限流
* 熔断
* 自动扩容
* 提前扩容

## 性能测试工具

| 工具         | 适用场景                              | 优点        | 缺点                |
| ---------- | --------------------------------- | --------- | ----------------- |
| ab         | 单个接口的性能测试                         | 简单实用      | 不能对整个业务流程测试       |
| JMeter     | 简单并发测试、整个业务流程测试、组合并发测试、csv 动态导入变量 | 功能全，可扩展性高 | 性能不稳定，高并发容易造成界面卡死 |
| LoadRunner | 包括JMeter 的常用功能，可以 IP 欺骗           | 专业、稳定、高效  | 价格高               |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yunzhao.gitbook.io/notes/java/tuning.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
