Module:Sandbox/Fewfre/perf

local getArgs = require('Module:Arguments').getArgs local item = require('Module:Item') local libutil = require('Module:LibraryUtil') local p = {}

function p._pageExistsNotWantedPage(frame) local args = getArgs(frame) return mw.title.existsWithoutWanted(args[1]) and "true" or "false" end

function p.load1(frame) local args = getArgs(frame) return "" end

function p.load2(frame) local args = getArgs(frame) local data = item.getApiData local al = item.getApiAliases return "" end

function p.listAllItems local data = item.getApiData local t = {} for key,val in pairs(data.items) do		local skipLink = string.match(val.name, "Minion") or val.category == "DUNGEON_PASS" or string.match(val.id, "_SKIN_") or string.match(val.id, "PERFECT_") or string.match(val.id, "MASTER_SKULL_TIER_") t[#t+1] = " "..table.concat({			skipLink and val.name or ""..val.name.."",			string.match(val.name, "Minion") and "MINION" or '',			val.material,			val.id,			val.date,		}, "  ").."  " end return table.concat{ " "	} end

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

function p.loadDataTest local COUNT = 500000 testPerformance("manual-cache", function		local data = nil		for i=COUNT,1,-1 do			if not data then				data = mw.loadData('Module:Item/ApiAliases')			end		end	end) testPerformance("default-cache", function		local data = nil		for i=COUNT,1,-1 do			data = mw.loadData('Module:Item/ApiAliases')		end	end) end

local foo1 = nil function p.frameMemoryTest foo1 = foo1 or 1 foo2 = foo2 or 1 -- foo2 not local, so remembored globally foo1 = foo1+1 foo2 = foo2+1 return foo1.."-"..foo2 end

return p