add benchmarking, more models and examples
This commit is contained in:
48
scripts/run_rhai.lua
Normal file
48
scripts/run_rhai.lua
Normal file
@@ -0,0 +1,48 @@
|
||||
--[[
|
||||
Submits a Rhai script to a distributed worker and returns the task ID.
|
||||
|
||||
Since BLPOP cannot block inside Lua scripts (they execute atomically),
|
||||
this script only submits the task and returns the task ID. The client
|
||||
must then use BLPOP separately to wait for the result.
|
||||
|
||||
ARGV[1] (string): The target circle name (e.g., "default").
|
||||
ARGV[2] (string): The Rhai script content to execute.
|
||||
|
||||
Returns:
|
||||
- A JSON string containing the task ID and reply queue name.
|
||||
]]
|
||||
|
||||
-- 1. Argument Validation
|
||||
local circle_name = ARGV[1]
|
||||
if not circle_name or circle_name == '' then
|
||||
return cjson.encode({error = "ARGV[1] 'circle_name' is required."})
|
||||
end
|
||||
|
||||
local rhai_script = ARGV[2]
|
||||
if not rhai_script or rhai_script == '' then
|
||||
return cjson.encode({error = "ARGV[2] 'rhai_script' is required."})
|
||||
end
|
||||
|
||||
-- 2. Initialization
|
||||
local task_details_prefix = "rhai_task_details:"
|
||||
local tasks_queue_prefix = "rhai_tasks:"
|
||||
local reply_queue_prefix = "rhai_reply:"
|
||||
|
||||
local task_id = redis.sha1hex(rhai_script .. redis.call('TIME')[1] .. redis.call('TIME')[2] .. math.random())
|
||||
local reply_queue_key = reply_queue_prefix .. task_id
|
||||
local task_details_key = task_details_prefix .. task_id
|
||||
local task_queue_key = tasks_queue_prefix .. circle_name
|
||||
|
||||
-- 3. Task Creation & Queuing
|
||||
redis.call('HSET', task_details_key,
|
||||
'script', rhai_script,
|
||||
'status', 'pending',
|
||||
'replyToQueue', reply_queue_key
|
||||
)
|
||||
redis.call('LPUSH', task_queue_key, task_id)
|
||||
|
||||
-- 4. Return task information for client to wait on
|
||||
return cjson.encode({
|
||||
task_id = task_id,
|
||||
reply_queue = reply_queue_key
|
||||
})
|
54
scripts/run_rhai_batch.lua
Normal file
54
scripts/run_rhai_batch.lua
Normal file
@@ -0,0 +1,54 @@
|
||||
-- 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
|
Reference in New Issue
Block a user