java - Spring Boot 应用程序性能缓慢
问题描述
我有一个非常基本的 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 在图中都遵循相同的趋势 - 一起上升或下降?
更新:
- 根据建议将它们全部托管在单独的机器上(基于云)
- 稍微分析一下 - 存储库需要时间(com.sun.proxy.$.methodName)
解决方案
您可以执行以下操作来查找问题:
1- 使用 Gatling 进行性能测试,它使用较少的资源并且不会影响您的结果。
2- 使用 APM 代理准确监控耗时长的进程是数据库查询还是不是(APM)
3- 使用 undertow 代替 tomcat 或其他 web 应用程序
推荐阅读
- html - 此路由不支持 post 方法
- sql - 如何解决嵌套在postgres sql中的聚合函数问题
- javascript - 在更改事件中获取单击的单选按钮 Id
- javafx - 通过另一个控制器更改组合框的值不会触发 JavaFX 中的 onAction 回调
- python - VGGnet 在训练时不学习
- xml - 从一个 XML 标记中获取值并使用 XSLT 放入另一个标记?
- maven - 无法使用 Maven 构建“Hello world”累积性示例
- python - 在多个条件下匹配 2 个 dfs
- php - 我在谷歌验证码控制台中收到此错误
- javascript - 如果使用 ajax 调用联系表单提交,标题(位置:/路径)不起作用