1 // Copyright 2006 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 // implied. See the License for the specific language governing
13 // permissions and limitations under the License.
15 * @pageoverview Some logic for the jstemplates test pages.
17 * @author Steffen Meschkat (mesch@google.com)
22 return document
.getElementById(id
);
26 * Are we actively profiling JstProcessor?
29 var profiling
= false;
31 logHtml = function(html
) {
32 elem('log').innerHTML
+= html
+ '<br/>';
37 * Initializer for interactive test: copies the value from the actual
38 * template HTML into a text area to make the HTML source visible.
41 elem('template').value
= elem('tc').innerHTML
;
48 * @param {boolean} reprocess If true, reprocesses the output of the
49 * previous invocation.
51 function jstest(reprocess
) {
52 var jstext
= elem('js').value
;
53 var input
= jsEval(jstext
);
58 elem('tc').innerHTML
= elem('template').value
;
59 t
= jstGetTemplate('t');
60 elem('out').innerHTML
= '';
61 elem('out').appendChild(t
);
63 if (profiling
) Profiler
.reset();
64 jstProcess(new JsEvalContext(input
), t
);
65 if (profiling
) Profiler
.dump();
66 elem('html').value
= elem('out').innerHTML
;
71 * Performance test: jst initial processing.
73 * @param {Object} data Test data to apply the template to.
75 function perf1(data
) {
76 elem('out').innerHTML
= '';
77 var t
= jstGetTemplate('t1');
78 elem('out').appendChild(t
);
79 if (profiling
) Profiler
.reset();
80 jstProcess(new JsEvalContext(data
), t
);
81 if (profiling
) Profiler
.dump();
86 * Performance test: jst reprocessing or previous processing output.
88 * @param {Object} data Test data to apply the template to.
90 function perf1a(data
) {
91 if (profiling
) Profiler
.reset();
92 jstProcess(new JsEvalContext(data
), elemOrDie('out'));
93 if (profiling
) Profiler
.dump();
98 * Performance test: jst initial processing, with display:none during
101 * @param {Object} data Test data to apply the template to.
103 function perf1b(data
) {
106 var t
= jstGetTemplate('t1');
109 if (profiling
) Profiler
.reset();
110 jstProcess(new JsEvalContext(data
), t
);
111 if (profiling
) Profiler
.dump();
117 * Performance test: create output procedurally as string and assign
120 * @param {Object} data Test data to apply the template to.
122 function perf2(data
) {
124 t
.push("<table><tr><th>item</th><th>label</th><th>address</th></tr>");
125 for (var i
= 0; i
< data
.entries
.length
; ++i
) {
126 var e
= data
.entries
[i
];
127 t
.push("<tr><td>" + i
+ "</td><td>" + e
.label
+ "</td><td>" +
128 e
.address
+ "</td></tr>");
131 elem("out").innerHTML
= t
.join('');
136 * A test runner for a test. Does the timing. @constructor
138 * @param {number} times number of iterations the test is executed.
139 * @param {Function} test The test to execute.
140 * @param {Function} result Function will be called with the execution
143 function TestRun(times
, test
, result
) {
147 this.result_
= result
;
148 this.start_
= (new Date
).valueOf();
154 * Executes the test run.
156 TestRun
.prototype.run_ = function() {
157 if (this.count_
< this.times_
) {
158 this.test_(this.count_
);
160 objectSetTimeout(this, this.run_
, 0);
162 this.stop_
= (new Date
).valueOf();
163 this.result_(this.stop_
- this.start_
);
169 * Creates a testrun function for test count invocations of function
170 * f, whose runtime will be output to the element with is given in
173 * @param {Object} data The test data object.
174 * @param {Function} f
175 * @param {number} count
176 * @param {string} result
178 function createTestRun(count
, test
) {
182 for (var i
= 0; i
< count
; ++i
) {
183 data
.entries
.push({ label
: "label" + i
, address
: "address" + i
});
185 // This function is passed to the TimeoutSequence, and receives the
186 // TimeoutSequence as argument on invocation.
188 new TestRun(1, function(i
) {
191 elem(test
+ '-' + count
).innerHTML
= time
+ 'ms';
198 * Runs all tests the given number of times. Invoked from the HTML page.
200 * @param {number} count
202 function jsperf(count
) {
203 elemOrDie('log').innerHTML
= '';
204 profiling
= !!elemOrDie('profile').checked
;
205 if (profiling
&& !JstProcessor
.profiling_
) {
206 JstProcessor
.profiling_
= true;
207 Profiler
.monitorAll(proto(JstProcessor
), false);
210 var s
= new TimeoutSequence(null, null, true);
212 s
.add(createTestRun(count
, "perf1"));
213 s
.add(createTestRun(count
, "perf1b"));
214 s
.add(createTestRun(count
, "perf1a"));
215 s
.add(createTestRun(count
, "perf2"));
220 function run(test
, count
) {
224 for (var i
= 0; i
< count
; ++i
) {
225 data
.entries
.push({ label
: "label" + i
, address
: "address" + i
});
227 new TestRun(1, function() {
230 elem(test
+ '-' + count
).innerHTML
= time
+ 'ms';