Module:MemoryTest

Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules


This is a module to test Lua memory usage.

The sample is on talk.

Change "maxitems=280" in preview to see when "Lua error: not enough memory." appears.

The "Parser profiling data:" on the bottom of the screen then indicates "Lua memory usage: 50 MB/50 MB". 280 is 45.98 MB.

Code

--- MemoryTest?

local p = {}
local wikidata = require('Module:Wikidata')

function p.makelist(frame)

	if not frame then return ':' end
	local lang = frame:preprocess('{{int:lang}}')
	local items = frame.args.items   
	local maxitems = frame.args.maxitems
	local property = frame.args.property
	
	local itemtable = mw.text.split(items, ',')


	outputtable = '<table class="wikitable sortable" style="font-size:smaller">'


	outputtable = outputtable .. '<tr valign="top">'
	outputtable = outputtable .. '<th> ct </th>'
	outputtable = outputtable .. '<th> Item </th>'
	outputtable = outputtable .. '<th> [[Property:' .. property .. '|' .. property .. ']]</th>'
	outputtable = outputtable .. '</tr>'


	for i=1, maxitems do
		if i > #itemtable then 
			break
		end
		item = 'Q' .. itemtable[i]
		outputtable = outputtable .. '<tr valign="top">'
		outputtable = outputtable .. '<td> ' .. i .. '</td>'
		outputtable = outputtable .. '<td> [[' .. item .. ']] </td>'
		outputtable = outputtable .. '<td> ' .. (wikidata.formatStatements({entity=item, property=property, numval=1, lang=lang}) or '') .. '</td>'
		outputtable = outputtable .. '</tr> '
	end


	outputtable = outputtable .. '</table>'


	return outputtable .. os.clock()

end


function p.makelistparser(frame)

	if not frame then return ':' end
	local lang = frame:preprocess('{{int:lang}}')
	local items = frame.args.items   
	local maxitems = frame.args.maxitems
	local property = frame.args.property
	
	local itemtable = mw.text.split(items, ',')


	outputtable = '<table class="wikitable sortable" style="font-size:smaller">'


	outputtable = outputtable .. '<tr valign="top">'
	outputtable = outputtable .. '<th> ct </th>'
	outputtable = outputtable .. '<th> Item </th>'
	outputtable = outputtable .. '<th> [[Property:' .. property .. '|' .. property .. ']]</th>'
	outputtable = outputtable .. '</tr>'


	for i=1, maxitems do
		if i > #itemtable then 
			break
		end
		item = 'Q' .. itemtable[i]
		outputtable = outputtable .. '<tr valign="top">'
		outputtable = outputtable .. '<td> ' .. i .. '</td>'
		outputtable = outputtable .. '<td> [[' .. item .. ']] </td>'
		outputtable = outputtable .. '<td> ' .. frame:callParserFunction{ name = '#property:' .. property, args = {from = item} }  .. '</td>'
		outputtable = outputtable .. '</tr> '
	end


	outputtable = outputtable .. '</table>'


	return outputtable .. os.clock()

end




function p.test1(frame) 	--does nothing 0.5 Mb
	local item
	return os.clock()
end

function p.test2(frame) -- loads Q1 0.7 Mb
	local item = mw.wikibase.getEntityObject('Q1')
	return os.clock()
end


function p.test3(frame) -- loads Q30 2.1 Mb
	local item = mw.wikibase.getEntityObject('Q30')
	return os.clock()
end

function p.test4(frame) -- loads Q30 twice: 2.24Mb
	for i = 1, 2 do
		local item = nil
		item = mw.wikibase.getEntityObject('Q30')
	end
	return os.clock()
end

function p.test5(frame) -- loads Q30 ten times: 3.44Mb
	for i = 1, 10 do
		local item = nil
		item = mw.wikibase.getEntityObject('Q30')
	end
	return os.clock()
end

function p.test6(frame) -- loads Q30 twice, and keeps it in memory each time: 4Mb
	local items = {}
	for i = 1, 2 do
		table.insert(items, mw.wikibase.getEntityObject('Q30'))
	end
	return os.clock()
end

function p.test7(frame) -- loads Q30 10 times, and keeps it in memory each time: 9Mb
	local items = {}
	for i = 1, 10 do
		table.insert(items, mw.wikibase.getEntityObject('Q30'))
	end
	return os.clock()
end

function p.test8(frame) -- loads Q30 once and keep it ten times in a table: 2.1 Mb
	local items = {}
	local item = mw.wikibase.getEntityObject('Q30')
	for i = 1, 10 do
		table.insert(items, item)
	end
	return os.clock()
end

return p