Spring boot 应用程序性能缓慢

问题描述 投票:0回答:2

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

Product (id, name, description)

约束: id PK 自动增量

设置:

  • 线程数: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)
java spring-boot jmeter hikaricp
2个回答
0
投票

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

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

0
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.