Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Debugging Tools: lead comment: --[[ -> feature disabled ---[[ -> feature enabled
- ---[=[ Name, Version, and Description management.
- Name = `Ali's Performance Comparer`
- Version = "0.6"
- Desc = [[
- Personal Version
- ]] .. `{if bit32.btest(ll.GetObjectPermMask(MASK_EVERYONE), PERM_COPY) then "Feel free to take a copy" else "" }`
- ll.SetObjectName( `{Name}: {Version}` ); ll.SetObjectDesc( Desc ); ll.SetText( `{Name}: {Version}\n \n{Desc}`, vector(0.5,0.5,1), 1 )
- --]=]
- --- Generally useful functions. Always enabled. Adds nothing to compiled code unless called.
- function concat( ... ) -- Return stringized values as a comma separated sting. Avoid using tables so explicit nils are preserved.
- local count = select("#", ...); if count == 0 then return "" end--if
- local result = tostring(select(1, ...)); for i = 2, count do result ..= `, {tostring(select(i, ...))}` end--for
- return result
- end--concat
- local rawprint = print -- always enabled just in case some code is using rawprint to bypass the redirect below
- ---[[ Redirect print Debug channel to avoid spamming chat. Must be enabled if DBG block below is enabled.
- local function print(...) ll.Shout( DEBUG_CHANNEL, concat(...) ) end--print
- --]=]
- ---[[ Send DBG messages to the Debug Channel to sync with error messages and avoid spamming Nearby Chat
- local DBG_buffer = ""
- local function rawDBG( msg ) ll.Shout( DEBUG_CHANNEL, msg ) end--rawDBG this function exist to allow redirecton of all DBG output to a single destination in one place
- local function DBGb( ... ) DBG_buffer ..= concat(...).."; "; end--DBGb (Buffer message for output wiht next DBG or DBGs call, allows multiple messages on a line.)
- local function DBGs( ... ) local n,l = debug.info(2, "nl"); rawDBG( `{ll.GetTimestamp()}: {l} {n}: {DBG_buffer} {concat(...)}` );DBG_buffer=""; ll.Sleep(0.2) end--DBG (with timestamp) & Delay
- local function DBG( ... ) local n,l = debug.info(2, "nl"); rawDBG( `{ll.GetTimestamp()}: {l} {n}: {DBG_buffer} {concat(...)}` ); DBG_buffer=""; end--DBG (with timestamp)
- local function DBG_dump( label, val, lvl ) --dump any data structure
- lvl = lvl or 0
- local indent = string.rep( " ", lvl )
- local leader = indent .. `({typeof(label)}) {tostring(label)}: `
- if lvl == 0 then local n,l = debug.info(2, "nl"); leader = `{ll.GetTimestamp()}: {l} {n}:\n` .. leader end--if (with timestamp)
- local typ = typeof( val )
- if typ == "table" then
- rawDBG( `{leader}({typ}) {tostring(val)} `.."{" )
- lvl += 1
- for k,v in pairs(val) do DBG_dump( k, v, lvl ) end--for
- rawDBG( indent .. "}" )
- else rawDBG( `{leader}({typ}) {tostring(val)}` )
- end--if
- end--DBG_dump
- function DBG_dump_list( label, list ) local n,l = debug.info(2, "nl"); rawDBG( `{ll.GetTimestamp()}: {l} {n}: {label}: {concat(unpack(list))}` ) end--dump a list (1D array) on one line.
- function DBG_err( ... )
- local n,l = debug.info(2, "nl")
- rawDBG( `{ll.GetTimestamp()}: {l} {n}: Error Caught by xpcall`, ... )
- rawDBG( debug.traceback( "Stack Trace", 2) )
- end--DBG_err
- --[=[]] --Switch to do-nothing dummy versions when above is disabled.
- local function rawDBG() end--rawDBG disabled
- local DBG, DBG_dump, DBG_dump_list = rawDBG, rawDBG, rawDBG --Other DBG functions disabled.
- local DBG_err = print --don't disable error reporting entirely.
- --]=]
- -->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code start <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- ----Constants/Parameters
- --usually a few large samples are better than many smaller ones. 10 milliseconds per sample is a good goal
- local sample_size = 650 -- Adjust as needed to keep sample times between the clock granularity factor and the frame break threshold.
- local sample_count = 50 -- Balance between the time you want to spend and needed precision of the measure. 10 to 100 is probably good for most uses.
- local script_frame_time = 1/45 -- nominal time between script frames. If a sample's time exceeds this value, frame break artifacts are present.
- local clock_granularity = 100e-6 -- 100 microseconds/tick
- local test_period_min, test_period_max = clock_granularity * 0.75, clock_granularity * 1.25 -- Script slice is supposedly 300. Aiming for exactly (DBG 1) 2 ticks per slice.
- --local frame_break_threshold = script_frame_time * 0.9 --short frame breaks are possible. Low value passes fewer short breaks, Hi value allows lager sample_size. Balance for best RSD @ sample_count.
- --local rejection_limit = sample_count * 2 --When trying to maximize sample size, rejections may pile up.
- ----Variables
- local start, finish, overhead --os.clock values for a given test loop.
- --local test_start
- local times --an array of arrays for holding sample times.
- local rejections -- a count of samples rejected due to frame break.
- ----Functions
- local function fmt(v, m, d) -- return value scaled by 10^m to d decimal places.
- d = d or 6; m = m or 6 -- default to 6 places scaled by a million (for microseconds)
- return string.format( `%.{d}f`, v*10^m )
- end--fmt
- --using Sample Standard Deviantion vs Population Standard Deviation should give more consistent results if we vary our total samples.
- --contracted name because calling min_max_mean_and_normalized_sample_standard_deviaton( list ) is too freakin' much *grins*.
- local function MMMSSD( list ) -- return the Min, Max, Mean and the Normalized Sample Standard Deviation of the values in list.
- local len = #list
- local min, max, mean = math.huge, -math.huge, 0
- for i = 1, len do
- local v = list[i]
- if v < min then min = v end--if
- if v > max then max = v end--if
- mean += v
- end--for
- mean /= len
- local ssd = 0
- for i = 1, len do
- ssd += ( (list[i]-mean) / mean ) ^ 2
- end--for
- ssd /= len-1 --Change the divisor to just len if you want the Population SDev.
- return min, max, mean, ssd
- end--MSSD
- local function report( label ) --do sanity checks, adjust times, and report results
- print( `\n{label} report:`)
- for sample = 1, sample_count do times[sample] /= sample_size end--for
- local min, max, mean, ssd = MMMSSD( times )
- local lo_d, hi_d = mean-min, max-mean; local max_dev = if lo_d > hi_d then lo_d else hi_d
- print( `Min= {fmt(min)} μs, Max= {fmt(max)} μs, Mean= {fmt(mean)} μs, NSSD= {fmt(ssd, 2)} %, MD= {fmt(max_dev)} μs, RSD= {math.floor( -math.log10(ssd) ) + 1}` )
- return min, max, mean, ssd, max_dev
- end--report
- local tests = {
- { name = "Overhead", test = function() -- This first, do nothing, loop established a base time to subtract from test times to account for loop overhead.
- local count, clk
- repeat
- ll.Sleep(0.01)
- count, clk = 0, os.clock()
- start = clk; while start == clk do start = os.clock() end--while --wait for a clock tick
- local finish_min, finish_max = start + test_period_min, start + test_period_max
- repeat
- --do nothing
- count +=1;
- clk = os.clock(); until clk > finish_min
- if clk > finish_max then print( `test_period_exceeded: period= {fmt(clk-start)}μs count= {count}. Retrying.`) end-- if
- until clk < finish_max
- return (clk-start)/count
- end}, -- Overhead
- { name = "Null", test = function() --Null Test. This should return stats with near zero time estimates
- local count, clk
- repeat
- ll.Sleep(0.01)
- count, clk = 0, os.clock()
- start = clk; while start == clk do start = os.clock() end--while --wait for a clock tick
- local finish_min, finish_max = start + test_period_min, start + test_period_max
- repeat
- --do nothing
- count +=1;
- clk = os.clock(); until clk > finish_min
- if clk > finish_max then print( `test_period_exceeded: period= {fmt(clk-start)}μs count= {count}. Retrying.`) end-- if
- until clk < finish_max
- return (clk-start)/count - overhead
- end}, --Null
- { name = "DBG", test = function() --Debug Null Test.
- local count, clk
- repeat
- ll.Sleep(0.01)
- count, clk = 0, os.clock()
- start = clk; while start == clk do start = os.clock() end--while --wait for a clock tick
- local finish_min, finish_max = start + test_period_min, start + test_period_max
- repeat
- --do nothing
- count +=1;
- clk = os.clock(); until clk > finish_min
- if clk > finish_max then print( `test_period_exceeded: period= {fmt(clk-start)}μs count= {count}. Retrying.`) end-- if
- until clk < finish_max
- return (clk-start)/count
- end}, --dbg
- { name = "Test 1", test = function() --first actual performance test
- local count, clk
- repeat
- ll.Sleep(0.01)
- count, clk = 0, os.clock()
- start = clk; while start == clk do start = os.clock() end--while --wait for a clock tick
- local finish_min, finish_max = start + test_period_min, start + test_period_max
- repeat
- --Start code to test
- _ = ll.AxisAngle2Rot( vector(0,0,1), math.random()*TWO_PI )
- --End code to test
- count +=1;
- clk = os.clock(); until clk > finish_min
- if clk > finish_max then print( `test_period_exceeded: period= {fmt(clk-start)}μs count= {count}. Retrying.`) end-- if
- until clk < finish_max
- return (clk-start)/count - overhead
- end}, -- Test 1
- { name = "Test 2", test = function() --second performance test
- local count, clk
- repeat
- ll.Sleep(0.01)
- count, clk = 0, os.clock()
- start = clk; while start == clk do start = os.clock() end--while --wait for a clock tick
- local finish_min, finish_max = start + test_period_min, start + test_period_max
- repeat
- --Start code to test
- local t = math.random()*TWO_PI/2; _ = quaternion( 0,0, math.sin(t), math.cos(t) )
- --End code to test
- count +=1;
- clk = os.clock(); until clk > finish_min
- if clk > finish_max then print( `test_period_exceeded: period= {fmt(clk-start)}μs count= {count}. Retrying.`) end-- if
- until clk < finish_max
- return (clk-start)/count - overhead
- end}, -- Test 2
- }
- local function do_test( num )
- times = {}
- print( `\n\nStarting Test: {tests[num].name}`)
- local sample = 1; while sample <= sample_count do
- times[sample] = tests[num].test()
- ll.SetText(`{tests[num].name} successful samples= {sample}.`, vector(0.5,1,0.5), 1 )
- sample += 1
- end--while
- end--do_test
- ----Do it
- test_start = os.clock();
- print(
- `\n\n ----Testing with {sample_count} samples.----\n`..
- "NSSD -> Normalized Sample Standard Deviation, MD -> Max Deviation, RSD -> estimated Reliable Significant Digits of the mean based on NSSD."
- )
- do_test(1); _, _, overhead = report( tests[1].name ) -- establish overhead
- print(`overhead = {fmt(overhead)}`)
- for num = 2, #tests do do_test(num); report( tests[num].name ) end--for -- perform tests
- ll.SetText( `{Name}: {Version}\n \n{Desc}`, vector(0.5,0.5,1), 1 )
Advertisement
Add Comment
Please, Sign In to add comment