diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/cancel.lua | 93 | ||||
-rw-r--r-- | tests/linda_perf.lua | 8 | ||||
-rw-r--r-- | tests/perftest.lua | 17 |
3 files changed, 105 insertions, 13 deletions
diff --git a/tests/cancel.lua b/tests/cancel.lua new file mode 100644 index 0000000..0aab341 --- /dev/null +++ b/tests/cancel.lua | |||
@@ -0,0 +1,93 @@ | |||
1 | local lanes = require "lanes" .configure{ with_timers = false} | ||
2 | |||
3 | local linda = lanes.linda() | ||
4 | |||
5 | local laneBody = function( timeout_) | ||
6 | set_finalizer( function( err, stk) | ||
7 | if err == lanes.cancel_error then | ||
8 | -- note that we don't get the cancel_error when running wrapped inside a protected call if it doesn't rethrow it | ||
9 | print( " laneBody after cancel" ) | ||
10 | elseif err then | ||
11 | print( " laneBody error: "..tostring(err)) | ||
12 | else | ||
13 | print(" laneBody finalized") | ||
14 | end | ||
15 | end) | ||
16 | |||
17 | print( " entering lane with " .. tostring( timeout_) .. " timeout") | ||
18 | repeat | ||
19 | -- block-wait to be hard-cancelled | ||
20 | print " lane calling receive()" | ||
21 | local key, val = linda:receive( timeout_, "boob") | ||
22 | print( " receive() -> ", lanes.cancel_error == key and "cancel_error" or tostring( key), tostring( val)) | ||
23 | until cancel_test() -- soft cancel self test | ||
24 | print " shutting down after breaking out of loop" | ||
25 | end | ||
26 | |||
27 | local protectedBody = function( ...) | ||
28 | local ce = lanes.cancel_error | ||
29 | local errorHandler = function( _msg) | ||
30 | -- forward the message to the main thread that will display it with a popup | ||
31 | print( " error handler got ", ce == _msg and "cancel_error"or tostring( _msg)) | ||
32 | return _msg | ||
33 | end | ||
34 | -- Lua 5.1 doesn't pass additional xpcall arguments to the called function | ||
35 | -- therefore we need to create a closure that has no arguments but pulls everything from its upvalue | ||
36 | local params = {...} | ||
37 | local paramLessClosure = function() laneBody(table.unpack( params)) end | ||
38 | local status, message = xpcall( paramLessClosure, errorHandler) | ||
39 | if status == false then | ||
40 | print( " error handler rethrowing '" .. (ce == message and "cancel_error"or tostring( message)) .. "'") | ||
41 | -- if the error isn't rethrown, the lane's finalizer won't get it | ||
42 | error( message) | ||
43 | end | ||
44 | end | ||
45 | |||
46 | --#################################################################### | ||
47 | |||
48 | print "####################################################################\nbegin soft cancel test\n" | ||
49 | h = lanes.gen("*", protectedBody)( 0.666) | ||
50 | print "wait 3s" | ||
51 | linda:receive( 3, "yeah") | ||
52 | |||
53 | -- soft cancel | ||
54 | print "soft cancel with awakening" | ||
55 | h:cancel( -1, true) | ||
56 | |||
57 | -- wait 10s: the lane will interrupt its loop and print the exit message | ||
58 | print "wait 2s" | ||
59 | linda:receive( 2, "yeah") | ||
60 | |||
61 | --#################################################################### | ||
62 | |||
63 | print "\n\n####################################################################\nbegin hard cancel test\n" | ||
64 | h = lanes.gen("*", protectedBody)() | ||
65 | |||
66 | -- wait 3s before cancelling the lane | ||
67 | print "wait 3s" | ||
68 | linda:receive( 3, "yeah") | ||
69 | |||
70 | -- hard cancel and wait 10s: the lane will be interrupted from inside its current linda:receive() and won't return from it | ||
71 | print "hard cancel (always awakens)" | ||
72 | h:cancel() | ||
73 | |||
74 | print "wait 5s" | ||
75 | linda:receive( 5, "yeah") | ||
76 | |||
77 | --#################################################################### | ||
78 | |||
79 | print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" | ||
80 | h = lanes.gen("*", laneBody)() | ||
81 | |||
82 | -- wait 3s before cancelling the lane | ||
83 | print "wait 3s" | ||
84 | linda:receive( 3, "yeah") | ||
85 | |||
86 | -- hard cancel and wait 10s: the lane will be interrupted from inside its current linda:receive() and won't return from it | ||
87 | print "hard cancel (always awakens)" | ||
88 | h:cancel() | ||
89 | |||
90 | print "wait 5s" | ||
91 | linda:receive( 5, "yeah") | ||
92 | |||
93 | print "\ndone" \ No newline at end of file | ||
diff --git a/tests/linda_perf.lua b/tests/linda_perf.lua index ebe9eac..9348f71 100644 --- a/tests/linda_perf.lua +++ b/tests/linda_perf.lua | |||
@@ -34,7 +34,7 @@ local function ziva( preloop, loop, batch) | |||
34 | -- prefill the linda a bit to increase fifo stress | 34 | -- prefill the linda a bit to increase fifo stress |
35 | local top = math.max( preloop, loop) | 35 | local top = math.max( preloop, loop) |
36 | local l, lane = lanes.linda() | 36 | local l, lane = lanes.linda() |
37 | local t1 = os.time() | 37 | local t1 = lanes.now_secs() |
38 | for i = 1, preloop do | 38 | for i = 1, preloop do |
39 | l:send( "key", i) | 39 | l:send( "key", i) |
40 | end | 40 | end |
@@ -59,7 +59,7 @@ local function ziva( preloop, loop, batch) | |||
59 | end | 59 | end |
60 | l:send( "done" ,"are you happy?") | 60 | l:send( "done" ,"are you happy?") |
61 | lane:join() | 61 | lane:join() |
62 | return os.difftime(os.time(), t1) | 62 | return lanes.now_secs() - t1 |
63 | end | 63 | end |
64 | 64 | ||
65 | local tests = | 65 | local tests = |
@@ -149,7 +149,7 @@ local function ziva2( preloop, loop, batch) | |||
149 | l:receive( "key") | 149 | l:receive( "key") |
150 | end | 150 | end |
151 | end | 151 | end |
152 | local t1 = os.time() | 152 | local t1 = lanes.now_secs() |
153 | -- first, prime the linda with some data | 153 | -- first, prime the linda with some data |
154 | for i = 1, preloop, step do | 154 | for i = 1, preloop, step do |
155 | batch_send() | 155 | batch_send() |
@@ -165,7 +165,7 @@ local function ziva2( preloop, loop, batch) | |||
165 | for i = 1, preloop, step do | 165 | for i = 1, preloop, step do |
166 | batch_read() | 166 | batch_read() |
167 | end | 167 | end |
168 | return os.difftime(os.time(), t1) | 168 | return lanes.now_secs() - t1 |
169 | end | 169 | end |
170 | 170 | ||
171 | local tests2 = | 171 | local tests2 = |
diff --git a/tests/perftest.lua b/tests/perftest.lua index 4df2ad8..6ffc064 100644 --- a/tests/perftest.lua +++ b/tests/perftest.lua | |||
@@ -27,8 +27,7 @@ | |||
27 | local MSYS= os.getenv("OSTYPE")=="msys" | 27 | local MSYS= os.getenv("OSTYPE")=="msys" |
28 | 28 | ||
29 | 29 | ||
30 | local lanes = require "lanes" | 30 | local lanes = require "lanes".configure{ with_timers = false} |
31 | lanes.configure() | ||
32 | 31 | ||
33 | local m= require "argtable" | 32 | local m= require "argtable" |
34 | local argtable= assert( m.argtable ) | 33 | local argtable= assert( m.argtable ) |
@@ -36,7 +35,7 @@ local argtable= assert( m.argtable ) | |||
36 | local N= 1000 -- threads/loops to use | 35 | local N= 1000 -- threads/loops to use |
37 | local M= 1000 -- sieves from 1..M | 36 | local M= 1000 -- sieves from 1..M |
38 | local PLAIN= false -- single threaded (true) or using Lanes (false) | 37 | local PLAIN= false -- single threaded (true) or using Lanes (false) |
39 | local SINGLE= false -- cores to use (false / 1..n) | 38 | local SINGLE= 0 -- cores to use (0 / 1..n) |
40 | local TIME= false -- use Lua for the timing | 39 | local TIME= false -- use Lua for the timing |
41 | local PRIO_ODD, PRIO_EVEN -- -3..+3 | 40 | local PRIO_ODD, PRIO_EVEN -- -3..+3 |
42 | 41 | ||
@@ -63,7 +62,7 @@ end | |||
63 | for k,v in pairs( argtable(...) ) do | 62 | for k,v in pairs( argtable(...) ) do |
64 | if k==1 then N= tonumber(v) or HELP() | 63 | if k==1 then N= tonumber(v) or HELP() |
65 | elseif k=="plain" then PLAIN= true | 64 | elseif k=="plain" then PLAIN= true |
66 | elseif k=="single" then SINGLE= v -- true/number | 65 | elseif k=="single" then SINGLE= v -- number |
67 | elseif k=="time" then TIME= true | 66 | elseif k=="time" then TIME= true |
68 | elseif k=="prio" then PRIO_ODD, PRIO_EVEN= prio_param(v) | 67 | elseif k=="prio" then PRIO_ODD, PRIO_EVEN= prio_param(v) |
69 | else HELP() | 68 | else HELP() |
@@ -104,7 +103,7 @@ local function sieve_lane(N,id) | |||
104 | while 1 do | 103 | while 1 do |
105 | local n = g() | 104 | local n = g() |
106 | if n == nil then return end | 105 | if n == nil then return end |
107 | if math.mod(n, p) ~= 0 then coroutine.yield(n) end | 106 | if math.fmod(n, p) ~= 0 then coroutine.yield(n) end |
108 | end | 107 | end |
109 | end) | 108 | end) |
110 | end | 109 | end |
@@ -138,7 +137,7 @@ local f_odd= lanes.gen( "base,coroutine,math,table,io", -- "*" = all | |||
138 | 137 | ||
139 | io.stderr:write( "*** Counting primes 1.."..M.." "..N.." times ***\n\n" ) | 138 | io.stderr:write( "*** Counting primes 1.."..M.." "..N.." times ***\n\n" ) |
140 | 139 | ||
141 | local t0= TIME and os.time() | 140 | local t0= TIME and lanes.now_secs() |
142 | 141 | ||
143 | if PLAIN then | 142 | if PLAIN then |
144 | io.stderr:write( "Plain (no multithreading):\n" ) | 143 | io.stderr:write( "Plain (no multithreading):\n" ) |
@@ -148,9 +147,9 @@ if PLAIN then | |||
148 | assert( type(tmp)=="table" and tmp[1]==2 and tmp[168]==997 ) | 147 | assert( type(tmp)=="table" and tmp[1]==2 and tmp[168]==997 ) |
149 | end | 148 | end |
150 | else | 149 | else |
151 | if SINGLE then | 150 | if SINGLE > 0 then |
152 | io.stderr:write( (tonumber(SINGLE) and SINGLE or 1) .. " core(s):\n" ) | 151 | io.stderr:write( (tonumber(SINGLE) and SINGLE or 1) .. " core(s):\n" ) |
153 | lanes.single(SINGLE) -- limit to N cores (just OS X) | 152 | lanes.set_singlethreaded(SINGLE) -- limit to N cores (just OS X) |
154 | else | 153 | else |
155 | io.stderr:write( "Multi core:\n" ) | 154 | io.stderr:write( "Multi core:\n" ) |
156 | end | 155 | end |
@@ -177,7 +176,7 @@ end | |||
177 | io.stderr:write "\n" | 176 | io.stderr:write "\n" |
178 | 177 | ||
179 | if TIME then | 178 | if TIME then |
180 | local t= os.time() - t0 | 179 | local t= lanes.now_secs() - t0 |
181 | io.stderr:write( "*** TIMING: "..t.." seconds ***\n" ) | 180 | io.stderr:write( "*** TIMING: "..t.." seconds ***\n" ) |
182 | end | 181 | end |
183 | 182 | ||