Coder Social home page Coder Social logo

wuqiu-ai / qps-helper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wujiuye/qps-helper

0.0 0.0 0.0 31 KB

通用的qps统计工具包,为项目中一些框架实现QPS限流提供支持,为项目中统计接口的QPS提供支持。此项目不维护,如果不满足需求,推荐使用guava。

License: Apache License 2.0

Java 100.00%

qps-helper's Introduction

qps-helper

通用的qps、tps统计工具包

使用qps-helper统计接口的QPS

@RestController
@RequestMapping("/api/v1")
@Slf4j
public class DemoController {
    
    private FlowHelper flowHelper = new FlowHelper(FlowType.HOUR);

    @GetMapping("/test")
    public ApiResponse testApi() {
        try{
            long startTime = TimeUtil.currentTimeMillis();
            // 业务逻辑
            //    ......
            // 计算耗时
            long rt = TimeUtil.currentTimeMillis() - startTime;
            flowHelper.incrSuccess(rt);
            return ApiResponse.ok();
        }catch (Exception e){
            flowHelper.incrException();
            return ApiResponse.error();
        }
    }

}

统计每秒钟

public class Main{
    private FlowHelper flowHelper = new FlowHelper(FlowType.Second);
}

输出最近一秒钟统计

public class Main{
    public static void print(){
        Flower flower = flowHelper.getFlow(FlowType.Second);
        System.out.println("总请求数:"+flower.total());
        System.out.println("成功请求数:"+flower.totalSuccess());
        System.out.println("异常请求数:"+flower.totalException());
        System.out.println("平均请求耗时:"+flower.avgRt());
        System.out.println("最大请求耗时:"+flower.maxRt());
        System.out.println("最小请求耗时:"+flower.minRt());
        System.out.println("平均请求成功数(每毫秒):"+flower.successAvg());
        System.out.println("平均请求异常数(每毫秒):"+flower.exceptionAvg());
        System.out.println();
    }
}

统计每分钟

public class Main{
    private FlowHelper flowHelper = new FlowHelper(FlowType.Minute);
}

输出最近一分钟统计

public class Main{
    public static void print(){
        Flower flower = flowHelper.getFlow(FlowType.Minute);
        System.out.println("总请求数:"+flower.total());
        System.out.println("成功请求数:"+flower.totalSuccess());
        System.out.println("异常请求数:"+flower.totalException());
        System.out.println("平均请求耗时:"+flower.avgRt());
        System.out.println("最大请求耗时:"+flower.maxRt());
        System.out.println("最小请求耗时:"+flower.minRt());
        System.out.println("平均请求成功数(每秒钟):"+flower.successAvg());
        System.out.println("平均请求异常数(每秒钟):"+flower.exceptionAvg());
        System.out.println();
    }
}

统计每小时

public class Main{
    private FlowHelper flowHelper = new FlowHelper(FlowType.HOUR);
}

输出最近一小时统计

public class Main{
    public static void print(){
        Flower flower = flowHelper.getFlow(FlowType.HOUR);
        System.out.println("总请求数:"+flower.total());
        System.out.println("成功请求数:"+flower.totalSuccess());
        System.out.println("异常请求数:"+flower.totalException());
        System.out.println("平均请求耗时:"+flower.avgRt());
        System.out.println("最大请求耗时:"+flower.maxRt());
        System.out.println("最小请求耗时:"+flower.minRt());
        System.out.println("平均请求成功数(每分钟):"+flower.successAvg());
        System.out.println("平均请求异常数(每分钟):"+flower.exceptionAvg());
        System.out.println();
    }
}

组合统计

public class Main{
    // 可任意组合
    private FlowHelper flowHelper = new FlowHelper(FlowType.HOUR,FlowType.Minute,FlowType.Second);
}

输出最近一小时、一分钟、一毫秒的统计

public class Main{
    public static void print(){
        // 获取每秒钟统计
        Flower secondFlower = flowHelper.getFlow(FlowType.Second);
        // 获取每分钟统计
        Flower minuteFlower = flowHelper.getFlow(FlowType.Minute);
        // 获取每小时统计
        Flower hourFlower = flowHelper.getFlow(FlowType.HOUR);
    }
}

获取详情

以统计每分钟数据为例

public class Main{
    private FlowHelper flowHelper = new FlowHelper(FlowType.Minute);
    
    public static void print(){
       // 获取每分钟统计
       Flower flower = flowHelper.getFlow(FlowType.Minute);
       List<WindowWrap<MetricBucket>> buckets = flower.windows();
       for (WindowWrap<MetricBucket> bucket : buckets) {
           System.out.print("开始时间戳:" + bucket.windowStart() + "\t");
           System.out.print("成功数:" + bucket.value().success() + "\t");
           System.out.print("失败数:" + bucket.value().exception() + "\t");
           System.out.print("平均耗时:" + (bucket.value().rt() / bucket.value().success()) + "\t");
           System.out.print("最大数:" + bucket.value().maxRt() + "\t");
           System.out.print("最小数:" + bucket.value().minRt() + "\t");
           System.out.println();
       }
       System.out.println();
    }
}

A&Q

  • 1、平均请求数的统计有问题? 平均请求数的计算:如果统计一分钟的qps,那么平均就是当前成功请求数/60秒。
    所以要准确的话,前一分钟是准确的,当前分钟是不准确的。

qps-helper's People

Contributors

wujiuye avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.