How can I time processing of chunk of code using M

2019-08-04 00:00发布

Often when developing a Movable Type template, I come up with multiple ways to generate the same result, and am curious which is more efficient. Or, I simply want to know how long something took, such as generating search results.

Is there an easy way to recording processing time or other timing strategies using template tags without requiring external tools?

2条回答
\"骚年 ilove
2楼-- · 2019-08-04 00:04

This is the method I came up with and have been using for some time. It reports timing to the nearest second, making use of standard Movable Type template language.

To time a chunk of template code, create a system or blog level template module called "timing":

<mt:If name="part" eq="start">
  <$mt:Date format="%H" setvar="hours"$>
  <$mt:Date format="%M" setvar="minutes"$>
  <$mt:Date format="%S" setvar="seconds"$>
  <$mt:Var name="hours" op="*" value="3600" setvar="hourseconds"$>
  <$mt:Var name="minutes" op="*" value="60" setvar="minuteseconds"$>
  <$mt:Var name="totalseconds" value="$hourseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$minuteseconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$seconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" setvar="startseconds"$>
<mt:Else name="part" eq="stop">
  <$mt:Date format="%H" setvar="hours"$>
  <$mt:Date format="%M" setvar="minutes"$>
  <$mt:Date format="%S" setvar="seconds"$>
  <$mt:Var name="hours" op="*" value="3600" setvar="hourseconds"$>
  <$mt:Var name="minutes" op="*" value="60" setvar="minuteseconds"$>
  <$mt:Var name="totalseconds" value="$hourseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$minuteseconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" op="+" value="$seconds" setvar="totalseconds"$>
  <$mt:Var name="totalseconds" setvar="finishseconds"$>
  <$mt:Var name="finishseconds" op="-" value="$startseconds" setvar="elapsedseconds">
  <!-- This search completed in <mt:If name="elapsedseconds" eq="0">less than 1 second<mt:Else name="elapsedseconds" eq="1">1 second<mt:Else><$mt:Var name="elapsedseconds"$> seconds</mt:If>. -->
</mt:If>

Then, in a template you want to time something in, place these two lines at the start and end of the chunk of interest:

<$mt:Include module="timing" part="start"$>
  <mt:Ignore>Code I want to time</mt:Ignore>
<$mt:Include module="timing" part="stop"$>

You could of course add a line of output in the "start" section if you want to denote in the output where the timing started.

查看更多
趁早两清
3楼-- · 2019-08-04 00:09

Or you could use the debug mode 8 as explained here: http://www.movabletype.org/documentation/developer/plugins/debug-mode.html

There's also an option (in the same General Settings panel as the debug mode) to activate a performance log with a threshold value.

查看更多
登录 后发表回答