Skip to content

Commit cb21dcb

Browse files
committed
Throttle test execution when tests are too fast
Large number (hundreds and more) of extremely fast tests could block the UI. To avoid that, measure how fast tests are. If they are too fast and the number of tests is significant then slow them down by sleeping.
1 parent d4dfbd1 commit cb21dcb

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

runner.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-- runner send messages with events to communicate with the parent process.
1010

1111
include "api.lua"
12+
include "throttler.lua"
1213

1314
local work_dir = env().path
1415

@@ -24,9 +25,12 @@ end
2425

2526
local id_sequence = 0
2627

27-
local tests <const> = {} -- {id=1,name=..}
28+
local tests <const> = {} -- {id=1,name=..}
29+
30+
local publish_throttler <const> = new_throttler(50) -- max 50 messages per frame
2831

2932
local function publish(msg)
33+
publish_throttler:throttle()
3034
send_message(parent_pid, msg)
3135
end
3236

throttler.lua

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- (c) 2024 Jacek Olszak
2+
-- This code is licensed under MIT license (see LICENSE for details)
3+
4+
-- creates an object which slows down the code if it runs too fast
5+
function new_throttler(max_per_frame)
6+
local fps <const> = 60
7+
local instructions_per_second = 8000000 -- max no of instructions in Picotron
8+
9+
local started, count
10+
11+
local function reset()
12+
started = time()
13+
count = 0
14+
end
15+
16+
reset()
17+
18+
local throttler <const> = {}
19+
20+
function throttler:throttle()
21+
count += 1
22+
if count > max_per_frame and
23+
count / (time() - started) / fps > max_per_frame
24+
then
25+
-- sleep for a frame
26+
for i = 1, instructions_per_second / fps do end
27+
reset()
28+
end
29+
end
30+
31+
return throttler
32+
end

0 commit comments

Comments
 (0)