Skip to content

Commit

Permalink
feat: 🎸 use performance.now, safe "fn" reference, fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 24, 2020
1 parent 9e35351 commit 6aef516
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/plugins/expressions/common/ast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { ExpressionValue, ExpressionValueError } from '../expression_types';
import { ExpressionFunction } from '../../public';

export type ExpressionAstNode =
| ExpressionAstExpression
Expand Down Expand Up @@ -46,6 +47,11 @@ export interface ExpressionAstFunctionDebug {
*/
success: boolean;

/**
* Reference to the expression function this AST node represents.
*/
fn: ExpressionFunction;

/**
* Input that expression function received as its first argument.
*/
Expand Down
21 changes: 19 additions & 2 deletions src/plugins/expressions/common/execution/execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import { createUnitTestExecutor } from '../test_helpers';
import { ExpressionFunctionDefinition } from '../../public';
import { ExecutionContract } from './execution_contract';

beforeAll(() => {
if (typeof performance === 'undefined') {
(global as any).performance = { now: Date.now };
}
});

const createExecution = (
expression: string = 'foo bar=123',
context: Record<string, unknown> = {},
Expand Down Expand Up @@ -445,6 +451,16 @@ describe('Execution', () => {
}
});

test('stores "fn" reference to the function', async () => {
const execution = createExecution('add val=1 | add val=2 | add val=3', {}, true);
execution.start(-1);
await execution.result;

for (const node of execution.state.get().ast.chain) {
expect(node.debug?.fn.name).toBe('add');
}
});

test('saves duration it took to execute each function', async () => {
const execution = createExecution('add val=1 | add val=2 | add val=3', {}, true);
execution.start(-1);
Expand All @@ -465,7 +481,7 @@ describe('Execution', () => {
const node = execution.state.get().ast.chain[0];
expect(typeof node.debug?.duration).toBe('number');
expect(node.debug?.duration).toBeLessThan(50);
expect(node.debug?.duration).toBeGreaterThanOrEqual(10);
expect(node.debug?.duration).toBeGreaterThanOrEqual(5);
});

test('adds .debug field in expression AST on each executed function', async () => {
Expand Down Expand Up @@ -541,7 +557,7 @@ describe('Execution', () => {
);
});

test('sore debug information about sub-expressions', async () => {
test('store debug information about sub-expressions', async () => {
const execution = createExecution(
'add val={var_set name=foo value=5 | var name=foo} | add val=10',
{},
Expand Down Expand Up @@ -627,6 +643,7 @@ describe('Execution', () => {

expect(node2.debug).toMatchObject({
success: false,
fn: expect.any(Object),
input: expect.any(Object),
args: expect.any(Object),
error: expect.any(Object),
Expand Down
16 changes: 9 additions & 7 deletions src/plugins/expressions/common/execution/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ export class Execution<
}

const { function: fnName, arguments: fnArgs } = link;
const fnDef = getByAlias(this.state.get().functions, fnName);
const fn = getByAlias(this.state.get().functions, fnName);

if (!fnDef) {
if (!fn) {
return createError({ message: `Function ${fnName} could not be found.` });
}

Expand All @@ -209,15 +209,16 @@ export class Execution<
try {
// `resolveArgs` returns an object because the arguments themselves might
// actually have a `then` function which would be treated as a `Promise`.
const { resolvedArgs } = await this.resolveArgs(fnDef, input, fnArgs);
const { resolvedArgs } = await this.resolveArgs(fn, input, fnArgs);
args = resolvedArgs;
timeStart = this.params.debug ? Date.now() : 0;
const output = await this.invokeFunction(fnDef, input, resolvedArgs);
timeStart = this.params.debug ? performance.now() : 0;
const output = await this.invokeFunction(fn, input, resolvedArgs);

if (this.params.debug) {
const timeEnd: number = Date.now();
const timeEnd: number = performance.now();
(link as ExpressionAstFunction).debug = {
success: true,
fn,
input,
args: resolvedArgs,
output,
Expand All @@ -228,13 +229,14 @@ export class Execution<
if (getType(output) === 'error') return output;
input = output;
} catch (rawError) {
const timeEnd: number = this.params.debug ? Date.now() : 0;
const timeEnd: number = this.params.debug ? performance.now() : 0;
rawError.message = `[${fnName}] > ${rawError.message}`;
const error = createError(rawError) as ExpressionValueError;

if (this.params.debug) {
(link as ExpressionAstFunction).debug = {
success: false,
fn,
input,
args,
error,
Expand Down

0 comments on commit 6aef516

Please sign in to comment.