Module:Timing/doc: Difference between revisions

Content deleted Content added
Testing a plain function: Example module consolidation
m change source to syntaxhighlight
Line 18:
 
The following code is a stripped-down version of [[Module:Example]]. See the page for a full version.
<sourcesyntaxhighlight lang="lua">
local p = {};
 
Line 27:
 
return 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="html5">
{{#invoke|HelloWorld|TIMING}}
</syntaxhighlight>
</source>
 
and we will have a timing report for the helper call in the module.