Jump to content
Sign in to follow this  
wolflake

_timers Stress Test with 100 timers

Recommended Posts

I was writing a program that was going to use timers and I began to wonder what would happen if a couple of the timers went off at the same time, so I decided to design a stress test for the timers.
I set up 2 sets and 50 timers one that would go off in 2 seconds and one that would go off in 1 second and they all callback to the same function. 
I set up the stack so that as the timers triggered they would push down their ID on the stack and kill the timer. 
 
I slowed down the processing so that the second set of timers triggering would interrupt the processing of the first set of timers. (To see how fast the processing should go un-comment the while loop.)
The processing loop pulls the timer-id off the stack and writes it to the console.
 
The stack is last in first out (LIFO) so that when the process is interrupted the stack is also changed but in the end all of the timers on the stack are processed.
 
As you can see Autoit handles everything beautifully, even though the timers are going off at the same time they queue up nicely.  And while the processing is interrupted it continues where it should.
 
Hope you find this as interesting as I did.
 
;Timers Test
#include <timers.au3>
#include <array.au3>
#include <date.au3>

HotKeySet("{Esc}", Close)
$hWnd = GUICreate("me")
Global $run = 0, $aStack[101], $cnt=0 ,$iTcnt = 1

For $i = 1 to 50
    $iTimer_id = _Timer_SetTimer($hWnd, 2000, "alrm1") ; create timers
Next
ConsoleWrite("2 Second Timers are all running" & @CRLF)

For $i = 1 to 50
    $iTimer_id = _Timer_SetTimer($hWnd, 1000, "alrm1") ; create more timer
Next
ConsoleWrite("1 Second Timers are all running" & @CRLF)

While 1 ;wait to process timers
    Sleep(50)
    ;while $cnt > 0
        if $cnt > 0 then
            if $iTcnt = 1 then  ConsoleWrite("Start " & _now() & @CRLF)
            $run = _ArrayPop($aStack)
            ConsoleWrite($iTcnt & " " &"R-"&$run & @CRLF)
            $iTcnt += 1
            $cnt -= 1
            if UBound($aStack) = 1 or $cnt = 0 or $iTcnt = 101 then
                ConsoleWrite("End " & _now() & @CRLF)
                ReDim $aStack[101]
                ExitLoop
            EndIf
        EndIf
    ;WEnd
    if $iTcnt > 100 then Exit
WEnd
Exit

Func alrm1($hWnd, $iMsg, $iIDTimer, $iTime)
    #forceref $hWnd, $iMsg, $iIDTimer, $iTime
    ;All timers return here and are pushed on a stack
    _ArrayPush($aStack,$iIDTimer) ;LIFO stack
    ConsoleWrite($iIDTimer &" "& $iTime & @CRLF)
    _Timer_KillTimer($hWnd, $iIDTimer)
    $cnt += 1 ;timers count
EndFunc   ;==>alrm1

Func Close()
    Exit 0
EndFunc

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...