<commit_before>/**
 * Copyright 2016 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

const TracingProcessor = require('../../../lib/traces/tracing-processor');
const assert = require('assert');

/* eslint-env mocha */

const defaultPercentiles = [0, 0.25, 0.5, 0.75, 0.9, 0.99, 1];

/**
 * Create a riskPercentiles result object by matching the values in percentiles
 * and times.
 * @param {!Array<number>} percentiles
 * @param {!Array<number>} times
 * @return {!Array<{percentile: number, time: number}>}
 */
function createRiskPercentiles(percentiles, times) {
  return percentiles.map((percentile, index) => {
    return {
      percentile,
      time: times[index]
    };
  });
}

describe('TracingProcessor lib', () => {
  describe('riskPercentiles calculation', () => {
    it('correctly calculates percentiles of no tasks', () => {
      const results = TracingProcessor._riskPercentiles([], 100, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles, [16, 16, 16, 16, 16, 16, 16]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of a single task with idle time', () => {
      const results = TracingProcessor._riskPercentiles([50], 100, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles, [16, 16, 16, 41, 56, 65, 66]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of a single task with no idle time', () => {
      const results = TracingProcessor._riskPercentiles([50], 50, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles,
          [16, 28.5, 41, 53.5, 61, 65.5, 66]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of several equal-length tasks', () => {
      const results = TracingProcessor._riskPercentiles([50, 50, 50, 50], 400, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles, [16, 16, 16, 41, 56, 65, 66]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of tasks including zero-length durations', () => {<commit_msg>add test for traceviewer's require not throwing.<commit_after>/**
 * Copyright 2016 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

let TracingProcessor;
const assert = require('assert');

/* eslint-env mocha */

const defaultPercentiles = [0, 0.25, 0.5, 0.75, 0.9, 0.99, 1];

/**
 * Create a riskPercentiles result object by matching the values in percentiles
 * and times.
 * @param {!Array<number>} percentiles
 * @param {!Array<number>} times
 * @return {!Array<{percentile: number, time: number}>}
 */
function createRiskPercentiles(percentiles, times) {
  return percentiles.map((percentile, index) => {
    return {
      percentile,
      time: times[index]
    };
  });
}

describe('TracingProcessor lib', () => {
  it('doesn\'t throw when module is loaded', () => {
    assert.doesNotThrow(_ => {
      TracingProcessor = require('../../../lib/traces/tracing-processor');
    });
  });

  describe('riskPercentiles calculation', () => {
    it('correctly calculates percentiles of no tasks', () => {
      const results = TracingProcessor._riskPercentiles([], 100, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles, [16, 16, 16, 16, 16, 16, 16]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of a single task with idle time', () => {
      const results = TracingProcessor._riskPercentiles([50], 100, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles, [16, 16, 16, 41, 56, 65, 66]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of a single task with no idle time', () => {
      const results = TracingProcessor._riskPercentiles([50], 50, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles,
          [16, 28.5, 41, 53.5, 61, 65.5, 66]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of several equal-length tasks', () => {
      const results = TracingProcessor._riskPercentiles([50, 50, 50, 50], 400, defaultPercentiles);
      const expected = createRiskPercentiles(defaultPercentiles, [16, 16, 16, 41, 56, 65, 66]);
      assert.deepEqual(results, expected);
    });

    it('correctly calculates percentiles of tasks including zero-length durations', () => {
{'fraction_non_alphanumeric': 0.1264265335235378, 'fraction_numerical': 0.037446504992867335, 'mean_word_length': 3.894415357766143, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6197363', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1529, 'n_words': 526}