首页 > 解决方案 > Spring Boot 应用程序性能缓慢

问题描述

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

Product (id, name, description)

约束: id PK AUTOINCREMENT

设置:

响应图 总结报告

公开 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)

标签: javaspring-bootjmeterhikaricp

解决方案


您可以执行以下操作来查找问题:

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

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

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


推荐阅读