tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

performance-marker.js (2587B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 "use strict";
      5 
      6 /**
      7 * This function returns a middleware, which is responsible for adding markers that will
      8 * be visible in performance profiles, and may help investigate performance issues.
      9 *
     10 * Example usage, adding a marker when console messages are added, and when they are cleared:
     11 *
     12 * return createPerformanceMarkerMiddleware({
     13 *   "MESSAGES_ADD": {
     14 *     label: "WebconsoleAddMessages",
     15 *     sessionId: 12345,
     16 *     getMarkerDescription: function({ action, state }) {
     17 *       const { messages } = action;
     18 *       const totalMessageCount = state.messages.mutableMessagesById.size;
     19 *       return `${messages.length} messages handled, store now has ${totalMessageCount} messages`;
     20 *     },
     21 *   },
     22 *   "MESSAGES_CLEARED": {
     23 *     label: "WebconsoleClearMessages",
     24 *     sessionId: 12345
     25 *   },
     26 * });
     27 *
     28 * @param {object} cases: An object, keyed by action type, that will determine if a
     29 *         given action will add a marker.
     30 * @param {string} cases.{actionType} - The type of the action that will trigger the
     31 *         marker creation.
     32 * @param {string} cases.{actionType}.label - The marker label
     33 * @param {Integer} cases.{actionType}.sessionId - The telemetry sessionId. This is used
     34 *        to be able to distinguish markers coming from different toolboxes.
     35 * @param {Function} [cases.{actionType}.getMarkerDescription] - An optional function that
     36 *        will be called when adding the marker to populate its description. The function
     37 *        is called with an object holding the action and the state
     38 */
     39 function createPerformanceMarkerMiddleware(cases) {
     40  return function (store) {
     41    return next => action => {
     42      const condition = cases[action.type];
     43      const shouldAddProfileMarker = !!condition;
     44 
     45      // Start the marker timer before calling next(action).
     46      const startTime = shouldAddProfileMarker ? ChromeUtils.now() : null;
     47      const newState = next(action);
     48 
     49      if (shouldAddProfileMarker) {
     50        ChromeUtils.addProfilerMarker(
     51          `${condition.label} ${condition.sessionId}`,
     52          startTime,
     53          condition.getMarkerDescription
     54            ? condition.getMarkerDescription({
     55                action,
     56                state: store.getState(),
     57              })
     58            : ""
     59        );
     60      }
     61      return newState;
     62    };
     63  };
     64 }
     65 
     66 module.exports = {
     67  createPerformanceMarkerMiddleware,
     68 };