Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- LuaPerf - Performance Benchmarking Utility for Lua 5.1
- by dennis96411
- Last updated: 01/06/2016 18:35 EST
- ]]
- local Configurations = {
- Passes = 5000000, --Number of passes for each benchmark
- PrintTimeDelta = 0.5 --The time between each status print
- }
- local GlobalSetup = function() --Function or table
- String = "I Am Testing This!"
- end
- local Benchmarks = {
- MethodInvocation = {
- Function = function()
- String:find("Test")
- end
- },
- StringTable = {
- Function = function()
- string.find(String, "Test")
- end
- }
- }
- print(("="):rep(50))
- for Name, Benchmark in next, Benchmarks do
- local tick, wait = tick, wait
- local PrintPrefix, Function, Setup = "[" .. Name .. "] ", Benchmark.Function, Benchmark.Setup
- local Environment, TimesPrinted, LastPrintTime, LastPrintPassCount = getfenv(Function), 0, tick(), 0
- print(PrintPrefix .. "Setting up benchmarking environment...")
- setfenv(Function, type(GlobalSetup) == "function" and getfenv(GlobalSetup()) or setmetatable(GlobalSetup, {__index = getfenv(0)}))
- if Setup then
- for Key, Value in next, type(Setup) == "function" and getfenv(Setup()) or Setup do
- Environment[Key] = Value
- end
- end
- Benchmark.Results = Benchmark.Results or {PassesPerSecond = {}}
- print(PrintPrefix .. "Beginning benchmark.")
- local DesiredPrintTimeDelta, Passes, Results, BeforeBenchmark, WaitTime = Configurations.PrintTimeDelta, Configurations.Passes, Benchmark.Results, tick(), 0
- local PassesPerSecond = Results.PassesPerSecond
- for Pass = 1, Passes do
- Environment.Index = Pass; Function()
- local Time = tick()
- local PrintTimeDelta, PassDelta = Time - LastPrintTime, Pass - LastPrintPassCount
- if PrintTimeDelta >= DesiredPrintTimeDelta then
- local _PassesPerSecond = PassDelta / PrintTimeDelta
- print(PrintPrefix .. "Pass " .. Pass .. "/" .. Passes .. "; pass delta: " .. PassDelta .. ", passes/sec: " .. _PassesPerSecond)
- TimesPrinted, LastPrintTime, LastPrintPassCount, WaitTime = TimesPrinted + 1, Time, Pass, WaitTime + wait()
- PassesPerSecond[TimesPrinted] = _PassesPerSecond
- end
- end
- Results.Time, Results.TimesPrinted = tick() - BeforeBenchmark - WaitTime, math.max(1, TimesPrinted)
- if #Results.PassesPerSecond == 0 then --If we didn't get to print, set the values for the average later
- table.insert(Results.PassesPerSecond, Passes / Results.Time)
- end
- print(PrintPrefix .. "Benchmark completed. Time taken: " .. Benchmark.Results.Time .. " seconds\n")
- end
- print("Benchmarks completed.")
- for Name, Benchmark in next, Benchmarks do
- local Sum = 0
- for Index = 1, #Benchmark.Results.PassesPerSecond do
- Sum = Sum + Benchmark.Results.PassesPerSecond[Index]
- end
- print("> " .. Name .. ": " .. Benchmark.Results.Time .. " seconds (avg. passes/sec: " .. Sum / Benchmark.Results.TimesPrinted .. ", sample size: " .. Benchmark.Results.TimesPrinted .. ")")
- end
- print(("="):rep(50))
Advertisement
Add Comment
Please, Sign In to add comment