Synchronization of elapsed time in the combined.run method
One of the special features of the run method is that it synchronizes the execution time of the
script and the actual time spent.
Here is an example: there is a list of events where the various actions "ld#Ld#k#d#K#d#" are
recorded, using the list of events you can understand that there is a click of mouse button and keystroke of the
keyboard key. We transformed this line to a more descriptive form and filled in the unknown parameters:
"ld#Ld#k#d#K#d#" >>>
mouse.press('LEFT');
system.sleep(30);
mouse.release('LEFT');
system.sleep(45);
key.press('A');
system.sleep(100);
key.release('A');
system.sleep(35);
There is a delay between each event in the form of system.sleep(ms). Theoretical time of the script execution will be equal to the sum of all specified delays by the methodsystem.sleep() - 30 + 45 + 100 + 35 = 210ms. The time for which this script is actually executed will be slightly longer (plus the time the script itself executes and the internal delays of objects), which will make an undesirable deviation from real time. To compensate the difference in time, this difference is subtracted from the time of the system.sleep() method after each event.
mouse.press('LEFT');
system.sleep(50);
mouse.release('LEFT');
system.sleep(50);
One event of pressing the left mouse button and the delay following it. Similarly for the releasing event. Let's calculate the execution time of this code.
mouse.press('LEFT');
mouse.pressDelay = 10 ms.
mouse.releaseDelay = 10 ms.
mouse.press('LEFT'); // 10 ms.
system.sleep(50); // 50 ms.
mouse.release('LEFT'); // 10 ms.
system.sleep(50); // 50 ms.
The specified execution time is 50 + 50 = 100 ms.
The actual execution time is 10 + 50 + 10 + 50 = 120 ms. + (overhead)
Run-time difference is 120 - 100 = 20 ms.
In such a situation, the actual execution time will be at least 120 ms., and taking into account overheads even
more. Under the overhead is meant the time of execution of the code by the program.
In order to remove this difference, the program, when executing the run method, compensates the
execution time by subtracting the difference from the system.sleep() method. So, the following
code will be executed instead.
mouse.press('LEFT'); // 10 ms.
system.sleep(40); // 50 - 10 = 40 ms.
mouse.release('LEFT'); // 10 ms.
system.sleep(40); // 50 - 10 = 40 ms.
Thus, the execution time of the script will correspond to the real time. But there are limitations.
What happens if the time of the event itself is less than the available time for compensation? Let's look at an
example.
mouse.pressDelay = 10 ms.
mouse.releaseDelay = 10 ms.
mouse.press('LEFT'); // 10 ms.
system.sleep(8); // 8 ms.
mouse.release('LEFT'); // 10 ms.
system.sleep(8); // 8 ms.
The specified execution time is 8 + 8 = 16 ms.
The actual execution time is 10 + 8 + 10 + 8 = 36 ms. + (overhead)
Run-time difference is 36 - 16 = 20 ms.
In this situation, it turns out that the time difference is greater than the time that can be compensated by subtracting from the system.sleep() method. In this situation, the maximum possible time is subtracted from the system.sleep() method, and the remainder is subtracted from the next event
mouse.press('LEFT'); // 10 ms.
system.sleep(0); // 8 - 10 = 0 ms. (remainder = 2)
mouse.release('LEFT'); // 10 ms.
system.sleep(0); // 8 - 10 = 0 ms. (remainder = 4)
If you do not succeed in subtracting the time from the next event, the remainder will accumulate, but it will always be limited to the combined.maxDelayDiff parameter. By default value of combined.maxDelayDiff = 100 ms.