# API `@deepkit/stopwatch`

```shell
npm install @deepkit/stopwatch
```

## API reference of @deepkit/stopwatch

### StopwatchStore (class)

```typescript
export abstract class StopwatchStore {
    frameQueue: (FrameStart | FrameEnd)[];
    dataQueue: FrameData[];
    analytics: AnalyticData[];
    async close();
    abstract run<T>(data: {
        [name: string]: any;
    }, cb: () => Promise<T>): Promise<T>;
    abstract getZone(): {
        [name: string]: any;
    } | undefined;
    data(data: FrameData);
    collectAnalytics();
    add(frame: FrameStart | FrameEnd): void;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/stopwatch.ts#L14

### StopwatchFrameInterface (type)

```typescript
interface StopwatchFrameInterface<C extends FrameCategory> {
    data(data: TypeOfCategory<C>): void;

    end(): void;

    run<T>(cb: () => Promise<T> | T, data?: { [name: string]: any }): Promise<T>;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/stopwatch.ts#L81

### StopwatchFrame (class)

```typescript
export class StopwatchFrame<C extends FrameCategory> implements StopwatchFrameInterface<C> {
    constructor(protected store: StopwatchStore, public context: number, public category: number, public cid: number);
    data(data: Partial<TypeOfCategory<C>>);
    end();
    run<T>(cb: () => Promise<T>, data: {
        [name: string]: any;
    } = {}): Promise<T>;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/stopwatch.ts#L89

### NoopStopwatchFrame (class)

```typescript
export class NoopStopwatchFrame<C extends FrameCategory> implements StopwatchFrameInterface<C> {
    data(data: any);
    end();
    run<T>(cb: () => Promise<T>, data: {
        [p: string]: any;
    } = {}): Promise<T>;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/stopwatch.ts#L117

### Stopwatch (class)

```typescript
export class Stopwatch {
    /**
     * It's active when there is a StopwatchStore attached.
     * Per default its inactive.
     */
    active: boolean;
    constructor(protected store?: StopwatchStore);
    enable();
    disable();
    /**
     * Please check Stopwatch.active before using this method.
     *
     * When a new context is created, it's important to use StopwatchFrame.run() so that all
     * sub frames are correctly assigned to the new context.
     */
    start<C extends FrameCategory>(label: string, category: C = FrameCategory.none as C, newContext: boolean = false): StopwatchFrameInterface<C>;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/stopwatch.ts#L132

### getTypeOfCategory (function)

```typescript
(category: FrameCategory): Type | undefined
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L46

### encodeCompoundKey (function)

```typescript
(id: number, worker: number): number
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L81

### incrementCompoundKey (function)

```typescript
(cid: number, id: number): number
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L85

### decodeCompoundKey (function)

```typescript
(cid: number): [id: number, worker: number]
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L89

### AnalyticData (type)

```typescript
type AnalyticData = {
    timestamp: number;
    cpu: number;
    memory: number;
    loopBlocked: number;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L3

### FrameCategoryData (type)

```typescript
interface FrameCategoryData {
    [FrameCategory.http]: {
        url?: string;
        method?: string;
        clientIp?: string;
        responseStatus?: number;
    };
    [FrameCategory.rpc]: {
        method: string;
        controller: string;
        arguments: any[];
    };
    [FrameCategory.database]: {
        collection?: string;
        className?: string;
    };
    [FrameCategory.databaseQuery]: {
        sql: string;
        sqlParams?: any[];
    };
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L20

### TypeOfCategory (type)

```typescript
type TypeOfCategory<C extends FrameCategory> = C extends keyof FrameCategoryData ? Partial<FrameCategoryData[C]> : undefined;
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L42

### FrameStart (type)

```typescript
interface FrameStart {
    cid: number;
    type: FrameType.start;
    timestamp: number;
    context: number;
    label: string;
    category: FrameCategory;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L60

### FrameEnd (type)

```typescript
interface FrameEnd {
    cid: number;
    type: FrameType.end;
    timestamp: number;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L69

### FrameData (type)

```typescript
interface FrameData {
    cid: number;
    category: FrameCategory;
    data: any;
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L75

### Frame (class)

```typescript
export class Frame {
    end?: number;
    data?: any;
    constructor(public cid: number, public start: number, public context: number, public label: string, public category: FrameCategory);
}
```

Source: https://github.com/deepkit/deepkit-framework/blob/master/packages/stopwatch/src/types.ts#L95
