Code coverage report for core/src/profiling/profiler.ts

Statements: 31.58% (6 / 19)      Branches: 0% (0 / 14)      Functions: 0% (0 / 7)      Lines: 31.58% (6 / 19)      Ignored: none     

All files » core/src/profiling/ » profiler.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 791 1 1 1     1               1                                                                                                                                
import {Enabled, PropEnabled} from '../configurable';
import {Env} from '../env';
import {Providers} from '../providers';
import {joinFqn} from '../util';
 
 
export const ENV_PROFILER = 'profiler';
 
export type ProfilerOptions = {
  parent?: Profiler;
  env?: Env;
}
 
 
export class Profiler implements Enabled {
  readonly fqn: string;
 
  protected readonly _enabled: PropEnabled;
 
  protected readonly options: ProfilerOptions;
 
 
  constructor(
    readonly name: string,
    options: ProfilerOptions,
  ) {
    this.options = { ...options };
    this.fqn = joinFqn(this.name, options.parent);
    this._enabled = options.parent?._enabled.extend(this.name, 'true') ||
                    new PropEnabled(
                      { name: this.name, global: ENV_PROFILER },
                      Providers.useOrProvide(Env, this.options.env).getConfig(this.fqn),
                      'true',
                    )
  }
 
 
 
 
  enabled(label?: string, or?: () => string): boolean {
    return this._enabled.enabled(label || this.name, or);
  }
 
 
  label(name: string): string {
    return joinFqn(name, this.fqn, ':');
  }
 
 
  extend(name: string): Profiler {
    return new Profiler(name, {
      ...this.options,
      parent: this,
    });
  }
 
 
  profile(label: string): void {
    if (this.enabled(label)) {
      console.profile(this.label(label));
    }
  }
 
 
  profileEnd(label: string): void {
    if (this.enabled(label)) {
      console.profileEnd(this.label(label));
    }
  }
 
 
  timeStamp(label: string): void {
    if (this.enabled(label)) {
      console.timeStamp(this.label(label));
    }
  }
}