summaryrefslogtreecommitdiffstats
path: root/third_party/jstemplate/jstemplate_example.js
blob: b9e2faf650161443b4edfd6437ba2f8baa5efd93 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2006 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
/**
 * @pageoverview Some logic for the jstemplates test pages.
 *
 * @author Steffen Meschkat (mesch@google.com)
 */


function elem(id) {
  return document.getElementById(id);
}

/**
 * Are we actively profiling JstProcessor?
 * @type boolean
 */
var profiling = false;

logHtml = function(html) {
  elem('log').innerHTML += html + '<br/>';
};


/**
 * Initializer for interactive test: copies the value from the actual
 * template HTML into a text area to make the HTML source visible.
 */
function jsinit() {
  elem('template').value = elem('tc').innerHTML;
}


/**
 * Interactive test.
 *
 * @param {boolean} reprocess If true, reprocesses the output of the
 * previous invocation.
 */
function jstest(reprocess) {
  var jstext = elem('js').value;
  var input = jsEval(jstext);
  var t;
  if (reprocess) {
    t = elem('out');
  } else {
    elem('tc').innerHTML = elem('template').value;
    t = jstGetTemplate('t');
    elem('out').innerHTML = '';
    elem('out').appendChild(t);
  }
  if (profiling) Profiler.reset();
  jstProcess(new JsEvalContext(input), t);
  if (profiling) Profiler.dump();
  elem('html').value = elem('out').innerHTML;
}


/**
 * Performance test: jst initial processing.
 *
 * @param {Object} data Test data to apply the template to.
 */
function perf1(data) {
  elem('out').innerHTML = '';
  var t = jstGetTemplate('t1');
  elem('out').appendChild(t);
  if (profiling) Profiler.reset();
  jstProcess(new JsEvalContext(data), t);
  if (profiling) Profiler.dump();
}


/**
 * Performance test: jst reprocessing or previous processing output.
 *
 * @param {Object} data Test data to apply the template to.
 */
function perf1a(data) {
  if (profiling) Profiler.reset();
  jstProcess(new JsEvalContext(data), elemOrDie('out'));
  if (profiling) Profiler.dump();
}


/**
 * Performance test: jst initial processing, with display:none during
 * processing.
 *
 * @param {Object} data Test data to apply the template to.
 */
function perf1b(data) {
  var o = elem('out');
  o.innerHTML = '';
  var t = jstGetTemplate('t1');
  o.appendChild(t);
  displayNone(o);
  if (profiling) Profiler.reset();
  jstProcess(new JsEvalContext(data), t);
  if (profiling) Profiler.dump();
  displayDefault(o);
}


/**
 * Performance test: create output procedurally as string and assign
 * to innerHTML.
 *
 * @param {Object} data Test data to apply the template to.
 */
function perf2(data) {
  var t = [];
  t.push("<table><tr><th>item</th><th>label</th><th>address</th></tr>");
  for (var i = 0; i < data.entries.length; ++i) {
    var e = data.entries[i];
    t.push("<tr><td>" + i + "</td><td>" + e.label + "</td><td>" +
           e.address + "</td></tr>");
  }
  t.push("</table>");
  elem("out").innerHTML = t.join('');
}


/**
 * A test runner for a test. Does the timing. @constructor
 *
 * @param {number} times number of iterations the test is executed.
 * @param {Function} test The test to execute.
 * @param {Function} result Function will be called with the execution
 * time as argument.
 */
function TestRun(times, test, result) {
  this.count_ = 0;
  this.times_ = times;
  this.test_ = test;
  this.result_ = result;
  this.start_ = (new Date).valueOf();
  this.run_();
}


/**
 * Executes the test run.
 */
TestRun.prototype.run_ = function() {
  if (this.count_ < this.times_) {
    this.test_(this.count_);
    this.count_ += 1;
    objectSetTimeout(this, this.run_, 0);
  } else {
    this.stop_ = (new Date).valueOf();
    this.result_(this.stop_ - this.start_);
  }
};


/**
 * Creates a testrun function for test count invocations of function
 * f, whose runtime will be output to the element with is given in
 * result.
 *
 * @param {Object} data The test data object.
 * @param {Function} f
 * @param {number} count
 * @param {string} result
*/
function createTestRun(count, test) {
  var data = {
    entries: []
  };
  for (var i = 0; i < count; ++i) {
    data.entries.push({ label: "label" + i, address: "address" + i });
  }
  // This function is passed to the TimeoutSequence, and receives the
  // TimeoutSequence as argument on invocation.
  return function(s) {
    new TestRun(1, function(i) {
      window[test](data);
    }, function(time) {
      elem(test + '-' + count).innerHTML = time + 'ms';
      s.run();
    });
  };
}

/**
 * Runs all tests the given number of times. Invoked from the HTML page.
 *
 * @param {number} count
 */
function jsperf(count) {
  elemOrDie('log').innerHTML = '';
  profiling = !!elemOrDie('profile').checked;
  if (profiling && !JstProcessor.profiling_) {
    JstProcessor.profiling_ = true;
    Profiler.monitorAll(proto(JstProcessor), false);
  }

  var s = new TimeoutSequence(null, null, true);

  s.add(createTestRun(count, "perf1"));
  s.add(createTestRun(count, "perf1b"));
  s.add(createTestRun(count, "perf1a"));
  s.add(createTestRun(count, "perf2"));

  s.run();
}

function run(test, count) {
  var data = {
    entries: []
  };
  for (var i = 0; i < count; ++i) {
    data.entries.push({ label: "label" + i, address: "address" + i });
  }
  new TestRun(1, function() {
    window[test](data);
  }, function(time) {
    elem(test + '-' + count).innerHTML = time + 'ms';
  });
}