当前位置:网站首页>QPS的概念和实现
QPS的概念和实现
2022-06-26 03:25:00 【thoughtCodes】
Survive by day and develop by night.
talk is cheap, show me the code,make a better result.
目录
概述
QPS是一种非常常见的需求。
需求:
1.QPS的实现
package qps.demo;
/** * @desc QPS控制器测试 * @author visy.wang * @date 2020/9/29 10:25 */
public class Main {
/** * 在多线程环境下,请确保拿到的QPSCtrl对象是同一个, * 建议在此处初始化,当然也可用别的方式创建(但需保证是同一个) * 不同的QPSCtrl对象是彼此独立的 */
private static QPSCtrl qpsCtrl1 = QPSCtrl.create(1);
// private static QPSCtrl qpsCtrl2 = QPSCtrl.create(3);
public void test1(int i){
//调用ctrl()来实现QPS的控制
// System.out.println(Thread.currentThread().getName()+"\t"+System.currentTimeMillis()+"开始访问\n");
// System.out.println(Thread.currentThread().getName());
qpsCtrl1.ctrl();
System.out.println(Thread.currentThread().getName()+"\t"+i+"(I) ");
// System.out.println(System.currentTimeMillis()+"END");
}
// public void test2(int i){
// //超出QPS值会阻塞在这里,直到下一个周期
// qpsCtrl2.ctrl();
// System.out.print(i+"(II) ");
// }
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
// System.out.println("开始结束"+startTime);
Main testQPS = new Main();
new Thread(()->{
for(int i=1; i<=3; i++){
testQPS.test1(i);
}
}).start();
new Thread(()->{
for(int i=4; i<=6; i++){
testQPS.test1(i);
}
}).start();
long endTime=0L;
new Thread(()->{
for(int i=7; i<=12; i++){
testQPS.test1(i);
}
}).start();
endTime = System.currentTimeMillis();
// System.out.println("开始结束"+endTime);
// new Thread(()->{
// for(int i=13; i<=18; i++){
// testQPS.test2(i);
// }
// }).start();
System.out.println("耗时"+Long.valueOf(endTime-startTime));
}
}
测试log:
耗时90
1656172702613=====================
Thread-2 7(I)
1656172703613=====================
Thread-2 8(I)
1656172704649=====================
Thread-0 1(I)
1656172705656=====================
Thread-0 2(I)
1656172706656=====================
Thread-1 4(I)
1656172707656=====================
Thread-1 5(I)
1656172708656=====================
Thread-1 6(I)
1656172709656=====================
Thread-0 3(I)
1656172710656=====================
Thread-2 9(I)
1656172711656=====================
Thread-2 10(I)
1656172712656=====================
Thread-2 11(I)
1656172713656=====================
Thread-2 12(I)
我疑惑的地方是为什么最后输出耗时呢?
设计思路
无
实现思路分析
1.QPS实现
拓展实现
这里参考:github:简单实现上述流程:
入门级实现:
: 部分源码实现.
: 源码实现
性能参数测试:
无
参考资料和推荐阅读
欢迎阅读,各位老铁,如果对你有帮助,点个赞加个关注呗!~
边栏推荐
- Uni app custom selection date 1 (September 16, 2021)
- 2020 summary: industrial software development under Internet thinking
- progress bar
- USB driver -debug
- Qixia fire department carries out fire safety training on construction site
- HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
- 虫子 构造与析构
- Tupu software is the digital twin of offshore wind power, striving to be the first
- Various errors in kitti2bag installation
- “再谈”协议
猜你喜欢

小米电视的网页和珠宝的网页

显卡、GPU、CPU、CUDA、显存、RTX/GTX及查看方式

Add an "open search description" to the site to adapt to the browser's "site search"“

Todolist incomplete, completed

Uni app QR code scanning and identification function

Cloud Computing Foundation -0

MySQL开发环境

MySQL高级篇第一章(linux下安装MySQL)【下】

云计算基础-0

Group counting notes - instruction pipeline of CPU
随机推荐
MySQL advanced part (IV: locking mechanism and SQL optimization)
Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
kotlin快速上手
Classic model - Nin & googlenet
【哈希表】很简单的拉链法哈希结构,以至于效果太差,冲突太多,链表太长
解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题
Where is it safe to open a fund account?
mysql 常用语句
Do you want to add a key to the applet or for sequence?
You cannot call Glide. get() in registerComponents(), use the provided Glide instance instead
MySQL高级部分( 四: 锁机制、SQL优化 )
Asynctask multiple simultaneous use methods
栖霞消防开展在建工地消防安全培训
上传文件/文本/图片,盒子阴影
WebRTC系列-网络传输之7-ICE补充之偏好(preference)与优先级(priority)
2022.6.20-----leetcode. seven hundred and fifteen
Kotlin learning apply plugin: 'kotlin Android extensions‘
Classic model alexnet
Tupu software is the digital twin of offshore wind power, striving to be the first
MySQL的视图