Skip to content

Commit 97e19c0

Browse files
committed
Adding micro benchmark
1 parent 4ebbe98 commit 97e19c0

File tree

2 files changed

+78
-9
lines changed

2 files changed

+78
-9
lines changed

x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createAgentAction } from './actions';
88
import { SavedObject } from 'kibana/server';
99
import { AgentAction } from '../../../common/types/models';
1010
import { savedObjectsClientMock } from 'src/core/server/mocks';
11+
import { esKuery } from '../../../../../../src/plugins/data/server';
1112

1213
describe('test agent actions services', () => {
1314
it('should create a new action', async () => {
@@ -35,3 +36,71 @@ describe('test agent actions services', () => {
3536
expect(createdAction?.sent_at).toEqual(newAgentAction.sent_at);
3637
});
3738
});
39+
40+
class Benchmark {
41+
private static NS_PER_SEC = 1e9;
42+
#startTime?: [number, number];
43+
#elapsed?: [number, number];
44+
45+
start() {
46+
if (this.#startTime) {
47+
throw new Error(`Start?? We're started, we can't start again!`);
48+
}
49+
50+
this.#startTime = process.hrtime();
51+
}
52+
stop() {
53+
if (this.#startTime == null) {
54+
throw new Error(`Stop?? We haven't even started yet!`);
55+
}
56+
57+
this.#elapsed = process.hrtime(this.#startTime);
58+
}
59+
60+
describe() {
61+
if (this.#elapsed == null) {
62+
return `Benchmark is still running`;
63+
}
64+
65+
return `Benchmark took ${
66+
this.#elapsed[0] * Benchmark.NS_PER_SEC + this.#elapsed[1]
67+
} nanoseconds`;
68+
}
69+
}
70+
71+
describe('micro-benchmark', () => {
72+
test('parsing KQL expression', () => {
73+
const b = new Benchmark();
74+
b.start();
75+
esKuery.fromKueryExpression(
76+
'not fleet-agent-actions.attributes.sent_at: * and fleet-agent-actions.attributes.agent_id:1234567'
77+
);
78+
b.stop();
79+
console.log(b.describe());
80+
});
81+
82+
test('manually building KueryNode', () => {
83+
const b = new Benchmark();
84+
b.start();
85+
esKuery.nodeTypes.function.buildNode('and', [
86+
esKuery.nodeTypes.function.buildNode(
87+
'not',
88+
esKuery.nodeTypes.function.buildNode('is', 'fleet-agent-actions.attributes.sent_at', '*')
89+
),
90+
esKuery.nodeTypes.function.buildNode(
91+
'is',
92+
'fleet-agent-actions.attributes.agent_id',
93+
'1234567'
94+
),
95+
]);
96+
b.stop();
97+
console.log(b.describe());
98+
});
99+
100+
test('doing nothing', () => {
101+
const b = new Benchmark();
102+
b.start();
103+
b.stop();
104+
console.log(b.describe());
105+
});
106+
});

x-pack/plugins/ingest_manager/server/services/agents/actions.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Agent, AgentAction, AgentActionSOAttributes } from '../../../common/typ
99
import { AGENT_ACTION_SAVED_OBJECT_TYPE } from '../../../common/constants';
1010
import { savedObjectToAgentAction } from './saved_objects';
1111
import { appContextService } from '../app_context';
12-
import { nodeTypes } from '../../../../../../src/plugins/data/common';
12+
import { esKuery } from '../../../../../../src/plugins/data/server';
1313

1414
export async function createAgentAction(
1515
soClient: SavedObjectsClientContract,
@@ -30,16 +30,16 @@ export async function getAgentActionsForCheckin(
3030
soClient: SavedObjectsClientContract,
3131
agentId: string
3232
): Promise<AgentAction[]> {
33-
const filter = nodeTypes.function.buildNode('and', [
34-
nodeTypes.function.buildNode(
33+
const filter = esKuery.nodeTypes.function.buildNode('and', [
34+
esKuery.nodeTypes.function.buildNode(
3535
'not',
36-
nodeTypes.function.buildNode(
36+
esKuery.nodeTypes.function.buildNode(
3737
'is',
3838
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`,
3939
'*'
4040
)
4141
),
42-
nodeTypes.function.buildNode(
42+
esKuery.nodeTypes.function.buildNode(
4343
'is',
4444
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.agent_id`,
4545
agentId
@@ -94,16 +94,16 @@ export async function getAgentActionByIds(
9494
}
9595

9696
export async function getNewActionsSince(soClient: SavedObjectsClientContract, timestamp: string) {
97-
const filter = nodeTypes.function.buildNode('and', [
98-
nodeTypes.function.buildNode(
97+
const filter = esKuery.nodeTypes.function.buildNode('and', [
98+
esKuery.nodeTypes.function.buildNode(
9999
'not',
100-
nodeTypes.function.buildNode(
100+
esKuery.nodeTypes.function.buildNode(
101101
'is',
102102
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`,
103103
'*'
104104
)
105105
),
106-
nodeTypes.function.buildNode(
106+
esKuery.nodeTypes.function.buildNode(
107107
'range',
108108
`${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.created_at`,
109109
{

0 commit comments

Comments
 (0)