Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / examples / api / core / example.js
blobe126026e9ee0ee6021ce8c1cd30df36adceff08d
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var itrMax = 1000;
6 var itrCount = 0;
7 var itrSend = new Float64Array(itrMax);
8 var itrNaCl = new Float64Array(itrMax);
9 var itrRecv = new Float64Array(itrMax);
10 var delay = 0;
12 function getTimeInMilliseconds() {
13 return (new Date()).getTime();
16 function attachListeners() {
17 document.getElementById('start').addEventListener('click', startTest);
18 countEl = document.getElementById('count');
19 countEl.textContent = itrMax;
22 function startTest() {
23 if (common.naclModule) {
24 var startEl = document.getElementById('start');
25 startEl.disabled = true;
27 var delayEl = document.getElementById('delay');
28 delay = parseInt(delayEl.value, 10);
30 common.updateStatus('Running Test');
31 itrCount = 0;
32 itrSend[0] = getTimeInMilliseconds();
33 common.naclModule.postMessage(delay);
37 function setStats(nacl, compute, total) {
38 var statNaClEl = document.getElementById('NaCl');
39 var statRoundEl = document.getElementById('Round');
40 var statTotalEl = document.getElementById('Total');
42 statNaClEl.textContent = (nacl / itrMax) + ' ms';
43 statRoundEl.textContent = (compute / itrMax) + ' ms';
44 statTotalEl.textContent = (total / itrMax) + ' ms';
47 // Called by the common.js module.
48 function handleMessage(message_event) {
49 // Convert NaCl Seconds elapsed to MS
50 itrNaCl[itrCount] = message_event.data * 1000.0;
51 itrRecv[itrCount] = getTimeInMilliseconds();
52 itrCount++;
54 if (itrCount === itrMax) {
55 common.updateStatus('Test Finished');
56 var startEl = document.getElementById('start');
57 startEl.disabled = false;
59 var naclMS = 0.0;
60 var computeMS = 0.0;
61 for (var i = 0; i < itrMax; i++) {
62 naclMS += itrNaCl[i];
63 computeMS += itrRecv[i] - itrSend[i];
66 setStats(naclMS, computeMS, itrRecv[itrMax - 1] - itrSend[0]);
67 } else {
68 itrSend[itrCount] = getTimeInMilliseconds();
69 common.naclModule.postMessage(delay);