rhailib/scripts/run_rhai_batch.lua
2025-06-12 05:21:52 +03:00

54 lines
1.8 KiB
Lua

-- Simple Batch Task Creation Script for Rhailib Benchmarking
-- Creates N tasks and returns their Redis keys for timing analysis
-- Script Arguments (ARGV):
-- ARGV[1]: circle_name - The worker circle to send tasks to (e.g., "bench_circle")
-- ARGV[2]: task_count - Number of tasks to create (N)
-- ARGV[3]: rhai_script_content - The Rhai script to execute for all tasks
-- ARGV[4]: batch_id - Batch identifier for grouping
-- Validate arguments
if #ARGV < 4 then
return redis.error_reply("Usage: EVAL script 0 circle_name task_count rhai_script_content batch_id")
end
local circle_name = ARGV[1]
local task_count = tonumber(ARGV[2])
local rhai_script_content = ARGV[3]
local batch_id = ARGV[4]
-- Validate task_count
if not task_count or task_count <= 0 or task_count > 10000 then
return redis.error_reply("task_count must be a positive integer between 1 and 10000")
end
-- Get current timestamp
local current_time = redis.call('TIME')[1]
local rhai_task_queue = 'rhai_tasks:' .. circle_name
local task_keys = {}
-- Create tasks and collect their keys
for i = 1, task_count do
-- Generate unique task ID
local task_id = 'task_' .. redis.call('INCR', 'global_task_counter')
local task_details_key = 'rhai_task_details:' .. task_id
-- Create task details hash with creation timestamp
redis.call('HSET', task_details_key,
'script', rhai_script_content,
'status', 'pending',
'batch_id', batch_id,
'createdAt', current_time,
'updatedAt', current_time,
'task_sequence', tostring(i)
)
-- Queue the task for workers
redis.call('LPUSH', rhai_task_queue, task_id)
-- Add key to return array
table.insert(task_keys, task_details_key)
end
-- Return array of task keys for benchmarking tool to analyze
return task_keys