Skip to content

Commit

Permalink
Adding micro benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Aug 27, 2020
1 parent 4ebbe98 commit 97e19c0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createAgentAction } from './actions';
import { SavedObject } from 'kibana/server';
import { AgentAction } from '../../../common/types/models';
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { esKuery } from '../../../../../../src/plugins/data/server';

describe('test agent actions services', () => {
it('should create a new action', async () => {
Expand Down Expand Up @@ -35,3 +36,71 @@ describe('test agent actions services', () => {
expect(createdAction?.sent_at).toEqual(newAgentAction.sent_at);
});
});

class Benchmark {
private static NS_PER_SEC = 1e9;
#startTime?: [number, number];
#elapsed?: [number, number];

start() {
if (this.#startTime) {
throw new Error(`Start?? We're started, we can't start again!`);
}

this.#startTime = process.hrtime();
}
stop() {
if (this.#startTime == null) {
throw new Error(`Stop?? We haven't even started yet!`);
}

this.#elapsed = process.hrtime(this.#startTime);
}

describe() {
if (this.#elapsed == null) {
return `Benchmark is still running`;
}

return `Benchmark took ${
this.#elapsed[0] * Benchmark.NS_PER_SEC + this.#elapsed[1]
} nanoseconds`;
}
}

describe('micro-benchmark', () => {
test('parsing KQL expression', () => {
const b = new Benchmark();
b.start();
esKuery.fromKueryExpression(
'not fleet-agent-actions.attributes.sent_at: * and fleet-agent-actions.attributes.agent_id:1234567'
);
b.stop();
console.log(b.describe());
});

test('manually building KueryNode', () => {
const b = new Benchmark();
b.start();
esKuery.nodeTypes.function.buildNode('and', [
esKuery.nodeTypes.function.buildNode(
'not',
esKuery.nodeTypes.function.buildNode('is', 'fleet-agent-actions.attributes.sent_at', '*')
),
esKuery.nodeTypes.function.buildNode(
'is',
'fleet-agent-actions.attributes.agent_id',
'1234567'
),
]);
b.stop();
console.log(b.describe());
});

test('doing nothing', () => {
const b = new Benchmark();
b.start();
b.stop();
console.log(b.describe());
});
});
18 changes: 9 additions & 9 deletions x-pack/plugins/ingest_manager/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Agent, AgentAction, AgentActionSOAttributes } from '../../../common/typ
import { AGENT_ACTION_SAVED_OBJECT_TYPE } from '../../../common/constants';
import { savedObjectToAgentAction } from './saved_objects';
import { appContextService } from '../app_context';
import { nodeTypes } from '../../../../../../src/plugins/data/common';
import { esKuery } from '../../../../../../src/plugins/data/server';

export async function createAgentAction(
soClient: SavedObjectsClientContract,
Expand All @@ -30,16 +30,16 @@ export async function getAgentActionsForCheckin(
soClient: SavedObjectsClientContract,
agentId: string
): Promise<AgentAction[]> {
const filter = nodeTypes.function.buildNode('and', [
nodeTypes.function.buildNode(
const filter = esKuery.nodeTypes.function.buildNode('and', [
esKuery.nodeTypes.function.buildNode(
'not',
nodeTypes.function.buildNode(
esKuery.nodeTypes.function.buildNode(
'is',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`,
'*'
)
),
nodeTypes.function.buildNode(
esKuery.nodeTypes.function.buildNode(
'is',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.agent_id`,
agentId
Expand Down Expand Up @@ -94,16 +94,16 @@ export async function getAgentActionByIds(
}

export async function getNewActionsSince(soClient: SavedObjectsClientContract, timestamp: string) {
const filter = nodeTypes.function.buildNode('and', [
nodeTypes.function.buildNode(
const filter = esKuery.nodeTypes.function.buildNode('and', [
esKuery.nodeTypes.function.buildNode(
'not',
nodeTypes.function.buildNode(
esKuery.nodeTypes.function.buildNode(
'is',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`,
'*'
)
),
nodeTypes.function.buildNode(
esKuery.nodeTypes.function.buildNode(
'range',
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.created_at`,
{
Expand Down

0 comments on commit 97e19c0

Please sign in to comment.