aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/cancel.lua93
-rw-r--r--tests/linda_perf.lua8
-rw-r--r--tests/perftest.lua17
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 @@
1local lanes = require "lanes" .configure{ with_timers = false}
2
3local linda = lanes.linda()
4
5local 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"
25end
26
27local 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
44end
45
46--####################################################################
47
48print "####################################################################\nbegin soft cancel test\n"
49h = lanes.gen("*", protectedBody)( 0.666)
50print "wait 3s"
51linda:receive( 3, "yeah")
52
53-- soft cancel
54print "soft cancel with awakening"
55h:cancel( -1, true)
56
57-- wait 10s: the lane will interrupt its loop and print the exit message
58print "wait 2s"
59linda:receive( 2, "yeah")
60
61--####################################################################
62
63print "\n\n####################################################################\nbegin hard cancel test\n"
64h = lanes.gen("*", protectedBody)()
65
66-- wait 3s before cancelling the lane
67print "wait 3s"
68linda: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
71print "hard cancel (always awakens)"
72h:cancel()
73
74print "wait 5s"
75linda:receive( 5, "yeah")
76
77--####################################################################
78
79print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n"
80h = lanes.gen("*", laneBody)()
81
82-- wait 3s before cancelling the lane
83print "wait 3s"
84linda: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
87print "hard cancel (always awakens)"
88h:cancel()
89
90print "wait 5s"
91linda:receive( 5, "yeah")
92
93print "\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
63end 63end
64 64
65local tests = 65local 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
169end 169end
170 170
171local tests2 = 171local 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 @@
27local MSYS= os.getenv("OSTYPE")=="msys" 27local MSYS= os.getenv("OSTYPE")=="msys"
28 28
29 29
30local lanes = require "lanes" 30local lanes = require "lanes".configure{ with_timers = false}
31lanes.configure()
32 31
33local m= require "argtable" 32local m= require "argtable"
34local argtable= assert( m.argtable ) 33local argtable= assert( m.argtable )
@@ -36,7 +35,7 @@ local argtable= assert( m.argtable )
36local N= 1000 -- threads/loops to use 35local N= 1000 -- threads/loops to use
37local M= 1000 -- sieves from 1..M 36local M= 1000 -- sieves from 1..M
38local PLAIN= false -- single threaded (true) or using Lanes (false) 37local PLAIN= false -- single threaded (true) or using Lanes (false)
39local SINGLE= false -- cores to use (false / 1..n) 38local SINGLE= 0 -- cores to use (0 / 1..n)
40local TIME= false -- use Lua for the timing 39local TIME= false -- use Lua for the timing
41local PRIO_ODD, PRIO_EVEN -- -3..+3 40local PRIO_ODD, PRIO_EVEN -- -3..+3
42 41
@@ -63,7 +62,7 @@ end
63for k,v in pairs( argtable(...) ) do 62for 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
139io.stderr:write( "*** Counting primes 1.."..M.." "..N.." times ***\n\n" ) 138io.stderr:write( "*** Counting primes 1.."..M.." "..N.." times ***\n\n" )
140 139
141local t0= TIME and os.time() 140local t0= TIME and lanes.now_secs()
142 141
143if PLAIN then 142if 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
150else 149else
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
177io.stderr:write "\n" 176io.stderr:write "\n"
178 177
179if TIME then 178if 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" )
182end 181end
183 182