Module:Timing/doc: Difference between revisions

Content deleted Content added
m Typo fixing, replaced: occurences → occurrences, typo(s) fixed: As a result → As a result,, ie. → i.e. , stripped down → stripped-down
mNo edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 15:
 
=== Testing a plain function ===
For example, assume we open a page [[Module:HelloWorldExample]], then we can write a call to profile the function p.hello(). The code in the page looks something like the example below, but local versions may be a little different. (At ''nowiki'' a similar code is at [[w:no:Module:HelloWorld2]].)
 
The following code is a stripped-down version of [[Module:HelloWorldExample]]. See the page for a full version.
<sourcesyntaxhighlight lang="lua">
my_objectlocal p = {};
 
my_objectp.hello = function( frame )
local str = "Hello World!"
return str
end
 
return my_object p
</syntaxhighlight>
</source>
 
Access to this code will be through a generic ''p'' in the debug console. Assuming the previous code the profiler can be called as
 
<sourcesyntaxhighlight lang="lua">
=require 'Module:Timing'(p.hello)
</syntaxhighlight>
</source>
 
A call like that produced for the same type of module a report like the following at enwiki
Line 57:
 
A common example on how to pass in an argument is how to do this for the current frame
<sourcesyntaxhighlight lang="lua">
=require 'Module:Timing'(p.hello, mw.getCurrentFrame())
</syntaxhighlight>
</source>
 
This produce the following output
Line 83:
The wrapper function can de defined in the debugger, that is added in the text area for debugging. Add a separate function like the following
 
<sourcesyntaxhighlight lang="lua">
function wrap() return p.hello(mw.getCurrentFrame()) end
=require 'Module:Timing'(wrap)
</syntaxhighlight>
</source>
 
A call sequence like that produced the following output
Line 105:
An alternate form with an anonymous function is
 
<sourcesyntaxhighlight lang="lua">
=require 'Module:Timing'(function() return p.hello(mw.getCurrentFrame()) end)
</syntaxhighlight>
</source>
 
In general you should precompute as much as possible to avoid unnecessary computations inside the loop, like this
<sourcesyntaxhighlight lang="lua">
my_frame=mw.getCurrentFrame()
=require 'Module:Timing'(function() return p.hello(my_frame) end)
</syntaxhighlight>
</source>
 
====Wrapper in the module====
Sometimes it is better to put the wrapper in the module itself
 
<sourcesyntaxhighlight lang="lua">
my_object = {};
 
Line 131:
 
return my_object
</syntaxhighlight>
</source>
 
That produced the following output
Line 148:
 
In general you should precompute as much as possible to avoid unnecessary computations inside the loop, like this
<sourcesyntaxhighlight lang="lua">
my_object = {};
 
Line 162:
 
return my_object
</syntaxhighlight>
</source>
 
===Testing in a sandboxed environment===
Line 169:
Sometimes it is better to put the wrapper in the module under inspection itself
 
<sourcesyntaxhighlight lang="lua">
local timing = require 'Module:Timing'
 
Line 184:
 
return my_object
</syntaxhighlight>
</source>
 
This can them be used in [[Special:TemplateSandbox]] with a prefix from [[Special:MyPage/sandbox]] and any "render page" we see fit for the purpose. Often we want a specific page though to get access to additional data like the Wikidata item. Fill inn an invoke-call for our helper function
 
<sourcesyntaxhighlight lang="html5wikitext">
{{#invoke|HelloWorld|TIMING}}
</syntaxhighlight>
</source>
 
and we will have a timing report for the helper call in the module.