OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * Copyright 2013 Google Inc. All Rights Reserved. |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 * See the License for the specific language governing permissions and |
| 14 * limitations under the License. |
| 15 */ |
| 16 |
| 17 (function(){ |
| 18 'use strict'; |
| 19 |
| 20 var disregardFramesCount = 10; |
| 21 var framesPerTimeReading = 10; |
| 22 var maxTimeReadings = 60; |
| 23 var maxFPSReadings = 31; |
| 24 |
| 25 var frameCount = 0; |
| 26 var timeReadings = []; |
| 27 var fpsReadings = []; |
| 28 var onCompleteHandler; |
| 29 var outputElement; |
| 30 var running = false; |
| 31 var visibilityLost = false; |
| 32 |
| 33 var getTime = (typeof window.performance === 'object' && |
| 34 typeof window.performance.now === 'function') ? |
| 35 function() { return window.performance.now(); } : |
| 36 function() { return Date.now(); }; |
| 37 |
| 38 var raf = window.requestAnimationFrame || |
| 39 window.webkitRequestAnimationFrame || |
| 40 window.mozRequestAnimationFrame || |
| 41 function(callback) { setTimeout(callback, 1000 / 60); }; |
| 42 |
| 43 var visibilityChangeEvent = null; |
| 44 var hidden = null; |
| 45 if (typeof document.hidden !== 'undefined') { |
| 46 visibilityChangeEvent = 'visibilitychange'; |
| 47 hidden = 'hidden'; |
| 48 } else if (typeof document.mozHidden !== 'undefined') { |
| 49 visibilityChangeEvent = 'mozvisibilitychange'; |
| 50 hidden = 'mozHidden'; |
| 51 } else if (typeof document.msHidden !== 'undefined') { |
| 52 visibilityChangeEvent = 'msvisibilitychange'; |
| 53 hidden = 'msHidden'; |
| 54 } else if (typeof document.webkitHidden !== 'undefined') { |
| 55 visibilityChangeEvent = 'webkitvisibilitychange'; |
| 56 hidden = 'webkitHidden'; |
| 57 } |
| 58 if (visibilityChangeEvent) { |
| 59 document.addEventListener(visibilityChangeEvent, function() { |
| 60 if (running && document[hidden]) { |
| 61 visibilityLost = true; |
| 62 } |
| 63 }); |
| 64 } |
| 65 |
| 66 var randomSeed = 3384413; |
| 67 function random() { |
| 68 var temp = randomSeed; |
| 69 // Robert Jenkins' 32 bit integer hash function. |
| 70 temp = ((temp + 0x7ed55d16) + (temp << 12)) & 0xffffffff; |
| 71 temp = ((temp ^ 0xc761c23c) ^ (temp >>> 19)) & 0xffffffff; |
| 72 temp = ((temp + 0x165667b1) + (temp << 5)) & 0xffffffff; |
| 73 temp = ((temp + 0xd3a2646c) ^ (temp << 9)) & 0xffffffff; |
| 74 temp = ((temp + 0xfd7046c5) + (temp << 3)) & 0xffffffff; |
| 75 temp = ((temp ^ 0xb55a4f09) ^ (temp >>> 16)) & 0xffffffff; |
| 76 randomSeed = temp; |
| 77 return (randomSeed & 0xfffffff) / 0x10000000; |
| 78 } |
| 79 |
| 80 function disregardFrames() { |
| 81 raf((frameCount++ < disregardFramesCount) ? disregardFrames : trackFrameRate); |
| 82 } |
| 83 |
| 84 function trackFrameRate() |
| 85 { |
| 86 frameCount++; |
| 87 if (frameCount % framesPerTimeReading === 0) { |
| 88 timeReadings.unshift(getTime()); |
| 89 if (timeReadings.length > maxTimeReadings) { |
| 90 timeReadings.pop(); |
| 91 } |
| 92 if (timeReadings.length > 1) { |
| 93 var fpsReading = 1000 * (timeReadings.length - 1) * framesPerTimeReading / |
| 94 (timeReadings[0] - timeReadings[timeReadings.length - 1]); |
| 95 fpsReadings.push(fpsReading); |
| 96 output(fpsReading + ' FPS\n'); |
| 97 if (fpsReadings.length >= maxFPSReadings) { |
| 98 outputSummary(); |
| 99 running = false; |
| 100 if (typeof onCompleteHandler === 'function') { |
| 101 onCompleteHandler(); |
| 102 } |
| 103 return; |
| 104 } |
| 105 } |
| 106 } |
| 107 raf(trackFrameRate); |
| 108 } |
| 109 |
| 110 function output(text) { |
| 111 outputElement.value += text; |
| 112 } |
| 113 |
| 114 function outputSummary() { |
| 115 if (visibilityLost) { |
| 116 output('Warning: Document lost visibility during test, ' + |
| 117 'results may be inaccurate.\n'); |
| 118 } |
| 119 var total = 0; |
| 120 fpsReadings.forEach(function(fpsReading) { total += fpsReading; }); |
| 121 var average = total / maxFPSReadings; |
| 122 output('Average: ' + average + ' FPS\n'); |
| 123 var temp = 0; |
| 124 fpsReadings.forEach(function(fpsReading) { |
| 125 temp += (fpsReading - average) * (fpsReading - average); |
| 126 }); |
| 127 output('Stddev: ' + Math.sqrt(temp / fpsReadings.length) + ' FPS\n'); |
| 128 fpsReadings.sort(); |
| 129 output('Min: ' + fpsReadings[0] + ' FPS\n'); |
| 130 output('Max: ' + fpsReadings[fpsReadings.length - 1] + ' FPS\n'); |
| 131 var median = fpsReadings[fpsReadings.length >> 1]; |
| 132 if (fpsReadings.length % 2 !== 0) { |
| 133 median = (median + fpsReadings[(fpsReadings.length >> 1) + 1]) / 2; |
| 134 } |
| 135 output('Median: ' + median + ' FPS\n'); |
| 136 } |
| 137 |
| 138 var start = function() { |
| 139 outputElement = document.createElement('textarea'); |
| 140 outputElement.setAttribute('rows', maxFPSReadings + 6); |
| 141 outputElement.setAttribute('cols', 32); |
| 142 document.body.appendChild(outputElement); |
| 143 |
| 144 output('Disregarding initial ' + disregardFramesCount + ' frames.\n') |
| 145 raf(disregardFrames); |
| 146 |
| 147 running = true; |
| 148 }; |
| 149 |
| 150 window.Perf = { |
| 151 set oncomplete(callback) { |
| 152 onCompleteHandler = callback; |
| 153 }, |
| 154 start: start, |
| 155 random: random, |
| 156 }; |
| 157 |
| 158 })(); |
OLD | NEW |