OLD | NEW |
---|---|
(Empty) | |
1 <style> | |
2 #options { | |
3 position: absolute; | |
4 background-color: #FFFFCC; | |
5 display: none; | |
6 font-family: "Courier New"; | |
7 font-size: 9pt; | |
8 padding: 5px; | |
9 border: 1px solid #CCCC88; | |
10 z-index: 3; | |
11 } | |
12 </style> | |
13 | |
14 <script> | |
15 var optionsForm; | |
16 function show_options() { | |
17 optionsForm = window.open("options.html", "optionswindow"); | |
18 } | |
19 | |
20 // Round a number to the 1's place. | |
21 function formatNumber(str) { | |
22 str += ''; | |
23 if (str == '0') { | |
24 return 'N/A '; | |
25 } | |
26 var x = str.split('.'); | |
27 var x1 = x[0]; | |
28 var x2 = x.length > 1 ? '.' + x[1] : ''; | |
29 var regex = /(\d+)(\d{3})/; | |
30 while (regex.test(x1)) { | |
31 x1 = x1.replace(regex, '$1' + ',' + '$2'); | |
32 } | |
33 return x1; | |
34 } | |
35 | |
36 // Configuration and results are stored globally. | |
37 window.iterations = 10; | |
38 window.clearConnections = true; | |
39 window.clearCache = true; | |
40 window.results = {}; | |
41 window.results.data = new Array(); | |
42 | |
43 function Benchmark() { | |
44 var runCount_ = 0; | |
45 var count_; | |
46 var totalTime_; | |
47 var lastWin_; | |
48 var me_ = this; | |
49 var current_; | |
50 | |
51 // Start a test run | |
52 this.start = function(url) { | |
53 // Check if a run is already in progress. | |
54 if (me_.isRunning()) { | |
55 return; | |
56 } | |
57 | |
58 runCount_ = window.iterations; | |
59 count_ = 0; | |
60 totalTime_ = 0; | |
61 lastWin_ = 0; | |
62 | |
63 current_ = {}; | |
64 current_.url = url; | |
65 current_.results = new Array(); | |
66 | |
67 me_.runPage(); | |
68 } | |
69 | |
70 // Is the benchmark currently in progress. | |
71 this.isRunning = function() { | |
72 return runCount_ > 0; | |
73 } | |
74 | |
75 // Called when the test run completes. | |
76 this.finish = function() { | |
77 lastWin_.close(); | |
78 lastWin_ = 0; | |
79 | |
80 // push the result | |
81 window.results.data.push(current_); | |
82 current_ = 0; | |
83 | |
84 // show the latest | |
85 show_options(); | |
86 } | |
87 | |
88 // Update the UI after a test run. | |
89 this.displayResults = function() { | |
90 var span = document.getElementById("result"); | |
91 var score = 0; | |
92 if (count_ > 0) { | |
93 score = totalTime_ / count_; | |
94 } | |
95 span.innerHTML = score.toFixed(1) + " (" + (runCount_) + ")"; | |
96 } | |
97 | |
98 // Run a single page in the benchmark | |
99 this.runPage = function() { | |
100 if (window.clearCache) { | |
101 chromium.benchmarking.clearCache(); | |
102 } | |
103 if (window.clearConnections) { | |
104 chromium.benchmarking.closeConnections(); | |
105 } | |
106 | |
107 if (lastWin_) { | |
108 lastWin_.location = current_.url; | |
109 } else { | |
110 lastWin_ = window.open(current_.url); | |
111 } | |
112 } | |
113 | |
114 // Called when a page finishes loading. | |
115 this.pageFinished = function(csi) { | |
116 var t = Math.round((csi.finishLoadTime - csi.startLoadTime) * 1000.0); | |
117 | |
118 // Record the result | |
119 current_.results.push(t); | |
120 | |
121 // For our toolbar counters | |
122 totalTime_ += t; | |
123 count_++; | |
124 | |
125 if (--runCount_ > 0) { | |
126 setTimeout(me_.runPage, 100); | |
127 } else { | |
128 me_.finish(); | |
129 } | |
130 | |
131 // Update the UI | |
132 me_.displayResults(); | |
133 } | |
134 } | |
135 | |
136 var benchmarks = new Array(); | |
137 | |
138 chrome.self.onConnect.addListener(function(port) { | |
139 port.onMessage.addListener(function(data) { | |
140 if (data.message == "load") { | |
141 var benchmark = benchmarks[data.url]; | |
142 if (benchmark != undefined && benchmark.isRunning()) { | |
143 benchmark.pageFinished(data.values); | |
144 } | |
145 } | |
146 }); | |
147 }); | |
148 | |
149 function run() { | |
150 show_options(); | |
151 var urls = document.getElementById("url").value.split(","); | |
152 for (var i = 0; i < urls.length; i++) { | |
153 var benchmark = new Benchmark(); | |
154 benchmarks[urls[i]] = benchmark; | |
155 benchmark.start(urls[i]); // XXXMB - move to constructor | |
156 } | |
157 } | |
158 </script> | |
159 | |
160 <style> | |
161 #result { | |
162 color: green; | |
163 text-align: center; | |
164 vertical-align: center; | |
165 } | |
166 </style> | |
167 | |
168 <div id="bench"> | |
169 <img src="stopwatch.jpg" height="25" width="25" align=top onclick="show_options( )"> | |
Erik does not do reviews
2009/06/09 16:21:59
style-wise, I'd wrap this in a toolstrip-button.
| |
170 <input type="text" id="url" value="http://www.google.com/"></input> | |
171 <div class="toolstrip-button"> | |
172 <span id="run" class="open" onclick="run()">Go</span> | |
173 </div> | |
174 <span id="result"></span> | |
175 </div> | |
OLD | NEW |