Module:Sandbox/Fewfre/perf

local customLibUtil = require('Module:LibraryUtil') local libraryUtil = require( 'libraryUtil' )

local COUNT = 500000 function testPerformance(name, func) mw.log("TEST: "..name) local start = os.clock func mw.log(" done - "..(os.clock - start)) end

function checkTypeSingle(name, argIdx, arg, allowedType, nilOk) local argType = type(arg) if argType == allowedType or (nilOk and arg == nil) then return arg end -- else invalid ({ checkType=error }).checkType(string.format("bad argument #%d to '%s' (%s expected, got %s)", argIdx, name, expectTypes, argType), 3) end

function checkTypeListOnly(name, argIdx, arg, allowedList) local argType = type(arg) for i=1, #allowedList do   if allowedList[i] == argType then return arg end end

({ checkType=error }).checkType(string.format("bad argument #%d to '%s' (%s expected, got %s)", argIdx, name, table.concat(expectTypes, '/'), argType), 3) end

local p = {}

function p.test testPerformance("checkTypeLight-single", function		local arg = {"foo"}		for i=COUNT,1,-1 do			checkTypeLight("foo", 1, arg, "table", true)		end	end) testPerformance("libraryUtil.checkType", function		local arg = {"foo"}		for i=COUNT,1,-1 do			libraryUtil.checkType("foo", 1, arg, "table", true)		end	end)

testPerformance("checkTypeSingle", function		local arg = {"foo"}		for i=COUNT,1,-1 do			checkTypeSingle("foo", 1, arg, "table", true)		end	end) testPerformance("checkTypeLight-multi", function		local arg = {"foo"}		for i=COUNT,1,-1 do			checkTypeLight("foo", 1, arg, { "string", "table", 'nil' })		end	end) testPerformance("libraryUtil.checkTypeMulti", function		local arg = {"foo"}		for i=COUNT,1,-1 do			libraryUtil.checkTypeMulti("foo", 1, arg, { "string", "table", 'nil' })		end	end)

testPerformance("checkTypeListOnly", function		local arg = {"foo"}		for i=COUNT,1,-1 do			checkTypeListOnly("foo", 1, arg, { "string", "table", 'nil' })		end	end) end

return p