I think I might be getting closer to finding the odd discrepancy here. The Promise and Task Scheduler implementations might be fine and it might be my test code. If the problem does indeed lie in my test code, then that circles back to my original question but with the added acknowledgement that the way I’m currently trying to pause between tasks is bad.
This time, I tried adding a quick benchmarker function at the top of my code using os.clock.
Test code with benchmarking
local lastBench = os.clock()
local function pushBench(label)
local now = os.clock()
print(tostring(label) .. ": " .. tostring(now - lastBench) .. " seconds")
lastBench = now
end
pushBench("Test start / benchmarker setup time")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local TaskScheduler = require(ReplicatedFirst.TaskScheduler)
local Promise = require(ReplicatedFirst.Promise)
local PAUSE_TIME = 2
pushBench("Make declarations")
local scheduler = TaskScheduler:CreateScheduler(60)
pushBench("Create 60 FPS scheduler")
local firstTaskScheduleTime
local secondTaskScheduleTime
scheduler:QueueTask(function ()
print("First task ran.")
Promise.try(scheduler.Pause, scheduler):andThen(function ()
return Promise.delay(PAUSE_TIME):andThenCall(scheduler.Resume, scheduler)
end)
firstTaskScheduleTime = os.clock()
end)
pushBench("First task queue time")
scheduler:QueueTask(function ()
print("Second task ran.")
secondTaskScheduleTime = os.clock()
end)
pushBench("Second task queue time")
scheduler:QueueTask(function ()
print("Time between tasks: " .. tostring(secondTaskScheduleTime - firstTaskScheduleTime) .. " seconds")
end)
pushBench("Third task queue time")
Raw results:
21:36:33.246 Test start / benchmarker setup time: 2.0000152289867e-07 seconds
21:36:33.248 Make declarations: 0.0018344999989495 seconds
21:36:33.262 Create 60 FPS scheduler: 0.014190900023095 seconds
21:36:37.048 First task ran.
21:36:37.048 First task queue time: 3.7856951999711 seconds
21:36:37.048 Second task queue time: 9.5700030215085e-05 seconds
21:36:37.048 Third task queue time: 8.5399951785803e-05 seconds
21:36:41.032 Second task ran.
21:36:41.032 Time between tasks: 3.9836974000791 seconds
Most of the times here are variable and expected except for the queue time of my first task. To reiterate in plain view what the queueing of my first task looks like (happens immediately after creating the scheduler and declaring two variables to find the delta execution time):
scheduler:QueueTask(function ()
print("First task ran.")
Promise.try(scheduler.Pause, scheduler):andThen(function ()
return Promise.delay(PAUSE_TIME):andThenCall(scheduler.Resume, scheduler)
end)
firstTaskScheduleTime = os.clock()
end)
I was close to chalking up the problem to the Promise but that wouldn’t make any sense. The Promise should not execute because the function is not called, thus making my results strange. The second and third task queue pretty much asynchronously as they should with barely time to get them in the scheduler but the first task takes an abysmal amount of time, nearly equivalent to the time I’m waiting to pause for. Commented out the Promise to check, still took 3 seconds to queue.
Next thought was to debug the time it takes to create a scheduler by declaring the current value of os.clock() immediately after TaskScheduler.CreateScheduler is called and print the difference just before the scheduler is returned to the caller script. The time it takes to create the scheduler is less than a tenth of a second, in some cases significantly smaller, so it’s giving me expected time frames.
This is where my problem lies. All my Lua-side code, regardless of SignalBehavior, is running at times that I’m fully expecting. The thing is, that first QueueTask takes three seconds to complete for whatever reason and then the Promise is delaying for 3-5 seconds when I specify it to delay for 2 seconds…
Problem with my machine maybe? Set up a Heartbeat test just in case.
local Heartbeat = game:GetService("RunService").Heartbeat
local PRINT_THRESHOLD = 1
Heartbeat:Connect(function (delta)
if delta >= PRINT_THRESHOLD then
print("Delta exceeds threshold (threshold: " .. tostring(PRINT_THRESHOLD) .. ", got " .. tostring(delta) .. ")")
end
end)
Only ever exceeds the threshold once. Not a problem with my machine. Everything is logical and correct except the time it’s taking to queue the first task and the time Promise is delaying for.