[timer]

Description

Link: [timer]
Author: Jason Huck
Category: Utility
Version: 8.5.x
License: Public Domain
Posted: Feb. 15, 2008
Updated: Feb. 17, 2008
More by this author...

This is yet another set of tags which allows you to set markers in your scripts and return the elapsed times between them. This version consists of two tags, [timer] and [timerstats], and works as follows:

You place markers in your code by calling [timer] with an optional note/label. You must set at least one "starter" timer before the first event you wish to time. If you want to see the results immediately, you may set the -collect param to false. Otherwise each duration is collected and displayed when [timerstats] is called.

Both tags require a page variable called 'debug' to be set to true, otherwise they immediately return null, so it's okay to leave your markers in place.

The [timerstats] tag returns a table showing each marker and the elapsed time both in milliseconds and as a percentage of the total (which is also shown). Percentages are represented as a CSS bar graph. The CSS is embedded within the tag for portability.

Parameters

none


Sample Usage

// start the timer
var('debug') = true;
timer('started timer');

// do some stuff
sleep(200);

// set a marker
timer('did some stuff');

// do more stuff
sleep(100);

// set another marker
timer('did some more stuff');

// display results
timerstats;
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
define_tag(
	'timer',
	-opt='note', -type='string',
	-opt='collect', -type='boolean',
	-priority='replace',
	-encodenone,
	-description='Generates a list of timed events.'
);
	!var_defined('debug') || !$debug ? return;

	!var_defined('__timerstart') ? var('__timerstart') = date_msec;
	!var_defined('__timer') ? var('__timer') = 0;
	!var_defined('__timerstats') ? var('__timerstats' = array);
	
	!local_defined('note') ? local('note') = '';
	!local_defined('collect') ? local('collect') = true;

	local('now') = date_msec;
	local('elapsed') = ($__timer == 0 ? 0 | #now - $__timer);
	#collect ? $__timer = #now;

	#collect ? $__timerstats->insert(#note = #elapsed) | return(#note + ': ' + #elapsed + 'ms.');	
/define_tag;




define_tag(
	'timerstats',
	-priority='replace',
	-encodenone,
	-description='Displays any timed events collected on the current page.'
);
	!var_defined('debug') || !$debug ? return;
	!var_defined('__timerstats') ? return;
	
	local('total') = (date_msec - $__timerstart);
	
	local('out' = '
		<style type="text/css">
			#timerclear {
				display: block;
				width: 1px;
				height: 1px;
				line-height: 1px;
				clear: both;
				margin-bottom: 50px;
			}
		
			#timerstats {
				width: 80%;
				margin: 0 auto;
				border-collapse: collapse;
				background-color: white;
			}
			
			#timerstats * {
				font-family: verdana, arial, helvetica, sans-serif;
				font-size: 10px;
				text-align: left;
			}
			
			#timerstats th {
				font-weight: bold;
			}
							
			#timerstats th,
			#timerstats td {
				padding: 2px;
				border-bottom: 1px solid #333;
				padding-right: 50px;
			}
			
			#timerstats>tfoot>tr>th {
				border-bottom: none;
			}
			
			.ms {
				text-align: right !important;
				padding-right: 2px !important;
			}
			
			div.barbg {
				background-color: #EEE;
				border: 2px outset #EEE;
				margin-left: 40px;
			}
			
			div.barval {
				width: 30px;
				text-align: right !important;
				margin-left: -40px;
			}
		</style>
		<br id="timerclear" />
		<table id="timerstats">
			<thead>
				<tr>
					<th>Marker</th>
					<th>Percent Total</th>
					<th class="ms">Time (ms)</th>
				</tr>
			</thead>
			<tbody>\
	');
	
	iterate($__timerstats, local('i'));
		local('w') = percent(decimal(#i->second) / decimal(#total));
	
		#out += '
				<tr>
					<td nowrap>' + #i->first + '</td>
					<td nowrap><div class="barbg" style="width: ' + #w + ';"><div class="barval">' + #w + '</div></div></td>
					<td class="ms">' + #i->second + '</td>
				</tr>\
		';
	/iterate;
	
	#out += '
			</tbody>
			<tfoot>
				<tr>
					<th>Total</th>
					<th></th>
					<th class="ms">' + #total + '</th>
				</tr>
			</tfoot>
		</table>
	';
	
	return(#out);
/define_tag;

 

Related Tags



Comments

02/17/2008, Jason Huck
Updated
Per request, [timerstats] now explicitly checks for $debug to be true rather than relying on whether or not [timer] has collected any data.
Email:


Password:



Newest

Most Popular

Support tagSwap.net