m-timing.mkxl /size: 4772 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=m-timing,
3%D        version=2007.12.23,
4%D          title=\CONTEXT\ Modules,
5%D       subtitle=Timing,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D Todo: adapt to latest metafun magic.
15
16\ifdefined\ShowNamedUsage \endinput \fi
17
18%D Written at the end of 2007, this module is dedicated to Taco. Reaching this
19%D point in \LUATEX\ was a non trivial effort. By visualizing a bit what happens
20%D when pages come out of \LUATEX, you may get an idea what is involved. It took
21%D much time an dedication to reach this point in the development. Add to that
22%D those daily Skype intense discussion, testing and debugging moments. Time flies
23%D but progress is impressive. The motto of this module could be: what you see
24%D is what you get. An there is much more to come \unknown.
25
26% \usemodule[timing]
27% \setupcolors[state=start]
28% \starttext
29%     \dorecurse{200}{\input tufte \par} \ShowUsage{}
30% \stoptext
31
32\definecolor[usage:line] [darkred]
33\definecolor[usage:time] [darkblue]
34\definecolor[usage:frame][darkgray]
35
36\ctxloadluafile{trac-tim.lmt}
37
38\startluacode
39local progress = moduledata.progress
40
41function progress.show(filename,names,other)
42    if not filename or filename == "" then
43        filename = progress.defaultfilename or ""
44    end
45    for n, name in pairs(progress.names(filename)) do
46        if string.find(name,names) and name ~= other and name ~= "variables: lasttime" then
47            context.ShowNamedUsage(filename,name or "",other or "")
48        end
49    end
50end
51\stopluacode
52
53\startnotmode[no-timing]
54    \appendtoks\ctxlua{moduledata.progress.store()}\to\everystarttext
55    \appendtoks\ctxlua{moduledata.progress.store()}\to\everyshipout
56    \ctxlua{luatex.registerstopactions(function() moduledata.progress.save() end)}
57\stopnotmode
58
59\permanent\protected\def\ShowNamedUsage#1#2#3%
60  {\setbox\scratchbox\vbox\bgroup
61    \startMPcode{doublefun}
62      begingroup ; save p, q, b, h, w ;
63      path p, q, b ; numeric h, w ;
64      p := \cldcontext{moduledata.progress.path("#1","#2")} ;
65    % p := p shifted -llcorner p ;
66      if bbwidth(p) > 0 :
67        h := 100 ; w := 2 * h ;
68        w := \the\textwidth-3pt ; % correct for pen
69        p := p xstretched w ;
70        b := boundingbox (llcorner p -- llcorner p shifted (w,h)) ;
71        pickup pencircle scaled 3pt ; linecap := butt ;
72        draw b withcolor \MPcolor{usage:frame} ;
73        draw p withcolor \MPcolor{usage:line} ;
74        if ("#3" <> "") and ("#3" <> "#2") :
75          q := \cldcontext{moduledata.progress.path("#1","#3")} ;
76        % q := q shifted -llcorner q ;
77          if bbwidth(q) > 1 :
78            q := q xstretched w ;
79            pickup pencircle scaled 1.5pt ; linecap := butt ;
80            draw q withcolor \MPcolor{usage:time} ;
81          fi ;
82        fi ;
83      fi ;
84      endgroup ;
85   \stopMPcode
86   \egroup
87   \scratchdimen\wd\scratchbox
88   \ifdim\scratchdimen>\zeropoint
89     \startlinecorrection[blank]
90       \box\scratchbox \endgraf
91       \hbox to \scratchdimen\bgroup
92         \tttf\strut\detokenize{#2}\hss
93         \scratchcounter     =\cldcontext{moduledata.progress.set("#1","\detokenize{#2}")}\relax
94         \scratchcounterone  =\cldcontext{moduledata.progress.bot("#1","\detokenize{#2}")}\relax
95         \scratchcountertwo  =\cldcontext{moduledata.progress.top("#1","\detokenize{#2}")}\relax
96         \scratchcounterthree=\numexpr\scratchcountertwo-\scratchcounterone\relax
97         \ifnum\scratchcounter     >\zerocount      set:\the\scratchcounter     \quad\fi
98         \ifnum\scratchcounterthree>\zerocount      inc:\the\scratchcounterthree\quad\fi
99         \ifnum\scratchcounterone  >\zerocount      min:\the\scratchcounterone  \quad\fi
100         \ifnum\scratchcountertwo  >\zerocount      max:\the\scratchcountertwo  \quad\fi
101         pages:\cldcontext{moduledata.progress.pages("#1")}%
102       \egroup
103     \stoplinecorrection
104   \fi}
105
106\permanent\protected\def\LoadUsage          #1{\ctxlua{moduledata.progress.convert("#1")}}
107\permanent\protected\def\ShowUsage          #1{\ctxlua{moduledata.progress.show("#1",".*",       "variables: elapsed")}}
108\permanent\protected\def\ShowMemoryUsage    #1{\ctxlua{moduledata.progress.show("#1","memory:.*","variables: elapsed")}}
109\permanent\protected\def\ShowNodeUsage      #1{\ctxlua{moduledata.progress.show("#1","nodes:.*", "variables: elapsed")}}
110\permanent\protected\def\ShowFilteredUsage#1#2{\ctxlua{moduledata.progress.show("#1","#2",       "variables: elapsed")}}
111\permanent\protected\def\ShowSimpleUsage  #1#2{\ctxlua{moduledata.progress.show("#1","#2")}}
112
113\endinput
114