<commit_before><!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/extras/chrome/chrome_user_friendly_category_driver.html">
<link rel="import" href="/tracing/metrics/metric_registry.html">
<link rel="import" href="/tracing/model/helpers/chrome_model_helper.html">
<link rel="import" href="/tracing/value/histogram.html">

<script>
'use strict';

tr.exportTo('tr.metrics.sh', function() {
  var LONG_TASK_MS = 50;

  // Anything longer than this should be so rare that its length beyond this is
  // uninteresting.
  var LONGEST_TASK_MS = 1000;

  /**
   * This helper function calls |cb| for each of the top-level tasks on the
   * given thread in the given range whose duration is longer than LONG_TASK_MS.
   *
   * @param {tr.model.Thread} thread
   * @param {tr.b.Range=} opt_range
   * @param {function()} cb
   * @param {Object=} opt_this
   */
  function iterateLongTopLevelTasksOnThreadInRange(
      thread, opt_range, cb, opt_this) {
    thread.sliceGroup.topLevelSlices.forEach(function(slice) {
      if (opt_range &&
          !opt_range.intersectsExplicitRangeInclusive(slice.start, slice.end))
        return;

      if (slice.duration < LONG_TASK_MS)
        return;

      cb.call(opt_this, slice);
    });
  }

  /**
   * This helper function calls |cb| for each of the main renderer threads in
   * the model.
   *
   * @param {tr.model.Model} model The model.
   * @param {function()} cb Callback.
   * @param {Object=} opt_this Context object.
   */
  function iterateRendererMainThreads(model, cb, opt_this) {
    var modelHelper = model.getOrCreateHelper(
        tr.model.helpers.ChromeModelHelper);
    tr.b.dictionaryValues(modelHelper.rendererHelpers).forEach(
        function(rendererHelper) {
          if (!rendererHelper.mainThread)
            return;
          cb.call(opt_this, rendererHelper.mainThread);
        });
  }

  /**
   * This metric directly measures long tasks on renderer main threads.
   * This metric requires only the 'toplevel' tracing category.
   *
   * @param {!tr.v.HistogramSet} values
   * @param {!tr.model.Model} model
   * @param {!Object=} opt_options
   */
  function longTasksMetric(values, model, opt_options) {
    var rangeOfInterest = opt_options ? opt_options.rangeOfInterest : undefined;
    var longTaskHist = new tr.v.Histogram('long tasks',
        tr.b.Unit.byName.timeDurationInMs_smallerIsBetter,<commit_msg>Fix bug in long tasks metric.<commit_after><!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/extras/chrome/chrome_user_friendly_category_driver.html">
<link rel="import" href="/tracing/metrics/metric_registry.html">
<link rel="import" href="/tracing/model/helpers/chrome_model_helper.html">
<link rel="import" href="/tracing/value/histogram.html">

<script>
'use strict';

tr.exportTo('tr.metrics.sh', function() {
  var LONG_TASK_MS = 50;

  // Anything longer than this should be so rare that its length beyond this is
  // uninteresting.
  var LONGEST_TASK_MS = 1000;

  /**
   * This helper function calls |cb| for each of the top-level tasks on the
   * given thread in the given range whose duration is longer than LONG_TASK_MS.
   *
   * @param {tr.model.Thread} thread
   * @param {tr.b.Range=} opt_range
   * @param {function()} cb
   * @param {Object=} opt_this
   */
  function iterateLongTopLevelTasksOnThreadInRange(
      thread, opt_range, cb, opt_this) {
    thread.sliceGroup.topLevelSlices.forEach(function(slice) {
      if (opt_range &&
          !opt_range.intersectsExplicitRangeInclusive(slice.start, slice.end))
        return;

      if (slice.duration < LONG_TASK_MS)
        return;

      cb.call(opt_this, slice);
    });
  }

  /**
   * This helper function calls |cb| for each of the main renderer threads in
   * the model.
   *
   * @param {tr.model.Model} model The model.
   * @param {function()} cb Callback.
   * @param {Object=} opt_this Context object.
   */
  function iterateRendererMainThreads(model, cb, opt_this) {
    var modelHelper = model.getOrCreateHelper(
        tr.model.helpers.ChromeModelHelper);
    if (modelHelper !== undefined) {
      tr.b.dictionaryValues(modelHelper.rendererHelpers).forEach(
          function(rendererHelper) {
            if (!rendererHelper.mainThread)
              return;
            cb.call(opt_this, rendererHelper.mainThread);
          });
    }
  }

  /**
   * This metric directly measures long tasks on renderer main threads.
   * This metric requires only the 'toplevel' tracing category.
   *
   * @param {!tr.v.HistogramSet} values
   * @param {!tr.model.Model} model
   * @param {!Object=} opt_options
   */
  function longTasksMetric(values, model, opt_options) {
    var rangeOfInterest = opt_options ? opt_options.rangeOfInterest : undefined;
    var longTaskHist = new tr.v.Histogram('long tasks',
        tr.b.Unit.byName.timeDurationInMs_smallerIsBetter,
{'fraction_non_alphanumeric': 0.11328049487724724, 'fraction_numerical': 0.0038662284941040014, 'mean_word_length': 3.3662447257383965, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2767280', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1600, 'n_words': 423}