| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<!-- |
| 4 |
Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 5 |
|
| 6 |
Use of this source code is governed by the Apache License, Version 2.0. |
| 7 |
See the COPYING file for details. |
| 8 |
--> |
| 9 |
<!-- |
| 10 |
Author: [email protected] (Attila Bodis) |
| 11 |
--> |
| 12 |
<head> |
| 13 |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 14 |
<title>Closure Performance Tests - goog.pubsub.PubSub</title> |
| 15 |
<link rel="stylesheet" href="../testing/performancetable.css" /> |
| 16 |
<script src="../base.js"></script> |
| 17 |
<script> |
| 18 |
goog.require('goog.events'); |
| 19 |
goog.require('goog.events.EventTarget'); |
| 20 |
goog.require('goog.pubsub.PubSub'); |
| 21 |
goog.require('goog.testing.PerformanceTable'); |
| 22 |
goog.require('goog.testing.PerformanceTimer'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
</script> |
| 25 |
</head> |
| 26 |
<body> |
| 27 |
<h1>goog.pubsub.PubSub Performance Tests</h1> |
| 28 |
<p> |
| 29 |
<b>User-agent:</b> <script>document.write(navigator.userAgent);</script> |
| 30 |
</p> |
| 31 |
<p> |
| 32 |
Compares the performance of the event system (<code>goog.events.*</code>) |
| 33 |
with the <code>goog.pubsub.PubSub</code> class. |
| 34 |
</p> |
| 35 |
<p> |
| 36 |
The baseline test creates 1000 event targets and 1000 objects that handle |
| 37 |
events dispatched by the event targets, and has each event target dispatch |
| 38 |
2 events 5 times each. |
| 39 |
</p> |
| 40 |
<p> |
| 41 |
The single-<code>PubSub</code> test creates 1000 publishers, 1000 |
| 42 |
subscribers, and a single pubsub channel. Each subscriber subscribes to |
| 43 |
topics on the same pubsub channel. Each publisher publishes 5 messages to |
| 44 |
2 topics each via the pubsub channel. |
| 45 |
</p> |
| 46 |
<p> |
| 47 |
The multi-<code>PubSub</code> test creates 1000 publishers that are |
| 48 |
subclasses of <code>goog.pubsub.PubSub</code> and 1000 subscribers. Each |
| 49 |
subscriber subscribes to its own publisher. Each publisher publishes 5 |
| 50 |
messages to 2 topics each via its own pubsub channel. |
| 51 |
</p> |
| 52 |
<div id="perfTable"></div> |
| 53 |
<hr> |
| 54 |
<script> |
| 55 |
var targets, publishers, pubsubs, handlers; |
| 56 |
|
| 57 |
// Number of objects to test per run. |
| 58 |
var SAMPLES_PER_RUN = 1000; |
| 59 |
|
| 60 |
// The performance table & performance timer. |
| 61 |
var table, timer; |
| 62 |
|
| 63 |
// Event/topic identifiers. |
| 64 |
var ACTION = 'action'; |
| 65 |
var CHANGE = 'change'; |
| 66 |
|
| 67 |
// Number of times handlers have been called. |
| 68 |
var actionCount = 0; |
| 69 |
var changeCount = 0; |
| 70 |
|
| 71 |
// Generic event handler class. |
| 72 |
function Handler() { |
| 73 |
} |
| 74 |
Handler.prototype.handleAction = function() { |
| 75 |
actionCount++; |
| 76 |
}; |
| 77 |
Handler.prototype.handleChange = function() { |
| 78 |
changeCount++; |
| 79 |
}; |
| 80 |
|
| 81 |
// Generic publisher class that uses a global pubsub channel. |
| 82 |
function Publisher(pubsub, id) { |
| 83 |
this.pubsub = pubsub; |
| 84 |
this.id = id; |
| 85 |
} |
| 86 |
Publisher.prototype.publish = function(topic) { |
| 87 |
this.pubsub.publish(this.id + '.' + topic); |
| 88 |
}; |
| 89 |
|
| 90 |
// PubSub subclass; allows clients to subscribe and uses itself to publish. |
| 91 |
function PubSub() { |
| 92 |
goog.pubsub.PubSub.call(this); |
| 93 |
} |
| 94 |
goog.inherits(PubSub, goog.pubsub.PubSub); |
| 95 |
|
| 96 |
// EventTarget subclass; uses goog.events.* to dispatch events. |
| 97 |
function Target() { |
| 98 |
goog.events.EventTarget.call(this); |
| 99 |
} |
| 100 |
goog.inherits(Target, goog.events.EventTarget); |
| 101 |
Target.prototype.fireEvent = function(type) { |
| 102 |
this.dispatchEvent(type); |
| 103 |
}; |
| 104 |
|
| 105 |
function initHandlers(count) { |
| 106 |
for (var i = 0; i < count; i++) { |
| 107 |
handlers[i] = new Handler(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
function initPublishers(pubsub, count) { |
| 112 |
for (var i = 0; i < count; i++) { |
| 113 |
publishers[i] = new Publisher(pubsub, i); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
function initPubSubs(count) { |
| 118 |
for (var i = 0; i < count; i++) { |
| 119 |
pubsubs[i] = new PubSub(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
function initTargets(count) { |
| 124 |
for (var i = 0; i < count; i++) { |
| 125 |
targets[i] = new Target(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
function createEventListeners(count) { |
| 130 |
initHandlers(count); |
| 131 |
initTargets(count); |
| 132 |
for (var i = 0; i < count; i++) { |
| 133 |
goog.events.listen(targets[i], ACTION, Handler.prototype.handleAction, |
| 134 |
false, handlers[i]); |
| 135 |
goog.events.listen(targets[i], CHANGE, Handler.prototype.handleChange, |
| 136 |
false, handlers[i]); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
function createGlobalSubscriptions(pubsub, count) { |
| 141 |
initHandlers(count); |
| 142 |
initPublishers(pubsub, count); |
| 143 |
for (var i = 0; i < count; i++) { |
| 144 |
pubsub.subscribe(i + '.' + ACTION, Handler.prototype.handleAction, |
| 145 |
handlers[i]); |
| 146 |
pubsub.subscribe(i + '.' + CHANGE, Handler.prototype.handleChange, |
| 147 |
handlers[i]); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
function createSubscriptions(count) { |
| 152 |
initHandlers(count); |
| 153 |
initPubSubs(count); |
| 154 |
for (var i = 0; i < count; i++) { |
| 155 |
pubsubs[i].subscribe(ACTION, Handler.prototype.handleAction, |
| 156 |
handlers[i]); |
| 157 |
pubsubs[i].subscribe(CHANGE, Handler.prototype.handleChange, |
| 158 |
handlers[i]); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
function dispatchEvents(count) { |
| 163 |
for (var i = 0; i < count; i++) { |
| 164 |
for (var j = 0; j < 5; j++) { |
| 165 |
targets[i].fireEvent(ACTION); |
| 166 |
targets[i].fireEvent(CHANGE); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
function publishGlobalMessages(count) { |
| 172 |
for (var i = 0; i < count; i++) { |
| 173 |
for (var j = 0; j < 5; j++) { |
| 174 |
publishers[i].publish(ACTION); |
| 175 |
publishers[i].publish(CHANGE); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
function publishMessages(count) { |
| 181 |
for (var i = 0; i < count; i++) { |
| 182 |
for (var j = 0; j < 5; j++) { |
| 183 |
pubsubs[i].publish(ACTION); |
| 184 |
pubsubs[i].publish(CHANGE); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
function setUpPage() { |
| 190 |
timer = new goog.testing.PerformanceTimer(); |
| 191 |
timer.setNumSamples(10); |
| 192 |
timer.setTimeoutInterval(9000); |
| 193 |
timer.setDiscardOutliers(true); |
| 194 |
table = new goog.testing.PerformanceTable( |
| 195 |
goog.dom.getElement('perfTable'), timer); |
| 196 |
} |
| 197 |
|
| 198 |
function setUp() { |
| 199 |
actionCount = 0; |
| 200 |
changeCount = 0; |
| 201 |
handlers = []; |
| 202 |
publishers = []; |
| 203 |
pubsubs = []; |
| 204 |
targets = []; |
| 205 |
} |
| 206 |
|
| 207 |
function testCreateEventListeners() { |
| 208 |
table.run(goog.partial(createEventListeners, SAMPLES_PER_RUN), |
| 209 |
'1A: Create event listeners'); |
| 210 |
assertEquals(0, actionCount); |
| 211 |
assertEquals(0, changeCount); |
| 212 |
} |
| 213 |
|
| 214 |
function testCreateGlobalSubscriptions() { |
| 215 |
var pubsub = new goog.pubsub.PubSub(); |
| 216 |
table.run( |
| 217 |
goog.partial(createGlobalSubscriptions, pubsub, SAMPLES_PER_RUN), |
| 218 |
'1B: Create global subscriptions'); |
| 219 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 2, |
| 220 |
pubsub.getCount()); |
| 221 |
assertEquals(0, actionCount); |
| 222 |
assertEquals(0, changeCount); |
| 223 |
pubsub.dispose(); |
| 224 |
} |
| 225 |
|
| 226 |
function testCreateSubscripions() { |
| 227 |
table.run(goog.partial(createSubscriptions, SAMPLES_PER_RUN), |
| 228 |
'1C: Create subscriptions'); |
| 229 |
assertEquals(0, actionCount); |
| 230 |
assertEquals(0, changeCount); |
| 231 |
} |
| 232 |
|
| 233 |
function testDispatchEvents() { |
| 234 |
createEventListeners(SAMPLES_PER_RUN); |
| 235 |
table.run(goog.partial(dispatchEvents, SAMPLES_PER_RUN), |
| 236 |
'2A: Dispatch events'); |
| 237 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount); |
| 238 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount); |
| 239 |
} |
| 240 |
|
| 241 |
function testPublishGlobalMessages() { |
| 242 |
var pubsub = new goog.pubsub.PubSub(); |
| 243 |
createGlobalSubscriptions(pubsub, SAMPLES_PER_RUN); |
| 244 |
table.run( |
| 245 |
goog.partial(publishGlobalMessages, SAMPLES_PER_RUN), |
| 246 |
'2B: Publish global messages'); |
| 247 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount); |
| 248 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount); |
| 249 |
pubsub.dispose(); |
| 250 |
} |
| 251 |
|
| 252 |
function testPublishMessages() { |
| 253 |
createSubscriptions(SAMPLES_PER_RUN); |
| 254 |
table.run(goog.partial(publishMessages, SAMPLES_PER_RUN), |
| 255 |
'2C: Publish messages'); |
| 256 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount); |
| 257 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount); |
| 258 |
} |
| 259 |
|
| 260 |
function testEvents() { |
| 261 |
table.run(function() { |
| 262 |
createEventListeners(SAMPLES_PER_RUN); |
| 263 |
dispatchEvents(SAMPLES_PER_RUN); |
| 264 |
}, '3A: Events'); |
| 265 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount); |
| 266 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount); |
| 267 |
} |
| 268 |
|
| 269 |
function testSinglePubSub() { |
| 270 |
table.run(function() { |
| 271 |
var pubsub = new goog.pubsub.PubSub(); |
| 272 |
createGlobalSubscriptions(pubsub, SAMPLES_PER_RUN); |
| 273 |
publishGlobalMessages(SAMPLES_PER_RUN); |
| 274 |
pubsub.dispose(); |
| 275 |
}, '3B: Single PubSub'); |
| 276 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount); |
| 277 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount); |
| 278 |
} |
| 279 |
|
| 280 |
function testMultiPubSub() { |
| 281 |
table.run(function() { |
| 282 |
createSubscriptions(SAMPLES_PER_RUN); |
| 283 |
publishMessages(SAMPLES_PER_RUN); |
| 284 |
}, '3C: Multi PubSub'); |
| 285 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount); |
| 286 |
assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount); |
| 287 |
} |
| 288 |
</script> |
| 289 |
</body> |
| 290 |
</html> |