Content deleted Content added
No edit summary |
mNo edit summary |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 1:
{{Module rating |beta<!-- Values: pre-alpha • alpha • beta • release • protected -- If a rating not needed/relevant, delete this template call -->}}
<!-- Categories go at the bottom of this page and interwikis go in Wikidata. -->
The purpose of this module is to provide a simple method to do timing analysis for [[w:en:performance tuning|performance tuning]] of Lua-functions, that is [[w:en:Profiling (computer programming)|profiling of functions]]. Usually measuring the duration of function calls is part of a larger effort to identify bottlenecks and problematic code. This is not a full-blown profiler, as it is not possible to do line-tracing (call-graph profiling) in the current set up. Its only purpose is to measure execution time (flat profiling), and to do this interactively from the debug console (
''As it is said several times in this text; the timing is not accurate and it should only be used as a coarse measure of the approximate run time and load of a function. Do not attempt to fine-tune a module to just fit into the maximum allowed time
The timing analysis is called with an executable function, and optionally a count (size, default 100) of each test set and a number of such test sets (default 10). The total number of calls will be ''count × sets'' and gives the mean runtime for the function. The standard deviation is calculated from the number of ''sets'' and will only be a coarse estimate. If only run with a single set the standard deviation will go away, but even if it isn't measured the execution time will still vary.
Line 15:
=== Testing a plain function ===
For example, assume we open a page [[Module:
The following code is a stripped
<
local str = "Hello World!"
return str
end
return
</syntaxhighlight>
Access to this code will be through a generic ''p'' in the debug console. Assuming the previous code the profiler can be called as
<
=require 'Module:Timing'(p.hello)
</syntaxhighlight>
A call like that produced for the same type of module a report like the following at enwiki
Line 47:
</pre>
In this the important information is <
Note that this function is very simple, and therefore the run time for the function gets close to the run time for the baseline. As a result, the run time for each set gets close to the standard deviation.
=== Testing with arguments ===
Line 57:
A common example on how to pass in an argument is how to do this for the current frame
<
=require 'Module:Timing'(p.hello, mw.getCurrentFrame())
</syntaxhighlight>
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
<
function wrap() return p.hello(mw.getCurrentFrame()) end
=require 'Module:Timing'(wrap)
</syntaxhighlight>
A call sequence like that produced the following output
Line 105:
An alternate form with an anonymous function is
<
=require 'Module:Timing'(function() return p.hello(mw.getCurrentFrame()) end)
</syntaxhighlight>
In general you should precompute as much as possible to avoid unnecessary computations inside the loop, like this
<
my_frame=mw.getCurrentFrame()
=require 'Module:Timing'(function() return p.hello(my_frame) end)
</syntaxhighlight>
====Wrapper in the module====
Sometimes it is better to put the wrapper in the module itself
<
my_object = {};
Line 131:
return my_object
</syntaxhighlight>
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
<
my_object = {};
Line 162:
return my_object
</syntaxhighlight>
===Testing in a sandboxed environment===
Line 169:
Sometimes it is better to put the wrapper in the module under inspection itself
<
local timing = require 'Module:Timing'
Line 184:
return my_object
</syntaxhighlight>
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
<
{{#invoke|HelloWorld|TIMING}}
</syntaxhighlight>
and we will have a timing report for the helper call in the module.
Line 211:
* [[w:en:Average]] ''The code use an arithmetic mean''
* [[w:en:Standard deviation]] ''The code use variance and standard deviation''
* [[w:en:Sum of normally distributed random variables]] ''The code makes an assumption that delays are independent random
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox||
|