win CreateTimerQueue CreateTimerQueueTimer DeleteTimerQueueTimer 使用测试 百万定时器压测
定时器数量最大一百万 超过一个都不行
// TestTimerQueue.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <Windows.h> //定时器相关//全军定时器 HANDLE hTimerQueue; //全局的定时器队列 CRITICAL_SECTION sectionTimerQueue;//边界条件 void InitTimerQueue() { hTimerQueue = CreateTimerQueue(); if (NULL == hTimerQueue) { printf("CreateTimerQueue failed (%d)\n", GetLastError()); } InitializeCriticalSection(§ionTimerQueue); } void ReleaseTimerQueue() { DeleteCriticalSection(§ionTimerQueue); if (hTimerQueue != NULL && hTimerQueue != INVALID_HANDLE_VALUE) { DeleteTimerQueue(hTimerQueue); hTimerQueue = NULL; } } BOOLEAN TimerQueueStart(HANDLE& hTimer, void* io, int interval, VOID(*func)(PVOID lpParam, BOOLEAN TimerOrWaitFired)) { EnterCriticalSection(§ionTimerQueue); if (!CreateTimerQueueTimer(&hTimer, hTimerQueue, (WAITORTIMERCALLBACK)func, io, interval, interval, 0)) { //printf("CreateTimerQueueTimer failed (%d)\n", GetLastError()); hTimer = INVALID_HANDLE_VALUE; LeaveCriticalSection(§ionTimerQueue); return false; } LeaveCriticalSection(§ionTimerQueue); return true; } BOOLEAN TimerQueueStartOnce(HANDLE& hTimer, void* io, int interval, VOID(*func)(PVOID lpParam, BOOLEAN TimerOrWaitFired)) { EnterCriticalSection(§ionTimerQueue); if (!CreateTimerQueueTimer(&hTimer, hTimerQueue, (WAITORTIMERCALLBACK)func, io, interval, 0, 0)) { //printf("CreateTimerQueueTimer failed (%d)\n", GetLastError()); hTimer = INVALID_HANDLE_VALUE; LeaveCriticalSection(§ionTimerQueue); return false; } LeaveCriticalSection(§ionTimerQueue); return true; } BOOLEAN TimerQueueStop(HANDLE& hTimer, int flag) { if (hTimer == INVALID_HANDLE_VALUE) { return true; } EnterCriticalSection(§ionTimerQueue); if (hTimer == INVALID_HANDLE_VALUE) { LeaveCriticalSection(§ionTimerQueue); return true; } try { if (DeleteTimerQueueTimer(hTimerQueue, hTimer, NULL) == 0) { if (GetLastError() == ERROR_IO_PENDING) { hTimer = INVALID_HANDLE_VALUE; } } else { hTimer = INVALID_HANDLE_VALUE; } } catch (...) { hTimer = INVALID_HANDLE_VALUE; printf("error TimerQueueStop\n"); } //ChangeTimerQueueTimer(hTimerQueue, hTimer, 1000, 0);//修改定时器,修改为只执行一次 LeaveCriticalSection(§ionTimerQueue); if (hTimer == INVALID_HANDLE_VALUE) { return true; } else { return false; } } #define TOTAL 1000000 //定时器总数 1百万 1000000 #define BASE_RATE 10000 #define RATE_DELETE 1 //掉线率 RATE_DELETE/BASE_RATE #define RATE_SHOW 1 //打印概率 RATE_SHOW/BASE_RATE #define BASE_TIME 3000 #define CHECK_STATE_TIME 1000 //#define DELETE_RATE_INNER 3 HANDLE hTimerlist[1000000]; int hTimerlistindex[1000000]; VOID CALLBACK _TimeCheckSocketState(PVOID lpParam, BOOLEAN TimerOrWaitFired) { int* a = (int*)lpParam; int b = 0; if (hTimerlist[*a] == INVALID_HANDLE_VALUE) {//经测试,发现已经关闭的定时器也有一定的几率进入这里 if(TimerOrWaitFired) std::cout << "INVALID_HANDLE_VALUE HANDLE INDEX:" << *a << "\t VALUE:" << hTimerlist[*a]<<"\t TimerOrWaitFired:"<< "true" << "\n"; else std::cout << "INVALID_HANDLE_VALUE HANDLE INDEX:" << *a << "\t VALUE:" << hTimerlist[*a]<<"\t TimerOrWaitFired:"<< "false" << "\n"; } if (TimerOrWaitFired) { //模拟处理逻辑 while (b < 10000) { b++; } if (rand() % BASE_RATE < RATE_SHOW) {//%1几率显示 std::cout << "CURRENT HANDLE INDEX:"<<*a<<"\t VALUE:"<< hTimerlist[*a] << "\n"; } //if (rand() % 100 < DELETE_RATE_INNER) {//内部自关闭//经过测试,不行 // TimerQueueStop(hTimerlist[*a], 1); // std::cout << "Inner Close Test Index:" << *a << " Value:" << hTimerlist[*a] << "\n"; //} } else { std::cout << "timer canceled index:" << *a << " value:" << hTimerlist[*a] << "\n"; } } int main() { std::cout << "Hello World!\n"; InitTimerQueue(); for (int i = 0; i < TOTAL; i++) { hTimerlistindex[i] = i; int a = BASE_TIME - 500 + rand() % 1000; TimerQueueStart(hTimerlist[i], &hTimerlistindex[i], a, _TimeCheckSocketState); } while (1) { Sleep(CHECK_STATE_TIME); for (int i = 0; i < TOTAL; i++) { if (hTimerlist[i] == INVALID_HANDLE_VALUE) { int a = BASE_TIME - 500 + rand() % 1000; TimerQueueStart(hTimerlist[i], &hTimerlistindex[i], a, _TimeCheckSocketState); } if (rand() % BASE_RATE < RATE_DELETE) {// 掉线几率 TimerQueueStop(hTimerlist[i], 0); } } } ReleaseTimerQueue(); } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
//