diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-04 14:21:05 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-04 14:21:05 +0200 |
| commit | f7c59d94a42cf2a650365aef492be64f62f33a62 (patch) | |
| tree | e9b4c014cff1007323d6a66f250b2670b055afbc | |
| parent | 21d4f6679725eb19f800b134d7207cd0257302d0 (diff) | |
| download | lanes-f7c59d94a42cf2a650365aef492be64f62f33a62.tar.gz lanes-f7c59d94a42cf2a650365aef492be64f62f33a62.tar.bz2 lanes-f7c59d94a42cf2a650365aef492be64f62f33a62.zip | |
tweaks to basic.lua and linda_perf.lua
| -rw-r--r-- | tests/basic.lua | 14 | ||||
| -rw-r--r-- | tests/linda_perf.lua | 103 |
2 files changed, 77 insertions, 40 deletions
diff --git a/tests/basic.lua b/tests/basic.lua index 385e22f..4b4fae6 100644 --- a/tests/basic.lua +++ b/tests/basic.lua | |||
| @@ -154,7 +154,7 @@ PRINT(" "..st) | |||
| 154 | assert( st == "cancelled" ) | 154 | assert( st == "cancelled" ) |
| 155 | 155 | ||
| 156 | -- cancellation of lanes waiting on a linda | 156 | -- cancellation of lanes waiting on a linda |
| 157 | local limited = lanes.linda() | 157 | local limited = lanes.linda("limited") |
| 158 | limited:limit( "key", 1) | 158 | limited:limit( "key", 1) |
| 159 | -- [[################################################ | 159 | -- [[################################################ |
| 160 | limited:send( "key", "hello") -- saturate linda | 160 | limited:send( "key", "hello") -- saturate linda |
| @@ -234,7 +234,7 @@ local chunk= function( linda ) | |||
| 234 | WR( "Lane ends!\n" ) | 234 | WR( "Lane ends!\n" ) |
| 235 | end | 235 | end |
| 236 | 236 | ||
| 237 | local linda= lanes_linda() | 237 | local linda= lanes_linda("communications") |
| 238 | assert( type(linda) == "userdata" ) | 238 | assert( type(linda) == "userdata" ) |
| 239 | -- | 239 | -- |
| 240 | -- ["->"] master -> slave | 240 | -- ["->"] master -> slave |
| @@ -278,10 +278,12 @@ local complex_table = RECEIVE(); WR( type(complex_table).." received\n" ) | |||
| 278 | assert( complex_table[1] == complex_table[3] and complex_table[2] == complex_table[4]) | 278 | assert( complex_table[1] == complex_table[3] and complex_table[2] == complex_table[4]) |
| 279 | WR( table.concat( {complex_table[1][1],complex_table[2][1],complex_table[3][1],complex_table[4][1]},", ")) | 279 | WR( table.concat( {complex_table[1][1],complex_table[2][1],complex_table[3][1],complex_table[4][1]},", ")) |
| 280 | 280 | ||
| 281 | WR("collectgarbage") | ||
| 281 | t = nil | 282 | t = nil |
| 282 | collectgarbage() | 283 | collectgarbage() |
| 283 | -- wait | 284 | -- wait |
| 284 | linda: receive( 1, "wait") | 285 | WR("waiting 1s") |
| 286 | linda:receive( 1, "wait") | ||
| 285 | 287 | ||
| 286 | --############################################################## | 288 | --############################################################## |
| 287 | --############################################################## | 289 | --############################################################## |
| @@ -336,6 +338,7 @@ for _, t in ipairs( stdlib_naming_tests) do | |||
| 336 | assert( f(t[1])[1] ) | 338 | assert( f(t[1])[1] ) |
| 337 | end | 339 | end |
| 338 | 340 | ||
| 341 | WR("collectgarbage") | ||
| 339 | collectgarbage() | 342 | collectgarbage() |
| 340 | 343 | ||
| 341 | --############################################################## | 344 | --############################################################## |
| @@ -361,12 +364,13 @@ local tc= lanes_gen( "io", {gc_cb = gc_cb}, | |||
| 361 | end | 364 | end |
| 362 | ) | 365 | ) |
| 363 | 366 | ||
| 364 | local linda= lanes_linda() | 367 | local linda= lanes_linda("criss cross") |
| 365 | 368 | ||
| 366 | local a,b= tc(linda, "A","B"), tc(linda, "B","A") -- launching two lanes, twisted comms | 369 | local a,b= tc(linda, "A","B"), tc(linda, "B","A") -- launching two lanes, twisted comms |
| 367 | 370 | ||
| 368 | local _= a[1],b[1] -- waits until they are both ready | 371 | local _= a[1],b[1] -- waits until they are both ready |
| 369 | 372 | ||
| 373 | WR("collectgarbage") | ||
| 370 | a, b = nil | 374 | a, b = nil |
| 371 | collectgarbage() | 375 | collectgarbage() |
| 372 | 376 | ||
| @@ -408,7 +412,7 @@ local function chunk2( linda ) | |||
| 408 | linda:send( "up", function() return ":)" end, "ok2" ) | 412 | linda:send( "up", function() return ":)" end, "ok2" ) |
| 409 | end | 413 | end |
| 410 | 414 | ||
| 411 | local linda= lanes.linda() | 415 | local linda= lanes.linda("linda") |
| 412 | local t2= lanes_gen( "debug,string,io", {gc_cb = gc_cb}, chunk2 )(linda) -- prepare & launch | 416 | local t2= lanes_gen( "debug,string,io", {gc_cb = gc_cb}, chunk2 )(linda) -- prepare & launch |
| 413 | linda:send( "down", function(linda) linda:send( "up", "ready!" ) end, | 417 | linda:send( "down", function(linda) linda:send( "up", "ready!" ) end, |
| 414 | "ok" ) | 418 | "ok" ) |
diff --git a/tests/linda_perf.lua b/tests/linda_perf.lua index a170b01..9177852 100644 --- a/tests/linda_perf.lua +++ b/tests/linda_perf.lua | |||
| @@ -1,38 +1,54 @@ | |||
| 1 | local lanes = require "lanes" | 1 | local lanes = require "lanes" |
| 2 | lanes.configure{ with_timers = false} | 2 | lanes.configure{ with_timers = false } |
| 3 | |||
| 4 | -- set TEST1, PREFILL1, FILL1, TEST2, PREFILL2, FILL2 from the command line | ||
| 3 | 5 | ||
| 4 | -- Lua 5.1/5.2 compatibility | 6 | -- Lua 5.1/5.2 compatibility |
| 5 | local table_unpack = unpack or table.unpack | 7 | local table_unpack = unpack or table.unpack |
| 6 | 8 | ||
| 9 | local finalizer = function(err, stk) | ||
| 10 | if err == lanes.cancel_error then | ||
| 11 | -- note that we don't get the cancel_error when running wrapped inside a protected call if it doesn't rethrow it | ||
| 12 | print(" laneBody after cancel" ) | ||
| 13 | elseif err then | ||
| 14 | print(" laneBody error: "..tostring(err)) | ||
| 15 | else | ||
| 16 | print(" laneBody finalized") | ||
| 17 | end | ||
| 18 | end | ||
| 19 | |||
| 7 | -- this lane eats items in the linda one by one | 20 | -- this lane eats items in the linda one by one |
| 8 | local eater = function( l, loop) | 21 | local eater = function( l, loop) |
| 22 | set_finalizer(finalizer) | ||
| 9 | -- wait for start signal | 23 | -- wait for start signal |
| 10 | l:receive( "go") | 24 | l:receive( "go") |
| 11 | -- eat data one by one | 25 | -- eat data one by one |
| 12 | for i = 1, loop do | 26 | for i = 1, loop do |
| 13 | local val, key = l:receive( "key") | 27 | local key, val = l:receive( "key") |
| 14 | --print( val) | 28 | -- print("eater:", val) |
| 15 | end | 29 | end |
| 16 | -- print "loop is over" | 30 | -- print "loop is over" |
| 17 | key, val = l:receive( "done") | 31 | key, val = l:receive( "done") |
| 18 | -- print( val) | 32 | print("eater: done ("..val..")") |
| 19 | end | 33 | end |
| 20 | 34 | ||
| 21 | -- this lane eats items in the linda in batches | 35 | -- this lane eats items in the linda in batches |
| 22 | local batched = function( l, loop, batch) | 36 | local gobbler = function( l, loop, batch) |
| 37 | set_finalizer(finalizer) | ||
| 23 | -- wait for start signal | 38 | -- wait for start signal |
| 24 | l:receive( "go") | 39 | l:receive( "go") |
| 25 | -- eat data in batches | 40 | -- eat data in batches |
| 26 | for i = 1, loop/batch do | 41 | for i = 1, loop/batch do |
| 27 | l:receive( l.batched, "key", batch) | 42 | l:receive( l.batched, "key", batch) |
| 43 | -- print("gobbler:", batch) | ||
| 28 | end | 44 | end |
| 29 | print "loop is over" | 45 | print "loop is over" |
| 30 | key, val = l:receive( "done") | 46 | key, val = l:receive( "done") |
| 31 | print( val) | 47 | print("gobbler: done ("..val..")") |
| 32 | end | 48 | end |
| 33 | 49 | ||
| 34 | local lane_eater_gen = lanes.gen( "*", {priority = 3}, eater) | 50 | local lane_eater_gen = lanes.gen( "*", {priority = 3}, eater) |
| 35 | local lane_batched_gen = lanes.gen( "*", {priority = 3}, batched) | 51 | local lane_gobbler_gen = lanes.gen( "*", {priority = 3}, gobbler) |
| 36 | 52 | ||
| 37 | -- main thread writes data while a lane reads it | 53 | -- main thread writes data while a lane reads it |
| 38 | local function ziva( preloop, loop, batch) | 54 | local function ziva( preloop, loop, batch) |
| @@ -46,7 +62,7 @@ local function ziva( preloop, loop, batch) | |||
| 46 | print( "stored " .. l:count( "key") .. " items in the linda before starting consumer lane") | 62 | print( "stored " .. l:count( "key") .. " items in the linda before starting consumer lane") |
| 47 | if batch > 0 then | 63 | if batch > 0 then |
| 48 | if l.batched then | 64 | if l.batched then |
| 49 | lane = lane_batched_gen( l, top, batch) | 65 | lane = lane_gobbler_gen( l, top, batch) |
| 50 | else | 66 | else |
| 51 | print "no batch support in this version of Lanes" | 67 | print "no batch support in this version of Lanes" |
| 52 | lane = lane_eater_gen( l, top) | 68 | lane = lane_eater_gen( l, top) |
| @@ -63,7 +79,9 @@ local function ziva( preloop, loop, batch) | |||
| 63 | for i = 1, batch do | 79 | for i = 1, batch do |
| 64 | table.insert( batch_values, i) | 80 | table.insert( batch_values, i) |
| 65 | end | 81 | end |
| 82 | local batch_send_log = "main: sending "..batch.." values" | ||
| 66 | local batch_send = function() | 83 | local batch_send = function() |
| 84 | -- print(batch_send_log) | ||
| 67 | l:send( "key", table_unpack( batch_values)) | 85 | l:send( "key", table_unpack( batch_values)) |
| 68 | end | 86 | end |
| 69 | if loop > preloop then | 87 | if loop > preloop then |
| @@ -76,23 +94,29 @@ local function ziva( preloop, loop, batch) | |||
| 76 | return lanes.now_secs() - t1 | 94 | return lanes.now_secs() - t1 |
| 77 | end | 95 | end |
| 78 | 96 | ||
| 97 | TEST1 = TEST1 or 1000 | ||
| 98 | PREFILL1 = PREFILL1 or 10000 | ||
| 99 | FILL1 = FILL1 or 2000000 | ||
| 100 | |||
| 79 | local tests1 = | 101 | local tests1 = |
| 80 | { | 102 | { |
| 81 | { 10000, 2000000, 0}, | 103 | { PREFILL1, FILL1, 0}, |
| 82 | { 10000, 2000000, 1}, | 104 | { PREFILL1, FILL1, 1}, |
| 83 | { 10000, 2000000, 2}, | 105 | { PREFILL1, FILL1, 2}, |
| 84 | { 10000, 2000000, 3}, | 106 | { PREFILL1, FILL1, 3}, |
| 85 | { 10000, 2000000, 5}, | 107 | { PREFILL1, FILL1, 5}, |
| 86 | { 10000, 2000000, 8}, | 108 | { PREFILL1, FILL1, 8}, |
| 87 | { 10000, 2000000, 13}, | 109 | { PREFILL1, FILL1, 13}, |
| 88 | { 10000, 2000000, 21}, | 110 | { PREFILL1, FILL1, 21}, |
| 89 | { 10000, 2000000, 44}, | 111 | { PREFILL1, FILL1, 44}, |
| 90 | } | 112 | } |
| 91 | print "############################################\ntests #1" | 113 | print "############################################ tests #1" |
| 92 | for k, v in pairs( tests1) do | 114 | for i, v in ipairs( tests1) do |
| 115 | if i > TEST1 then break end | ||
| 93 | local pre, loop, batch = v[1], v[2], v[3] | 116 | local pre, loop, batch = v[1], v[2], v[3] |
| 94 | print( "testing", pre, loop, batch) | 117 | print("-------------------------------------------------\n") |
| 95 | print( pre, loop, batch, "duration = " .. ziva( pre, loop, batch) .. "\n") | 118 | print("START", "prefill="..pre, "fill="..loop, "batch="..batch) |
| 119 | print("DURATION = " .. ziva( pre, loop, batch) .. "\n") | ||
| 96 | end | 120 | end |
| 97 | 121 | ||
| 98 | --[[ | 122 | --[[ |
| @@ -169,11 +193,15 @@ local function ziva2( preloop, loop, batch) | |||
| 169 | end | 193 | end |
| 170 | -- here, we have preloop elements still waiting inside the linda | 194 | -- here, we have preloop elements still waiting inside the linda |
| 171 | for i = 1, preloop, step do | 195 | for i = 1, preloop, step do |
| 172 | batch_read() | 196 | batch_read() |
| 173 | end | 197 | end |
| 174 | return lanes.now_secs() - t1 | 198 | return lanes.now_secs() - t1 |
| 175 | end | 199 | end |
| 176 | 200 | ||
| 201 | TEST2 = TEST2 or 1000 | ||
| 202 | PREFILL2 = PREFILL2 or 0 | ||
| 203 | FILL2 = FILL2 or 4000000 | ||
| 204 | |||
| 177 | local tests2 = | 205 | local tests2 = |
| 178 | { | 206 | { |
| 179 | -- prefill, then consume everything | 207 | -- prefill, then consume everything |
| @@ -189,20 +217,25 @@ local tests2 = | |||
| 189 | { 4000000, 0, 44}, | 217 | { 4000000, 0, 44}, |
| 190 | --]] | 218 | --]] |
| 191 | -- alternatively fill and consume | 219 | -- alternatively fill and consume |
| 192 | { 0, 4000000}, | 220 | { PREFILL2, FILL2}, |
| 193 | { 0, 4000000, 1}, | 221 | { PREFILL2, FILL2, 1}, |
| 194 | { 0, 4000000, 2}, | 222 | { PREFILL2, FILL2, 2}, |
| 195 | { 0, 4000000, 3}, | 223 | { PREFILL2, FILL2, 3}, |
| 196 | { 0, 4000000, 5}, | 224 | { PREFILL2, FILL2, 5}, |
| 197 | { 0, 4000000, 8}, | 225 | { PREFILL2, FILL2, 8}, |
| 198 | { 0, 4000000, 13}, | 226 | { PREFILL2, FILL2, 13}, |
| 199 | { 0, 4000000, 21}, | 227 | { PREFILL2, FILL2, 21}, |
| 200 | { 0, 4000000, 44}, | 228 | { PREFILL2, FILL2, 44}, |
| 201 | } | 229 | } |
| 202 | 230 | ||
| 203 | print "\n############################################\ntests #2" | 231 | print "############################################ tests #2" |
| 204 | for k, v in pairs( tests2) do | 232 | for i, v in ipairs( tests2) do |
| 233 | if i > TEST2 then break end | ||
| 205 | local pre, loop, batch = v[1], v[2], v[3] | 234 | local pre, loop, batch = v[1], v[2], v[3] |
| 206 | print( "testing", pre, loop, batch) | 235 | print("-------------------------------------------------\n") |
| 207 | print( pre, loop, batch, "duration = " .. ziva2( pre, loop, batch) .. "\n") | 236 | print("START", "prefill="..pre, "fill="..loop, "batch="..(batch or "no")) |
| 237 | print("DURATION = " .. ziva2( pre, loop, batch) .. "\n") | ||
| 208 | end | 238 | end |
| 239 | |||
| 240 | print "############################################" | ||
| 241 | print "THE END" \ No newline at end of file | ||
