summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-03-19 23:03:25 +0800
committerruki <[email protected]>2025-04-08 15:31:54 +0800
commit6785ce5822f298ce64684548dfc9d35ec8dd5030 (patch)
treeb9a4dc5298695b320b3e0bdf709a7a759d0ec24d
parent1621db050a13474f4d79d02c4d306013770d35b4 (diff)
improve runjobs tests
-rw-r--r--tests/modules/async/jobgraph.lua7
-rw-r--r--tests/modules/async/run_callback.lua19
-rw-r--r--tests/modules/async/run_jobgraph.lua28
-rw-r--r--tests/modules/async/run_jobpool.lua28
-rw-r--r--tests/modules/async/runjobs.lua43
-rw-r--r--xmake/modules/async/jobgraph.lua15
6 files changed, 90 insertions, 50 deletions
diff --git a/tests/modules/async/jobgraph.lua b/tests/modules/async/jobgraph.lua
deleted file mode 100644
index 7a0b87c65..000000000
--- a/tests/modules/async/jobgraph.lua
+++ /dev/null
@@ -1,7 +0,0 @@
-import("async.jobgraph")
-
-function main()
- local gh = jobgraph.new()
- print(gh)
-end
-
diff --git a/tests/modules/async/run_callback.lua b/tests/modules/async/run_callback.lua
new file mode 100644
index 000000000..21e75014b
--- /dev/null
+++ b/tests/modules/async/run_callback.lua
@@ -0,0 +1,19 @@
+import("core.base.scheduler")
+import("async.runjobs")
+
+function _jobfunc(index, total, opt)
+ print("%s: run job (%d/%d)", scheduler.co_running(), index, total)
+ local dt = os.mclock()
+ os.sleep(1000)
+ dt = os.mclock() - dt
+ print("%s: run job (%d/%d) end, progress: %s, dt: %d ms", scheduler.co_running(), index, total, opt.progress, dt)
+end
+
+function main()
+ print("==================================== test callback ====================================")
+ local t = os.mclock()
+ runjobs("test", _jobfunc, {total = 100, comax = 6, timeout = 1000, timer = function (running_jobs_indices)
+ print("%s: timeout (%d ms), running: %s", scheduler.co_running(), os.mclock() - t, table.concat(running_jobs_indices, ","))
+ end})
+end
+
diff --git a/tests/modules/async/run_jobgraph.lua b/tests/modules/async/run_jobgraph.lua
new file mode 100644
index 000000000..bcf5be2fd
--- /dev/null
+++ b/tests/modules/async/run_jobgraph.lua
@@ -0,0 +1,28 @@
+import("core.base.scheduler")
+import("async.jobgraph")
+import("async.runjobs")
+
+function _jobfunc(job, opt)
+ print("%s: run job (%s)", scheduler.co_running(), job.name)
+ local dt = os.mclock()
+ os.sleep(1000)
+ dt = os.mclock() - dt
+ print("%s: run job (%s) end, progress: %s, dt: %d ms", scheduler.co_running(), job.name, opt.progress, dt)
+end
+
+function main()
+ print("==================================== test jobpool ====================================")
+ local jobs = jobgraph.new()
+ jobs:add_job("job/root", _jobfunc)
+ for i = 1, 3 do
+ jobs:add_job("job/" .. i, _jobfunc)
+ for j = 1, 50 do
+ jobs:add_job("job/" .. i .. "/" .. j, _jobfunc)
+ end
+ end
+ t = os.mclock()
+ runjobs("test", jobs, {comax = 6, timeout = 1000, timer = function (running_jobs_indices)
+ print("%s: timeout (%d ms), running: %s", scheduler.co_running(), os.mclock() - t, table.concat(running_jobs_indices, ","))
+ end})
+end
+
diff --git a/tests/modules/async/run_jobpool.lua b/tests/modules/async/run_jobpool.lua
new file mode 100644
index 000000000..5de54e36c
--- /dev/null
+++ b/tests/modules/async/run_jobpool.lua
@@ -0,0 +1,28 @@
+import("core.base.scheduler")
+import("private.async.jobpool")
+import("async.runjobs")
+
+function _jobfunc(index, total, opt)
+ print("%s: run job (%d/%d)", scheduler.co_running(), index, total)
+ local dt = os.mclock()
+ os.sleep(1000)
+ dt = os.mclock() - dt
+ print("%s: run job (%d/%d) end, progress: %s, dt: %d ms", scheduler.co_running(), index, total, opt.progress, dt)
+end
+
+function main()
+ print("==================================== test jobpool ====================================")
+ local jobs = jobpool.new()
+ local root = jobs:addjob("job/root", _jobfunc)
+ for i = 1, 3 do
+ local job = jobs:addjob("job/" .. i, _jobfunc, {rootjob = root})
+ for j = 1, 50 do
+ jobs:addjob("job/" .. i .. "/" .. j, _jobfunc, {rootjob = job})
+ end
+ end
+ t = os.mclock()
+ runjobs("test", jobs, {comax = 6, timeout = 1000, timer = function (running_jobs_indices)
+ print("%s: timeout (%d ms), running: %s", scheduler.co_running(), os.mclock() - t, table.concat(running_jobs_indices, ","))
+ end})
+end
+
diff --git a/tests/modules/async/runjobs.lua b/tests/modules/async/runjobs.lua
deleted file mode 100644
index 4e2a98cc0..000000000
--- a/tests/modules/async/runjobs.lua
+++ /dev/null
@@ -1,43 +0,0 @@
-import("core.base.scheduler")
-import("private.async.jobpool")
-import("async.runjobs")
-
-function _jobfunc(index, total, opt)
- print("%s: run job (%d/%d)", scheduler.co_running(), index, total)
- local dt = os.mclock()
- os.sleep(1000)
- dt = os.mclock() - dt
- print("%s: run job (%d/%d) end, progress: %s, dt: %d ms", scheduler.co_running(), index, total, opt.progress, dt)
-end
-
-function main()
-
- -- test callback
- print("==================================== test callback ====================================")
- local t = os.mclock()
- runjobs("test", _jobfunc, {total = 100, comax = 6, timeout = 1000, timer = function (running_jobs_indices)
- print("%s: timeout (%d ms), running: %s", scheduler.co_running(), os.mclock() - t, table.concat(running_jobs_indices, ","))
- end})
-
- -- test jobs
- print("==================================== test jobs ====================================")
- local jobs = jobpool.new()
- local root = jobs:addjob("job/root", function (index, total, opt)
- _jobfunc(index, total, opt)
- end)
- for i = 1, 3 do
- local job = jobs:addjob("job/" .. i, function (index, total, opt)
- _jobfunc(index, total, opt)
- end, {rootjob = root})
- for j = 1, 50 do
- jobs:addjob("job/" .. i .. "/" .. j, function (index, total, opt)
- _jobfunc(index, total, opt)
- end, {rootjob = job})
- end
- end
- t = os.mclock()
- runjobs("test", jobs, {comax = 6, timeout = 1000, timer = function (running_jobs_indices)
- print("%s: timeout (%d ms), running: %s", scheduler.co_running(), os.mclock() - t, table.concat(running_jobs_indices, ","))
- end})
-end
-
diff --git a/xmake/modules/async/jobgraph.lua b/xmake/modules/async/jobgraph.lua
index 3fd36194a..30513ae56 100644
--- a/xmake/modules/async/jobgraph.lua
+++ b/xmake/modules/async/jobgraph.lua
@@ -31,6 +31,21 @@ function jobgraph:jobs()
return self._jobs
end
+-- add a job to the jobgraph
+--
+-- e.g.
+-- jobgraph:add_job("xxx", function (job, opt)
+-- end)
+--
+-- @param name the job name
+-- @param run the job run command/script
+-- @param opt the job options
+--
+function jobgraph:add_job(name, run, opt)
+ local job = {name = name, run = run, opt = opt}
+ self:jobs():insert(job)
+end
+
-- tostring
function jobgraph:__tostring()
return string.format("<jobgraph:%s>", self:jobs():size())