From febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354 Mon Sep 17 00:00:00 2001 From: moteus Date: Thu, 26 Dec 2013 12:00:41 +0400 Subject: First commit. --- test/test_llthreads.lua | 80 ++++++++++++++++++++++++++++ test/test_table_copy.lua | 134 +++++++++++++++++++++++++++++++++++++++++++++++ test/test_threads.lua | 74 ++++++++++++++++++++++++++ 3 files changed, 288 insertions(+) create mode 100644 test/test_llthreads.lua create mode 100644 test/test_table_copy.lua create mode 100644 test/test_threads.lua (limited to 'test') diff --git a/test/test_llthreads.lua b/test/test_llthreads.lua new file mode 100644 index 0000000..3474b9b --- /dev/null +++ b/test/test_llthreads.lua @@ -0,0 +1,80 @@ +-- Copyright (c) 2011 by Robert G. Jakabosky +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in +-- all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +-- THE SOFTWARE. + +local llthreads = require"llthreads" + +local sleep +local status, socket = pcall(require,"socket") +if status then + sleep = function(secs) + return socket.sleep(secs) + end +end + +if not sleep then + local status, ztimer = pcall(require, "lzmq.timer") + if status then + sleep = function(secs) + ztimer.sleep(secs * 1000) + end + end +end + +if not sleep then + sleep = function(secs) + os.execute("sleep " .. tonumber(secs)) + end +end + +local function detached_thread(...) + local thread = llthreads.new([[ print("print_detached_thread:", ...) ]], ...) + -- start detached thread + assert(thread:start(true)) + return thread +end + +local function print_thread(...) + local thread = llthreads.new([[ print("print_thread:", ...); ]], ...) + -- start joinable thread + assert(thread:start()) + return thread +end + +local function pass_through_thread(...) + local thread = llthreads.new([[ return "pass_thread:", ... ]], ...) + -- start joinable thread + assert(thread:start()) + return thread +end + +local thread1 = detached_thread("number:", 1234, "nil:", nil, "bool:", true) + +sleep(1) + +local thread2 = print_thread("number:", 1234, "nil:", nil, "bool:", true) +print("thread2:join: results # = ", select('#', thread2:join())) + +sleep(1) + +local thread3 = pass_through_thread("number:", 1234, "nil:", nil, "bool:", true) +print("thread3:join:", thread3:join()) + +sleep(1) + diff --git a/test/test_table_copy.lua b/test/test_table_copy.lua new file mode 100644 index 0000000..0408ad3 --- /dev/null +++ b/test/test_table_copy.lua @@ -0,0 +1,134 @@ +-- Copyright (c) 2011 by Robert G. Jakabosky +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in +-- all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +-- THE SOFTWARE. + +local llthreads = require"llthreads" + +local sleep +local status, socket = pcall(require,"socket") +if status then + sleep = function(secs) + return socket.sleep(secs) + end +else + sleep = function(secs) + os.execute("sleep " .. tonumber(secs)) + end +end + +local dump_code = [==[ +local function dump_recur(seen, obj, depth) + local out + local t = type(obj) + -- if not a table just convert to string. + if t ~= "table" then + if t == "string" then + return '"' .. obj .. '"' + end + return tostring(obj) + end + -- check if this table has been seen already. + if seen[obj] then + return "Already dumped " .. tostring(obj) + end + seen[obj] = true + -- restrict max depth. + if depth >= 10 then + return "{... max depth reached ...}" + end + depth = depth + 1 + -- output table key/value pairs + local tabs = string.rep(" ",depth) + local out = "{\n" + for k,v in pairs(obj) do + if type(k) ~= "number" then + out = out .. tabs .. '[' .. dump_recur(seen, k, depth) .. '] = ' .. + dump_recur(seen, v, depth) .. ',\n' + else + out = out .. tabs .. '[' .. k .. '] = ' .. dump_recur(seen, v, depth) .. ',\n' + end + end + return out .. tabs:sub(1,-3) .. "}" +end + +local obj = ... +local seen = {} +return dump_recur(seen, obj, 0) +]==] + +local dump = (loadstring or load)(dump_code) + +local child_code = [==[ +local dump = (loadstring or load)[[ +]==] .. dump_code .. [==[ +]] +local args = ... + +print("Child thread args:", dump(args)) + +-- return all values. +return ... +]==] + +local function test_thread_value_copying(...) + local args = {...} + print("Main thread args:", dump(args)) + local thread = llthreads.new(child_code, args) + -- start joinable thread + assert(thread:start()) + + local status, results = thread:join() + print("Main thread results:", dump(results)) +end + +-- create some tables. +local a1 = { "a1" } +local a2 = { "a2" } +local a3 = { "a3" } +local a4 = { "a4" } +local b1 = { a1, a2, a3, a4 } +local b2 = { a1=a1, a2=a2, a3=a3, a4=a4 } + +-- +-- no loops +-- +test_thread_value_copying(b1, b2) + +local top = {} +-- self reference. +top.top = top +top[top] = top +-- nested reference. +top.sub1 = { sub2 = { sub3 = { top } } } + +-- +-- loops +-- +test_thread_value_copying(top) + +-- +-- Test max depth +-- +local outer = {} +for n=1,100 do + outer = {outer} +end +local status, err = pcall(test_thread_value_copying,outer) +assert(not status, "Assertion failed: max depth test failed.") + diff --git a/test/test_threads.lua b/test/test_threads.lua new file mode 100644 index 0000000..467526e --- /dev/null +++ b/test/test_threads.lua @@ -0,0 +1,74 @@ +-- Copyright (c) 2011 by Ross Anderson +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in +-- all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +-- THE SOFTWARE. + +-- Sub-thread processing example in Lua using llthreads - 1,000 quick sub-thread execution + +-- luajit sub_threads.lua + +local llthreads = require"llthreads" + +local num_threads = tonumber(arg[1] or 1000) + +-- level 0 string literal enclosure [[ ]] of child execution code +local thread_code = [[ + local lua_init = os.getenv("lua_init") + if lua_init and #lua_init > 0 then + if lua_init:sub(1,1) == '@' then + dofile(lua_init:sub(2)) + else + assert((loadstring or load)(lua_init))() + end + end + + local num_threads = ... + print("CHILD: received from ROOT params:", ...) + local llthreads = require"llthreads" -- need to re-declare this under this scope + local t = {} -- thread storage table + + -- create a new child sub-thread execution code - it requires level 1 literal string [=[ ]=] enclosures, level 2 would be [==[ ]==] + local executed_child_code = [=[ + return "Hello from child sub-thread, new input params:", ... + ]=] + + -- create 1000 sub-threads - which creates an incremental 30% / 20% utilization spike on the two AMD cpu cores + print("CHILD: Create sub threads:", num_threads) + for i=1,num_threads do + -- create child sub-thread with code to execute and the input parmeters + local thread = llthreads.new(executed_child_code , "number:", 1000 + i, "nil:", nil, "bool:", true) + assert(thread:start()) -- start new child sub-thread + table.insert(t, thread) -- append the thread at the end of the thread table + end + + -- wait (block) for all child sub-threads to complete before returning to ROOT + while true do + -- always wait on the first element, since order is not important + print("CHILD: sub-thread returned: ", t[1]:join()) + table.remove(t,1) -- always remove the first element + if (#t == 0) then break end + end + return ... -- return the parents' input params back to the root +]] + +-- create child thread. +local thread = llthreads.new(thread_code, num_threads, "number:", 1000, "nil:", nil, "bool:", true) +-- start joinable child thread. +assert(thread:start()) +-- wait for all child and child sub-threads to finish +print("ROOT: child returned: ", thread:join()) -- cgit v1.2.3-55-g6feb