pubsub_perf.html 8.9 KB

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