dennis96411

[Lua 5.1] LuaPerf - Performance Benchmarking Utility

Jan 1st, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. --[[
  2.     LuaPerf - Performance Benchmarking Utility for Lua 5.1
  3.     by dennis96411
  4.     Last updated: 01/06/2016 18:35 EST
  5. ]]
  6.  
  7. local Configurations = {
  8.     Passes = 5000000, --Number of passes for each benchmark
  9.     PrintTimeDelta = 0.5 --The time between each status print
  10. }
  11.  
  12. local GlobalSetup = function() --Function or table
  13.     String = "I Am Testing This!"
  14. end
  15.  
  16. local Benchmarks = {
  17.     MethodInvocation = {
  18.         Function = function()
  19.             String:find("Test")
  20.         end
  21.     },
  22.    
  23.     StringTable = {
  24.         Function = function()
  25.             string.find(String, "Test")
  26.         end
  27.     }
  28. }
  29.  
  30. print(("="):rep(50))
  31. for Name, Benchmark in next, Benchmarks do
  32.     local tick, wait = tick, wait
  33.     local PrintPrefix, Function, Setup = "[" .. Name .. "] ", Benchmark.Function, Benchmark.Setup
  34.     local Environment, TimesPrinted, LastPrintTime, LastPrintPassCount = getfenv(Function), 0, tick(), 0
  35.    
  36.     print(PrintPrefix .. "Setting up benchmarking environment...")
  37.     setfenv(Function, type(GlobalSetup) == "function" and getfenv(GlobalSetup()) or setmetatable(GlobalSetup, {__index = getfenv(0)}))
  38.     if Setup then
  39.         for Key, Value in next, type(Setup) == "function" and getfenv(Setup()) or Setup do
  40.             Environment[Key] = Value
  41.         end
  42.     end
  43.     Benchmark.Results = Benchmark.Results or {PassesPerSecond = {}}
  44.    
  45.     print(PrintPrefix .. "Beginning benchmark.")
  46.     local DesiredPrintTimeDelta, Passes, Results, BeforeBenchmark, WaitTime = Configurations.PrintTimeDelta, Configurations.Passes, Benchmark.Results, tick(), 0
  47.     local PassesPerSecond = Results.PassesPerSecond
  48.     for Pass = 1, Passes do
  49.         Environment.Index = Pass; Function()
  50.         local Time = tick()
  51.         local PrintTimeDelta, PassDelta = Time - LastPrintTime, Pass - LastPrintPassCount
  52.         if PrintTimeDelta >= DesiredPrintTimeDelta then
  53.             local _PassesPerSecond = PassDelta / PrintTimeDelta
  54.             print(PrintPrefix .. "Pass " .. Pass .. "/" .. Passes .. "; pass delta: " .. PassDelta .. ", passes/sec: " .. _PassesPerSecond)
  55.             TimesPrinted, LastPrintTime, LastPrintPassCount, WaitTime = TimesPrinted + 1, Time, Pass, WaitTime + wait()
  56.             PassesPerSecond[TimesPrinted] = _PassesPerSecond
  57.         end
  58.     end
  59.     Results.Time, Results.TimesPrinted = tick() - BeforeBenchmark - WaitTime, math.max(1, TimesPrinted)
  60.     if #Results.PassesPerSecond == 0 then --If we didn't get to print, set the values for the average later
  61.         table.insert(Results.PassesPerSecond, Passes / Results.Time)
  62.     end
  63.     print(PrintPrefix .. "Benchmark completed. Time taken: " .. Benchmark.Results.Time .. " seconds\n")
  64. end
  65.  
  66. print("Benchmarks completed.")
  67. for Name, Benchmark in next, Benchmarks do
  68.     local Sum = 0
  69.     for Index = 1, #Benchmark.Results.PassesPerSecond do
  70.         Sum = Sum + Benchmark.Results.PassesPerSecond[Index]
  71.     end
  72.     print("> " .. Name .. ": " .. Benchmark.Results.Time .. " seconds (avg. passes/sec: " .. Sum / Benchmark.Results.TimesPrinted .. ", sample size: " .. Benchmark.Results.TimesPrinted .. ")")
  73. end
  74. print(("="):rep(50))
Advertisement
Add Comment
Please, Sign In to add comment