Hopp til innhold

Modul:Timing/dok

Fra Wikipedia, den frie encyklopedi

Dette er dokumentasjonssiden for Modul:Timing

Note that the module is not ready for production, it is still under active development!

The purpose of this module is to provide a simple method to do profiling of functions, as in measuring the duration of calls, so it is possible to identify bottlenecks and problematic code. It is not a full-blown profiler, as it is not possible to do line-tracing (call-graph profiling) in the current setup. Its only purpose is to measure execution time (flat profiling), and to do this interactively from the debug console (ie. on a single function).

The profiler is called with at least 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.

The profiler does two passes over the total calls, one with a dummy function and one with the actual function. This makes it possible to get a baseline for how heavy the additional code are, and then we can later subtract this from the measurement of the function we are testing. If the function under test is to simple those to execution times will be close and the uncertainty in the difference can be high. If the execution time is similar to the standard deviation, then the profilier should be rerun with over larger or more sets.

During development it became clear that 100 calls in 10 sets is usually enough to get good estimates, but do not put overly much weight on those numbers. If one function is twice, or tree times, or even ten times slower, never mind as long as it runs in constant or linear time. If something is 100 times slower or have indications that it runs in exponential time, then you should probably consider other algorithms.

Usage

This module is not for use in ordinary templates, or on pages, it is to be used in the debug console below the edit window on module pages. For example, assume we open the page Module:HelloWorld, then we can write a call to profile the function p.helloWorld(). The code in the page looks something like the example below, but local versions may be a little different.

local p = {}

function p.helloWorld()
	return "Hi there!"
end

return p

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:Profile'(p.helloWorld)

A call like that produced for the same type of module a report like the following at nowiki

=require 'Module:Profile'(p.hei,1000,100)
Each call was running for about 6.20966E-9 seconds. Mean run time for each set was 6.20966E-6 seconds,
with a standard deviation of 5.6091767397007E-6 seconds. Total time spent was about 0.010717389 seconds.

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.

Gadget

There is an additional gadget (to be continued…)

See also