<commit_before>/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict
 * @format
 */

'use strict';

const Systrace = require('../Performance/Systrace');

const infoLog = require('./infoLog');
const performanceNow: () => number =
  global.nativeQPLTimestamp ?? global.performance.now.bind(global.performance);

type Timespan = {
  startTime: number,
  endTime?: number,
  totalTime?: number,
};

// Extra values should be serializable primitives
type ExtraValue = number | string | boolean;

export interface IPerformanceLogger {
  addTimespan(key: string, startTime: number, endTime: number): void;
  startTimespan(key: string): void;
  stopTimespan(key: string, options?: {update?: boolean}): void;
  clear(): void;
  clearCompleted(): void;
  currentTimestamp(): number;
  getTimespans(): {[key: string]: Timespan, ...};
  hasTimespan(key: string): boolean;
  setExtra(key: string, value: ExtraValue): void;
  getExtras(): {[key: string]: ExtraValue, ...};
  removeExtra(key: string): ExtraValue | void;
  markPoint(key: string, timestamp?: number): void;
  getPoints(): {[key: string]: number, ...};
  logEverything(): void;
}

const _cookies: {[key: string]: number, ...} = {};

const PRINT_TO_CONSOLE: false = false; // Type as false to prevent accidentally committing `true`;

class PerformanceLogger implements IPerformanceLogger {
  _timespans: {[key: string]: Timespan} = {};
  _extras: {[key: string]: ExtraValue} = {};
  _points: {[key: string]: number} = {};

  addTimespan(key: string, startTime: number, endTime: number) {
    if (this._timespans[key]) {
      if (PRINT_TO_CONSOLE && __DEV__) {
        infoLog(
          'PerformanceLogger: Attempting to add a timespan that already exists ',
          key,
        );
      }
      return;
    }

    this._timespans[key] = {
      startTime,
      endTime,
      totalTime: endTime - (startTime || 0),
    };
  }

  startTimespan(key: string) {
    if (this._timespans[key]) {
      if (PRINT_TO_CONSOLE && __DEV__) {
        infoLog(
          'PerformanceLogger: Attempting to start a timespan that already exists ',
          key,
        );
      }
      return;
    }

    this._timespans[key] = {
      startTime: performanceNow(),
    };
    _cookies[key] = Systrace.beginAsyncEvent(key);
    if (PRINT_TO_CONSOLE) {
      infoLog('PerformanceLogger.js', 'start: ' + key);
    }
  }

  stopTimespan(key: string, options?: {update?: boolean}) {
    const timespan = this._timespans[key];
    if (!timespan || timespan.startTime == null) {
      if (PRINT_TO_CONSOLE && __DEV__) {
        infoLog(
          'PerformanceLogger: Attempting to end a timespan that has not started ',
          key,
        );
      }
      return;
    }
    if (timespan.endTime != null && !options?.update) {
      if (PRINT_TO_CONSOLE && __DEV__) {<commit_msg>Remove update option from stopTimestamp method in performance loggers<commit_after>/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict
 * @format
 */

'use strict';

const Systrace = require('../Performance/Systrace');

const infoLog = require('./infoLog');
const performanceNow: () => number =
  global.nativeQPLTimestamp ?? global.performance.now.bind(global.performance);

type Timespan = {
  startTime: number,
  endTime?: number,
  totalTime?: number,
};

// Extra values should be serializable primitives
type ExtraValue = number | string | boolean;

export interface IPerformanceLogger {
  addTimespan(key: string, startTime: number, endTime: number): void;
  startTimespan(key: string): void;
  stopTimespan(key: string): void;
  clear(): void;
  clearCompleted(): void;
  currentTimestamp(): number;
  getTimespans(): {[key: string]: Timespan, ...};
  hasTimespan(key: string): boolean;
  setExtra(key: string, value: ExtraValue): void;
  getExtras(): {[key: string]: ExtraValue, ...};
  removeExtra(key: string): ExtraValue | void;
  markPoint(key: string, timestamp?: number): void;
  getPoints(): {[key: string]: number, ...};
  logEverything(): void;
}

const _cookies: {[key: string]: number, ...} = {};

const PRINT_TO_CONSOLE: false = false; // Type as false to prevent accidentally committing `true`;

class PerformanceLogger implements IPerformanceLogger {
  _timespans: {[key: string]: Timespan} = {};
  _extras: {[key: string]: ExtraValue} = {};
  _points: {[key: string]: number} = {};

  addTimespan(key: string, startTime: number, endTime: number) {
    if (this._timespans[key]) {
      if (PRINT_TO_CONSOLE && __DEV__) {
        infoLog(
          'PerformanceLogger: Attempting to add a timespan that already exists ',
          key,
        );
      }
      return;
    }

    this._timespans[key] = {
      startTime,
      endTime,
      totalTime: endTime - (startTime || 0),
    };
  }

  startTimespan(key: string) {
    if (this._timespans[key]) {
      if (PRINT_TO_CONSOLE && __DEV__) {
        infoLog(
          'PerformanceLogger: Attempting to start a timespan that already exists ',
          key,
        );
      }
      return;
    }

    this._timespans[key] = {
      startTime: performanceNow(),
    };
    _cookies[key] = Systrace.beginAsyncEvent(key);
    if (PRINT_TO_CONSOLE) {
      infoLog('PerformanceLogger.js', 'start: ' + key);
    }
  }

  stopTimespan(key: string) {
    const timespan = this._timespans[key];
    if (!timespan || timespan.startTime == null) {
      if (PRINT_TO_CONSOLE && __DEV__) {
        infoLog(
          'PerformanceLogger: Attempting to end a timespan that has not started ',
          key,
        );
      }
      return;
    }
    if (timespan.endTime != null) {
      if (PRINT_TO_CONSOLE && __DEV__) {
{'fraction_non_alphanumeric': 0.13591740013540962, 'fraction_numerical': 0.00033852403520649965, 'mean_word_length': 3.1730225988700567, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1451534', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1812, 'n_words': 497}