Code coverage report for core/src/apm/console/timer.ts

Statements: 65.22% (15 / 23)      Branches: 50% (2 / 4)      Functions: 62.5% (5 / 8)      Lines: 65.22% (15 / 23)      Ignored: none     

All files » core/src/apm/console/ » timer.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    1     1             3         3 3   3 3   3 3 3                   3             3           1         2         3                  
import {Tags} from '../../util';
import {TimerWrapper, TimerWrapperFactory} from '../timed';
import {ConsoleMetric, ConsoleMetricOptions} from './metric';
 
 
export class ConsoleTimer<T> extends ConsoleMetric implements TimerWrapper<T> {
 
  constructor(
    name: string,
    tags: Tags,
    options: ConsoleMetricOptions,
  ) {
    super(name, tags, options, 'timer');
  }
 
 
  wrap(func: (...args: unknown[]) => T): (...args: unknown[]) => T {
    const _console = this.console;
    const label = this.fqn;
 
    return function consoleTimerWrapper(...args: unknown[]): T {
      _console.time(label);
      let result: T;
      try {
        result = func(...args);
        Iif (result instanceof Promise) {
          result.then(result => {
            _console.timeEnd(label);
            return result;
          }).catch(err => {
            _console.timeEnd(label);
            return err;
          });
        }
        else {
          _console.timeEnd(label);
        }
      }
      catch (error) {
        _console.timeEnd(label);
        throw error;
      }
      return result;
    };
  }
}
 
 
export class ConsoleTimerFactory implements TimerWrapperFactory {
  protected readonly options: ConsoleMetricOptions;
 
 
  constructor(options: ConsoleMetricOptions) {
    this.options = { ...options };
  }
 
 
  asyncTimer<T>(name: string, tags: Tags = {}): TimerWrapper<Promise<T>> {
    return new ConsoleTimer(name, tags, this.options);
  }
 
 
  timer<T>(name: string, tags: Tags = {}): TimerWrapper<T> {
    return new ConsoleTimer(name, tags, this.options);
  }
 
}