From f7c59d94a42cf2a650365aef492be64f62f33a62 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 4 Apr 2024 14:21:05 +0200 Subject: tweaks to basic.lua and linda_perf.lua --- tests/basic.lua | 14 ++++--- 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) assert( st == "cancelled" ) -- cancellation of lanes waiting on a linda -local limited = lanes.linda() +local limited = lanes.linda("limited") limited:limit( "key", 1) -- [[################################################ limited:send( "key", "hello") -- saturate linda @@ -234,7 +234,7 @@ local chunk= function( linda ) WR( "Lane ends!\n" ) end -local linda= lanes_linda() +local linda= lanes_linda("communications") assert( type(linda) == "userdata" ) -- -- ["->"] master -> slave @@ -278,10 +278,12 @@ local complex_table = RECEIVE(); WR( type(complex_table).." received\n" ) assert( complex_table[1] == complex_table[3] and complex_table[2] == complex_table[4]) WR( table.concat( {complex_table[1][1],complex_table[2][1],complex_table[3][1],complex_table[4][1]},", ")) +WR("collectgarbage") t = nil collectgarbage() -- wait -linda: receive( 1, "wait") +WR("waiting 1s") +linda:receive( 1, "wait") --############################################################## --############################################################## @@ -336,6 +338,7 @@ for _, t in ipairs( stdlib_naming_tests) do assert( f(t[1])[1] ) end +WR("collectgarbage") collectgarbage() --############################################################## @@ -361,12 +364,13 @@ local tc= lanes_gen( "io", {gc_cb = gc_cb}, end ) -local linda= lanes_linda() +local linda= lanes_linda("criss cross") local a,b= tc(linda, "A","B"), tc(linda, "B","A") -- launching two lanes, twisted comms local _= a[1],b[1] -- waits until they are both ready +WR("collectgarbage") a, b = nil collectgarbage() @@ -408,7 +412,7 @@ local function chunk2( linda ) linda:send( "up", function() return ":)" end, "ok2" ) end -local linda= lanes.linda() +local linda= lanes.linda("linda") local t2= lanes_gen( "debug,string,io", {gc_cb = gc_cb}, chunk2 )(linda) -- prepare & launch linda:send( "down", function(linda) linda:send( "up", "ready!" ) end, "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 @@ local lanes = require "lanes" -lanes.configure{ with_timers = false} +lanes.configure{ with_timers = false } + +-- set TEST1, PREFILL1, FILL1, TEST2, PREFILL2, FILL2 from the command line -- Lua 5.1/5.2 compatibility local table_unpack = unpack or table.unpack +local finalizer = function(err, stk) + if err == lanes.cancel_error then + -- note that we don't get the cancel_error when running wrapped inside a protected call if it doesn't rethrow it + print(" laneBody after cancel" ) + elseif err then + print(" laneBody error: "..tostring(err)) + else + print(" laneBody finalized") + end +end + -- this lane eats items in the linda one by one local eater = function( l, loop) + set_finalizer(finalizer) -- wait for start signal l:receive( "go") -- eat data one by one for i = 1, loop do - local val, key = l:receive( "key") - --print( val) + local key, val = l:receive( "key") + -- print("eater:", val) end -- print "loop is over" key, val = l:receive( "done") - -- print( val) + print("eater: done ("..val..")") end -- this lane eats items in the linda in batches -local batched = function( l, loop, batch) +local gobbler = function( l, loop, batch) + set_finalizer(finalizer) -- wait for start signal l:receive( "go") -- eat data in batches for i = 1, loop/batch do l:receive( l.batched, "key", batch) + -- print("gobbler:", batch) end print "loop is over" key, val = l:receive( "done") - print( val) + print("gobbler: done ("..val..")") end local lane_eater_gen = lanes.gen( "*", {priority = 3}, eater) -local lane_batched_gen = lanes.gen( "*", {priority = 3}, batched) +local lane_gobbler_gen = lanes.gen( "*", {priority = 3}, gobbler) -- main thread writes data while a lane reads it local function ziva( preloop, loop, batch) @@ -46,7 +62,7 @@ local function ziva( preloop, loop, batch) print( "stored " .. l:count( "key") .. " items in the linda before starting consumer lane") if batch > 0 then if l.batched then - lane = lane_batched_gen( l, top, batch) + lane = lane_gobbler_gen( l, top, batch) else print "no batch support in this version of Lanes" lane = lane_eater_gen( l, top) @@ -63,7 +79,9 @@ local function ziva( preloop, loop, batch) for i = 1, batch do table.insert( batch_values, i) end + local batch_send_log = "main: sending "..batch.." values" local batch_send = function() + -- print(batch_send_log) l:send( "key", table_unpack( batch_values)) end if loop > preloop then @@ -76,23 +94,29 @@ local function ziva( preloop, loop, batch) return lanes.now_secs() - t1 end +TEST1 = TEST1 or 1000 +PREFILL1 = PREFILL1 or 10000 +FILL1 = FILL1 or 2000000 + local tests1 = { - { 10000, 2000000, 0}, - { 10000, 2000000, 1}, - { 10000, 2000000, 2}, - { 10000, 2000000, 3}, - { 10000, 2000000, 5}, - { 10000, 2000000, 8}, - { 10000, 2000000, 13}, - { 10000, 2000000, 21}, - { 10000, 2000000, 44}, + { PREFILL1, FILL1, 0}, + { PREFILL1, FILL1, 1}, + { PREFILL1, FILL1, 2}, + { PREFILL1, FILL1, 3}, + { PREFILL1, FILL1, 5}, + { PREFILL1, FILL1, 8}, + { PREFILL1, FILL1, 13}, + { PREFILL1, FILL1, 21}, + { PREFILL1, FILL1, 44}, } -print "############################################\ntests #1" -for k, v in pairs( tests1) do +print "############################################ tests #1" +for i, v in ipairs( tests1) do + if i > TEST1 then break end local pre, loop, batch = v[1], v[2], v[3] - print( "testing", pre, loop, batch) - print( pre, loop, batch, "duration = " .. ziva( pre, loop, batch) .. "\n") + print("-------------------------------------------------\n") + print("START", "prefill="..pre, "fill="..loop, "batch="..batch) + print("DURATION = " .. ziva( pre, loop, batch) .. "\n") end --[[ @@ -169,11 +193,15 @@ local function ziva2( preloop, loop, batch) end -- here, we have preloop elements still waiting inside the linda for i = 1, preloop, step do - batch_read() + batch_read() end return lanes.now_secs() - t1 end +TEST2 = TEST2 or 1000 +PREFILL2 = PREFILL2 or 0 +FILL2 = FILL2 or 4000000 + local tests2 = { -- prefill, then consume everything @@ -189,20 +217,25 @@ local tests2 = { 4000000, 0, 44}, --]] -- alternatively fill and consume - { 0, 4000000}, - { 0, 4000000, 1}, - { 0, 4000000, 2}, - { 0, 4000000, 3}, - { 0, 4000000, 5}, - { 0, 4000000, 8}, - { 0, 4000000, 13}, - { 0, 4000000, 21}, - { 0, 4000000, 44}, + { PREFILL2, FILL2}, + { PREFILL2, FILL2, 1}, + { PREFILL2, FILL2, 2}, + { PREFILL2, FILL2, 3}, + { PREFILL2, FILL2, 5}, + { PREFILL2, FILL2, 8}, + { PREFILL2, FILL2, 13}, + { PREFILL2, FILL2, 21}, + { PREFILL2, FILL2, 44}, } -print "\n############################################\ntests #2" -for k, v in pairs( tests2) do +print "############################################ tests #2" +for i, v in ipairs( tests2) do + if i > TEST2 then break end local pre, loop, batch = v[1], v[2], v[3] - print( "testing", pre, loop, batch) - print( pre, loop, batch, "duration = " .. ziva2( pre, loop, batch) .. "\n") + print("-------------------------------------------------\n") + print("START", "prefill="..pre, "fill="..loop, "batch="..(batch or "no")) + print("DURATION = " .. ziva2( pre, loop, batch) .. "\n") end + +print "############################################" +print "THE END" \ No newline at end of file -- cgit v1.2.3-55-g6feb