【必会面试题】什么是QPS?如何统计QPS?

什么是QPS

QPS(Queries Per Second),即每秒查询率,是对一个特定的服务或系统在单位时间内能够处理的查询请求的数量度量。qps是评估系统处理能力的一个重要指标,广泛应用于数据库、Web服务器、API接口等性能测试和监控中。

如何统计QPS

QPS的统计方法多种多样,取决于具体的环境和技术栈,但基本思路是计算单位时间内完成的请求数量。

  1. 手动记录与计算:在代码中埋点,记录每个请求的开始和结束时间,然后根据时间戳计算单位时间内的请求数量。这种方法简单但不够精确,且在高并发场景下可能会引入额外的性能开销。

  2. 使用工具:很多性能测试工具(如Apache JMeter、LoadRunner、wrk等)和监控工具(如Prometheus、Grafana、New Relic等)可以直接提供QPS的统计功能。这些工具会自动发送请求并记录响应时间,计算出QPS。

  3. 日志分析:通过分析服务器或应用的日志,提取出请求开始和结束的信息,然后汇总计算。一些日志分析平台(如ELK Stack、Splunk)支持自动或半自动完成这项工作。

  4. 系统自带统计:许多数据库(如MySQL)和应用服务器(如Nginx、Apache HTTP Server)提供了内置的性能统计功能,可以直接查看或通过API查询到QPS信息。

  5. 代码层面统计:在应用代码中利用计数器和时间戳,每秒重置计数器并记录过去一秒的请求数,或者使用滑动窗口算法来平滑统计QPS。

  • 统计公式

Q P S = 总请求数 时间间隔 \color{blue}QPS = \cfrac{总请求数}{时间间隔} QPS=时间间隔总请求数

例如,如果在1分钟内,一个服务处理了3600个请求,那么它的QPS为:

Q P S = 3600 60 = 60 \color{blue}QPS = \cfrac{3600}{60} = 60 QPS=603600=60

就是说服务每秒可以处理60次请求。

滑动窗口QPS统计器示例

  • 滑动窗口:通过维护一个队列requestTimestampQueue来代表当前窗口内的请求,每当有新请求到达时,将其时间戳加入队列,并移除窗口外的旧请求。
  • 平滑统计:在printSmoothedQPS方法中,每秒检查一次是否需要更新并打印QPS,通过计算窗口期内的请求数量除以窗口大小(秒),得到平滑的QPS值。
  • 同步控制:为保证线程安全,使用synchronized关键字对共享数据访问进行同步控制。
import java.util.LinkedList;
import java.util.Queue;

public class SlidingWindowQPSCalculator {

    private static final int WINDOW_SIZE_SECONDS = 5; // 窗口大小,例如5秒
    private Queue<Long> requestTimestampQueue = new LinkedList<>(); // 用于存放请求到达的时间戳
    private long lastPrintTime = System.currentTimeMillis(); // 上次打印QPS的时间
    private int requestCountInWindow = 0; // 窗口期内的请求数量

    public void addRequest() {
        synchronized (requestTimestampQueue) {
            long currentTime = System.currentTimeMillis();
            requestTimestampQueue.offer(currentTime);
            requestCountInWindow++; // 请求计数增加
            // 清理超出窗口期的旧请求
            while (!requestTimestampQueue.isEmpty() && currentTime - requestTimestampQueue.peek() > WINDOW_SIZE_SECONDS * 1000L) {
                requestTimestampQueue.poll();
                requestCountInWindow--; // 对应的请求数量减少
            }
        }
    }

    public void printSmoothedQPS() {
        while (true) {
            synchronized (requestTimestampQueue) {
                long currentTime = System.currentTimeMillis();
                // 每秒检查一次是否需要打印QPS
                if (currentTime - lastPrintTime >= 1000) {
                    double qps = (double) requestCountInWindow / WINDOW_SIZE_SECONDS;
                    System.out.printf("Smoothed QPS over the last %d seconds: %.2f%n", WINDOW_SIZE_SECONDS, qps);
                    lastPrintTime = currentTime;
                }
            }
            try {
                Thread.sleep(1000); // 每秒检查一次
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SlidingWindowQPSCalculator calculator = new SlidingWindowQPSCalculator();
        // 模拟请求到来
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(() -> {
            for (int i = 0; i < 60; i++) { // 模拟一分钟内的请求
                calculator.addRequest();
                try {
                    Thread.sleep(100); // 模拟请求间隔
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });

        // 启动QPS打印线程
        Thread printThread = new Thread(calculator::printSmoothedQPS);
        printThread.start();

        // 等待模拟请求线程结束
        executor.shutdown();
        printThread.join();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值