【问题标题】:Spring boot application slow performanceSpring Boot 应用程序性能缓慢
【发布时间】:2021-04-23 04:31:21
【问题描述】:

我有一个非常基本的 Spring 启动应用程序,它为一个非常简单的实体公开了 CRUD Rest API。使用 JMeter 运行性能测试显示响应时间很差

Product (id, name, description)

约束: id PK AUTOINCREMENT

设置:

  • 线程数:100
  • 加速期:10(秒)
  • 无限循环
  • 持续时间:60(秒)

4 个 API 用于执行以下功能

@Override
public Product findById(Integer id) {
    return repository.findById(id)
            .map(mapper::map)
            .orElseThrow(() -> new RecordNotFoundException(1, "Product not found"));
}

@Transactional
@Override
public Product create(Product Product) {
    ProductEntity entity = mapper.map(Product);
    
    repository.save(entity);

    return mapper.map(entity);
}

@Transactional
@Override
public Product update(Integer id, Product Product) {
    ProductEntity entity = repository.findById(id).orElseThrow();

    mapper.mapTo(entity, Product);

    repository.save(entity);

    return mapper.map(entity);
}

@Transactional
@Override
public void delete(Integer id) {    
    repository.deleteById(id);
}

我试图通过可视化虚拟机了解原因。似乎存储库功能花费了太多时间。我假设它是导致问题的数据库。

连接池设置:因为我没有设置任何东西,假设我的应用程序使用 HikariCP,默认大小为 100。

数据库:以上结果尝试使用 MySQL(innodb 引擎)并禁用自动提交。

Spring Boot 版本: 2.4.0

系统: 16GB RAM,i7 内核

问题:

连接池或数据库是否是这里的瓶颈?池大小的最佳值是多少以减少响应时间?

为什么所有 API 在图表中都遵循相同的趋势 - 一起上升或下降?

更新:

  1. 根据建议将它们全部托管在单独的机器上(基于云)
  2. 简要介绍一下 - 存储库需要时间 (com.sun.proxy.$.methodName)

【问题讨论】:

  • JMeter 测试、Spring Boot 和 DB 是否在同一台服务器上运行(开发人员设置)?您使用的是 SSD 还是 HDD?在这种情况下,所有线程都在相互竞争 CPU 执行时间。每个应用程序都应该在不同的机器上执行,例如在生产中以获得可靠的测量结果。
  • 这里有一些阅读,希望对您有所帮助:medium.com/kenshoos-engineering-blog/…

标签: java spring-boot jmeter hikaricp


【解决方案1】:

我可以想到以下可能的原因:

  1. 运行 Spring Boot 应用程序的机器缺少资源(CPU、RAM 等) - 确保使用 JMeter PerfMon Plugin 监控它们
  2. 使用像 YourKitJProfiler 这样的分析工具来查看应用程序在哪里花费的时间最多
  3. 如果您认为问题出在数据库中:

【讨论】:

  • 肯定它在存储库和数据库之间的某个地方(com.sun.proxy.$(Repository)方法一直在占用。我想也许调整连接池可能会有所帮助
【解决方案2】:

你可以通过这些来找到问题:

1- 使用 Gatling 进行性能测试,它使用较少的资源并且不会影响您的结果。

2- 使用 APM 代理来准确监控耗时长的进程是数据库查询还是不是 (APM)

3- 使用 undertow 代替 tomcat 或其他网络应用程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2017-08-06
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    相关资源
    最近更新 更多