aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/pubsub/pubsub_perf.html
blob: 8294b3936b6de1c1390644ebd4b71eac2e108a1c (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
Author:  attila@google.com (Attila Bodis)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Performance Tests - goog.pubsub.PubSub</title>
  <link rel="stylesheet" href="../testing/performancetable.css" />
  <script src="../base.js"></script>
  <script>
    goog.require('goog.events');
    goog.require('goog.events.EventTarget');
    goog.require('goog.pubsub.PubSub');
    goog.require('goog.testing.PerformanceTable');
    goog.require('goog.testing.PerformanceTimer');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
  <h1>goog.pubsub.PubSub Performance Tests</h1>
  <p>
    <b>User-agent:</b> <script>document.write(navigator.userAgent);</script>
  </p>
  <p>
    Compares the performance of the event system (<code>goog.events.*</code>)
    with the <code>goog.pubsub.PubSub</code> class.
  </p>
  <p>
    The baseline test creates 1000 event targets and 1000 objects that handle
    events dispatched by the event targets, and has each event target dispatch
    2 events 5 times each.
  </p>
  <p>
    The single-<code>PubSub</code> test creates 1000 publishers, 1000
    subscribers, and a single pubsub channel.  Each subscriber subscribes to
    topics on the same pubsub channel.  Each publisher publishes 5 messages to
    2 topics each via the pubsub channel.
  </p>
  <p>
    The multi-<code>PubSub</code> test creates 1000 publishers that are
    subclasses of <code>goog.pubsub.PubSub</code> and 1000 subscribers.  Each
    subscriber subscribes to its own publisher.  Each publisher publishes 5
    messages to 2 topics each via its own pubsub channel.
  </p>
  <div id="perfTable"></div>
  <hr>
  <script>
    var targets, publishers, pubsubs, handlers;

    // Number of objects to test per run.
    var SAMPLES_PER_RUN = 1000;

    // The performance table & performance timer.
    var table, timer;

    // Event/topic identifiers.
    var ACTION = 'action';
    var CHANGE = 'change';

    // Number of times handlers have been called.
    var actionCount = 0;
    var changeCount = 0;

    // Generic event handler class.
    function Handler() {
    }
    Handler.prototype.handleAction = function() {
      actionCount++;
    };
    Handler.prototype.handleChange = function() {
      changeCount++;
    };

    // Generic publisher class that uses a global pubsub channel.
    function Publisher(pubsub, id) {
      this.pubsub = pubsub;
      this.id = id;
    }
    Publisher.prototype.publish = function(topic) {
      this.pubsub.publish(this.id + '.' + topic);
    };

    // PubSub subclass; allows clients to subscribe and uses itself to publish.
    function PubSub() {
      goog.pubsub.PubSub.call(this);
    }
    goog.inherits(PubSub, goog.pubsub.PubSub);

    // EventTarget subclass; uses goog.events.* to dispatch events.
    function Target() {
      goog.events.EventTarget.call(this);
    }
    goog.inherits(Target, goog.events.EventTarget);
    Target.prototype.fireEvent = function(type) {
      this.dispatchEvent(type);
    };

    function initHandlers(count) {
      for (var i = 0; i < count; i++) {
        handlers[i] = new Handler();
      }
    }

    function initPublishers(pubsub, count) {
      for (var i = 0; i < count; i++) {
        publishers[i] = new Publisher(pubsub, i);
      }
    }

    function initPubSubs(count) {
      for (var i = 0; i < count; i++) {
        pubsubs[i] = new PubSub();
      }
    }

    function initTargets(count) {
      for (var i = 0; i < count; i++) {
        targets[i] = new Target();
      }
    }

    function createEventListeners(count) {
      initHandlers(count);
      initTargets(count);
      for (var i = 0; i < count; i++) {
        goog.events.listen(targets[i], ACTION, Handler.prototype.handleAction,
            false, handlers[i]);
        goog.events.listen(targets[i], CHANGE, Handler.prototype.handleChange,
            false, handlers[i]);
      }
    }

    function createGlobalSubscriptions(pubsub, count) {
      initHandlers(count);
      initPublishers(pubsub, count);
      for (var i = 0; i < count; i++) {
        pubsub.subscribe(i + '.' + ACTION, Handler.prototype.handleAction,
            handlers[i]);
        pubsub.subscribe(i + '.' + CHANGE, Handler.prototype.handleChange,
            handlers[i]);
      }
    }

    function createSubscriptions(count) {
      initHandlers(count);
      initPubSubs(count);
      for (var i = 0; i < count; i++) {
        pubsubs[i].subscribe(ACTION, Handler.prototype.handleAction,
            handlers[i]);
        pubsubs[i].subscribe(CHANGE, Handler.prototype.handleChange,
            handlers[i]);
      }
    }

    function dispatchEvents(count) {
      for (var i = 0; i < count; i++) {
        for (var j = 0; j < 5; j++) {
          targets[i].fireEvent(ACTION);
          targets[i].fireEvent(CHANGE);
        }
      }
    }

    function publishGlobalMessages(count) {
      for (var i = 0; i < count; i++) {
        for (var j = 0; j < 5; j++) {
          publishers[i].publish(ACTION);
          publishers[i].publish(CHANGE);
        }
      }
    }

    function publishMessages(count) {
      for (var i = 0; i < count; i++) {
        for (var j = 0; j < 5; j++) {
          pubsubs[i].publish(ACTION);
          pubsubs[i].publish(CHANGE);
        }
      }
    }

    function setUpPage() {
      timer = new goog.testing.PerformanceTimer();
      timer.setNumSamples(10);
      timer.setTimeoutInterval(9000);
      timer.setDiscardOutliers(true);
      table = new goog.testing.PerformanceTable(
          goog.dom.getElement('perfTable'), timer);
    }

    function setUp() {
      actionCount = 0;
      changeCount = 0;
      handlers = [];
      publishers = [];
      pubsubs = [];
      targets = [];
    }

    function tearDown() {
      goog.events.removeAll();
    }

    function testCreateEventListeners() {
      table.run(goog.partial(createEventListeners, SAMPLES_PER_RUN),
          '1A: Create event listeners');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 2,
          goog.events.getTotalListenerCount());
      assertEquals(0, actionCount);
      assertEquals(0, changeCount);
    }

    function testCreateGlobalSubscriptions() {
      var pubsub = new goog.pubsub.PubSub();
      table.run(
          goog.partial(createGlobalSubscriptions, pubsub, SAMPLES_PER_RUN),
          '1B: Create global subscriptions');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 2,
          pubsub.getCount());
      assertEquals(0, actionCount);
      assertEquals(0, changeCount);
      pubsub.dispose();
    }

    function testCreateSubscripions() {
      table.run(goog.partial(createSubscriptions, SAMPLES_PER_RUN),
          '1C: Create subscriptions');
      assertEquals(0, actionCount);
      assertEquals(0, changeCount);
    }

    function testDispatchEvents() {
      createEventListeners(SAMPLES_PER_RUN);
      table.run(goog.partial(dispatchEvents, SAMPLES_PER_RUN),
          '2A: Dispatch events');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount);
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount);
    }

    function testPublishGlobalMessages() {
      var pubsub = new goog.pubsub.PubSub();
      createGlobalSubscriptions(pubsub, SAMPLES_PER_RUN);
      table.run(
          goog.partial(publishGlobalMessages, SAMPLES_PER_RUN),
          '2B: Publish global messages');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount);
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount);
      pubsub.dispose();
    }

    function testPublishMessages() {
      createSubscriptions(SAMPLES_PER_RUN);
      table.run(goog.partial(publishMessages, SAMPLES_PER_RUN),
          '2C: Publish messages');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount);
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount);
    }

    function testEvents() {
      table.run(function() {
        createEventListeners(SAMPLES_PER_RUN);
        dispatchEvents(SAMPLES_PER_RUN);
      }, '3A: Events');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 2,
          goog.events.getTotalListenerCount());
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount);
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount);
    }

    function testSinglePubSub() {
      table.run(function() {
        var pubsub = new goog.pubsub.PubSub();
        createGlobalSubscriptions(pubsub, SAMPLES_PER_RUN);
        publishGlobalMessages(SAMPLES_PER_RUN);
        pubsub.dispose();
      }, '3B: Single PubSub');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount);
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount);
    }

    function testMultiPubSub() {
      table.run(function() {
        createSubscriptions(SAMPLES_PER_RUN);
        publishMessages(SAMPLES_PER_RUN);
      }, '3C: Multi PubSub');
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, actionCount);
      assertEquals(SAMPLES_PER_RUN * timer.getNumSamples() * 5, changeCount);
    }
  </script>
</body>
</html>