- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
![](https://sslprod.oss-cn-shanghai.aliyuncs.com/stable/slides/ShardingSphereJava69221/ShardingSphereJava69221_1440-01.jpg)
从 ShardingSphere 性能提升场景探析 Java 性能工具及实践-吴伟杰
视频回放全集:
展开查看详情
1 .从 ShardingSphere 性能提升 场景探析 Java 性能工具及实践 吴伟杰 July 11, 2022
2 . 图片 吴伟杰 Apache ShardingSphere PMC SphereEx 01 基础设施研发工程师 o 在与 openGauss 社区合作的 16 节点达成 TPC-C 1000 万 tpmC 的性能目 标中,参与 ShardingSphere 性能优化 o 目前专注于 ShardingSphere-Proxy 研发
3 .目录 01 Apache ShardingSphere 历史性能测试结果 02 环境与参数优化带来的性能收益 03 async-profiler 的使用与 ShardingSphere 优化案例 04 借助 JMH 更准确地测量代码性能
4 .场景 1:ShardingSphere-Proxy 透传对比直连 PostgreSQL ShardingSphere- PostgreSQL BenchmarkSQL Proxy 2 * Intel(R) 2 * Intel(R) Xeon(R) 2 * Intel(R) Xeon(R) Xeon(R) CPU E5- CPU CPU E5-2650 v4 @ Gold 6146 CPU @ 2650 v4 @ 2.20GHz 3.20GHz 2.20GHz RAM 96 GB 2400MHz 512 GB 2666MHz 96 GB 2400MHz AVAGO SCSI 硬盘 / / 240GB 网卡 10 Gb 10 Gb 10 Gb 操作系统 CentOS 7.9 CentOS 7.9 CentOS 7.9 Java 17.0.1 BenchmarkSQL 软件环境 ShardingSphere- PostgreSQL 14.2 5.0 Proxy 5.0.0 / 5.1.0 网卡队列绑核 0- fsync=off 1,24-25 full_page_writes=of 其他配置 ShardingSphere- f Proxy 绑核 2-23,26- shared_buffers=128 47 GB
5 .场景 2:共 16 节点 ShardingSphere-JDBC 5.1.1 + openGauss 数据分片 7 x ShardingSphere- 配置 8 x openGauss 1 x ShardingSphere-Proxy JDBC 128 核 128 核 128 核 CPU 2 路鲲鹏 920 2 路鲲鹏 920 2 路鲲鹏 920 内存 768GB 768GB 768GB 系统盘 1TB 1TB 1TB 数据盘 3 x 4TB NVMe SSD 无 无
6 .目录 01 Apache ShardingSphere 历史性能测试结果 02 环境与参数优化带来的性能收益 03 async-profiler 的使用与 ShardingSphere 优化案例 04 借助 JMH 更准确地测量代码性能
7 .ShardingSphere 性能优化思路
8 .案例:云服务器单队列网卡软中断性能瓶颈 解决思路: • (硬件支持)网卡多队列绑核 • irqbalance
9 .brendangregg.com
10 .JVM 升级与参数调优带给 ShardingSphere 的性能收益 * 在 128 核鲲鹏 920 环境运行 ShardingSphere,使用 Java 17 相比 Java 8 性能提升 5% ~ 10%
11 .VM Options Explorer -- chriswhocodes.com
12 .目录 01 Apache ShardingSphere 历史性能测试结果 02 环境与参数优化带来的性能收益 03 async-profiler 的使用与 ShardingSphere 优化案例 04 借助 JMH 更准确地测量代码性能
13 .借助 async-profiler 发现 ShardingSphere 代码优化点 async-profiler 是针对 JVM 的采样分析工具。 特点: ü 不受 Safepoint Bias 影响 ü 性能开销低(采样频率可调整),生产可用 ü 使用方便,可基于 Java Agent 启动,或指定 PID 连接已有 JVM ü 支持多种输出格式(HTML、SVG、JFR 等)及格式转换 支持事件: ü CPU 周期 ü 硬件和软件性能指标,如缓存未命中、分支未命中、缺页、上下文切换等 ü Java 堆中的分配 ü 锁,包括 Java 对象监视器和 ReentrantLock ü ……
14 .JFR vs perf vs async-profiler 来源:https://github.com/apangin/java-profiling-presentation/blob/master/presentation/java-profiling.pdf
15 .async-profiler 采集 CPU 火焰图
16 .async-profiler 采集内存分配火焰图
17 .async-profiler 采集其他事件
18 . 案例:Java 8 ConcurrentHashMap computeIfAbsent 性能问题 https://github.com/apache/shardingsphere/pull/13275 命令示例:使用 async-profiler 采集 JVM 进程中,阻塞超过 100us 的 lock,采样 60 秒并输出到文件 output.jfr ./profiler.sh -d 60 -e cpu --lock 1ms -f output.jfr $JVM_PID
19 .案例:Java 8 ConcurrentHashMap computeIfAbsent 性能问题 多线程使用相同 key 调用 computeIfAbsent
20 .目录 01 Apache ShardingSphere 历史性能测试结果 02 环境与参数优化带来的性能收益 03 async-profiler 的使用与 ShardingSphere 优化案例 04 借助 JMH 更准确地测量代码性能
21 .代码基准测试反面教材 输出结果: 调整 bench 方法顺序后输出结果:
22 .Java Microbenchmark Harness (JMH) https://github.com/openjdk/jmh
23 . 案例:使用 JMH 测试 Java 8 ConcurrentHashMap computeIfAbsent @State(Scope.Benchmark) @Benchmark public class ConcurrentHashMapBenchmark { public Object benchGetBeforeComputeIfAbsent() { Object result = map.get(KEY); private static final String KEY = "key"; return null == result ? map.computeIfAbsent(KEY, __ -> VALUE) : result; } private static final Object VALUE = new Object(); @Benchmark private final Map<String, Object> map = public Object benchComputeIfAbsent() { new ConcurrentHashMap<>(); return map.computeIfAbsent(KEY, __ -> VALUE); } @Setup(Level.Iteration) public void setup() { public static void main(String[] args) { map.clear(); new Runner(new OptionsBuilder() } .addProfiler(AsyncProfiler.class, "event=cpu;lock=100us;dir=/tmp/jmh-jfr;output=jfr") .threads(32) .forks(3) .warmupIterations(3).warmupTime(TimeValue.seconds(5)) .measurementIterations(3).measurementTime(TimeValue.seconds(5)) .build()).run(); }
24 . 案例:使用 JMH 测试 Java 8 ConcurrentHashMap computeIfAbsent GetBeforeComputeIfAbsent JMH 测试结果: ComputeIfAbsent
25 .JMH 使用不当也会测不准代码性能 private double x = Math.PI; 代码来源于 JMHSample_08_DeadCode.java private double compute(double d) { 更多示例请参考 JMH 源码 samples for (int c = 0; c < 10; c++) d = d * d / Math.PI; return d; } @Benchmark public void baseline() { // do nothing, this is a baseline } @Benchmark public void measureWrong() { // This is wrong: result is not used and the entire computation is optimized away. compute(x); } 测试结果: @Benchmark public double measureRight() { // This is correct: the result is being used. return compute(x); }
26 .欢迎关注 ShardingSphere 技术干货 加入交流群 Apache ShardingSphere Website:https://shardingsphere.apache.org Apache ShardingSphere GitHub: https://github.com/apache/shardingsphere Apache ShardingSphere Slack Channel:https://apacheshardingsphere.slack.com
27 .