压测和提高QPS
压测和提高QPS
1.对项目的所有接口响应时间进行监控(使用拦截器,依赖swagger3增强版),将接口的响应时间统一控制在500毫秒内完成
(1)swagger3增强版
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency> ** * @author Yubo Huang * @date Created in 2024/11/10 10:05 上午 * @description 文档配置 */ @Configuration public class Knife4jConfig { @Bean public OpenAPI springShopOpenApi() { return new OpenAPI() // 接口文档标题 .info( new Info().title("接口文档") // 接口文档简介 .description("Knife4j OpenApi3的接口文档") // 接口文档版本 .version("v1.0") // 开发者联系方式 .contact(new Contact().name("").email("")) ); } }
(2)拦截器
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import java.lang.reflect.Method; /** * @author Yubo Huang * @date Created in 2024/11/10 9:17 上午 * @description 响应时间 */ @Slf4j @Component public class ResTimeInterceptor implements HandlerInterceptor { private static final Long MAX_RES_TIME = 500L; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { long startTime = System.currentTimeMillis(); request.setAttribute("startTime", startTime); return true; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { long startTime = (Long) request.getAttribute("startTime"); long endTime = System.currentTimeMillis(); long resTime = endTime - startTime; if (resTime < MAX_RES_TIME) { return; } if (handler instanceof HandlerMethod handlerMethod) { //controller Class<?> beanType = handlerMethod.getBeanType(); //api Method method = handlerMethod.getMethod(); //info Tag tag = beanType.getAnnotation(Tag.class); String name = tag.name(); Operation operation = method.getAnnotation(Operation.class); String summary = operation.summary(); //log String queryString = request.getQueryString(); log.warn("{} -> {} -> {} -> resTime:{} -> param:{}", name, summary, request.getRequestURI(), resTime, queryString); } } }
(3).添加拦截器
import com.yb.interceptor.ResTimeInterceptor; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @author Yubo Huang * @date Created in 2024/11/10 10:19 上午 * @description web配置 */ @Configuration @RequiredArgsConstructor public class WebConfig implements WebMvcConfigurer { private final ResTimeInterceptor resTimeInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(resTimeInterceptor); } }
2.独立的服务器或者虚拟机下进行(限制cpu和内存)
3.新写一个接口,接口中使用sleep休眠300毫秒(对该接口进行压测)
4.单个节点的项目调整(最大线程数和最大等待数),通过jmeter压测到最大qps
5.这就是服务器下该节点的最大QPS,根据需求增加服务器和项目节点就可以了
6.当多个节点的QPS到达了需求,接下来还要考虑nginx的QPS和mysql的TPS,共同来支撑需求对应的QPS
网络的上行和下行是否影响QPS