Skip to content

Commit

Permalink
Small fixes to microbenchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed Apr 26, 2016
1 parent 73849a8 commit 05447b6
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/resty/template/microbenchmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if not ok then
end

local function run(iterations)
local total, print, parse, compile, iterations, clock, format = 0, ngx and ngx.say or print, template.parse, template.compile, iterations or 1000, os.clock, string.format
local gc, total, print, parse, compile, iterations, clock, format = collectgarbage, 0, ngx and ngx.say or print, template.parse, template.compile, iterations or 1000, os.clock, string.format
local view = [[
<ul>
{% for _, v in ipairs(context) do %}
Expand All @@ -16,6 +16,9 @@ local function run(iterations)

print(format("Running %d iterations in each test", iterations))

gc()
gc()

local x = clock()
for _ = 1, iterations do
parse(view, true)
Expand All @@ -24,6 +27,9 @@ local function run(iterations)
print(format(" Parsing Time: %.6f", z))
total = total + z

gc()
gc()

x = clock()
for _ = 1, iterations do
compile(view, nil, true)
Expand All @@ -35,18 +41,24 @@ local function run(iterations)

compile(view, nil, true)

gc()
gc()

x = clock()
for _ = 1, iterations do
compile(view, 1, true)
end
z = clock() - x
print(format("Compilation Time: %.6f (template cached)", z))
print(format("Compilation Time: %.6f (template, cached)", z))
total = total + z

local context = { "Emma", "James", "Nicholas", "Mary" }

template.cache = {}

gc()
gc()

x = clock()
for _ = 1, iterations do
compile(view, 1, true)(context)
Expand All @@ -59,12 +71,15 @@ local function run(iterations)
template.cache = {}
compile(view, 1, true)

gc()
gc()

x = clock()
for _ = 1, iterations do
compile(view, 1, true)(context)
end
z = clock() - x
print(format(" Execution Time: %.6f (same template cached)", z))
print(format(" Execution Time: %.6f (same template, cached)", z))
total = total + z

template.cache = {}
Expand All @@ -74,6 +89,9 @@ local function run(iterations)
views[i] = "<h1>Iteration " .. i .. "</h1>\n" .. view
end

gc()
gc()

x = clock()
for i = 1, iterations do
compile(views[i], i, true)(context)
Expand All @@ -82,21 +100,28 @@ local function run(iterations)
print(format(" Execution Time: %.6f (different template)", z))
total = total + z

gc()
gc()

x = clock()
for i = 1, iterations do
compile(views[i], i, true)(context)
end
z = clock() - x
print(format(" Execution Time: %.6f (different template cached)", z))
print(format(" Execution Time: %.6f (different template, cached)", z))
total = total + z

template.cache = {}
local contexts = new_tab(iterations, 0)

for i = 1, iterations do
contexts[i] = {"Emma " .. i, "James " .. i, "Nicholas " .. i, "Mary " .. i }
end

template.cache = {}

gc()
gc()

x = clock()
for i = 1, iterations do
compile(views[i], i, true)(contexts[i])
Expand All @@ -105,12 +130,15 @@ local function run(iterations)
print(format(" Execution Time: %.6f (different template, different context)", z))
total = total + z

gc()
gc()

x = clock()
for i = 1, 1000 do
for i = 1, iterations do
compile(views[i], i, true)(contexts[i])
end
z = clock() - x
print(format(" Execution Time: %.6f (different template, different context cached)", z))
print(format(" Execution Time: %.6f (different template, different context, cached)", z))
total = total + z
print(format(" Total Time: %.6f", total))
end
Expand Down

0 comments on commit 05447b6

Please sign in to comment.