Skip to content

Commit ec94af7

Browse files
author
alexandre-perrin
committed
wip: add profiling funcs
1 parent d9b307e commit ec94af7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

test/mock_objects/agent_test.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,3 +1464,23 @@ agent_ensure_deployment_config(struct agent_deployment *d, const char *payload,
14641464

14651465
agent_ensure_deployment_status(deploymentId, "ok");
14661466
}
1467+
1468+
struct profile
1469+
agent_profile_start(char *id)
1470+
{
1471+
struct profile p = {.id = id};
1472+
clock_gettime(CLOCK_MONOTONIC, &p.start);
1473+
// message_info("profile %s: started", id);
1474+
return p;
1475+
}
1476+
1477+
void
1478+
agent_profile_print(struct profile *p)
1479+
{
1480+
struct timespec now, diff;
1481+
clock_gettime(CLOCK_MONOTONIC, &now);
1482+
timespecsub(&now, &p->start, &diff);
1483+
int ms = (timespec2ns(&diff) + 999999) / 1000000;
1484+
1485+
message_info("profile %s: %d ms", p->id, ms);
1486+
}

test/mock_objects/agent_test.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
#include <setjmp.h>
1111
#include <stdarg.h>
12+
#include <stdbool.h>
1213
#include <stddef.h>
1314
#include <stdint.h>
1415
#include <stdio.h>
16+
#include <time.h>
1517

1618
#include <cmocka.h>
1719
#include <evp/agent.h>
@@ -324,4 +326,26 @@ void agent_ensure_deployment_config(struct agent_deployment *d,
324326
const char *deploymentId,
325327
const char *instance_config);
326328

329+
#define CAT_(a, b) a##b
330+
#define CAT(a, b) CAT_(a, b)
331+
#define UID(Name) CAT(Name, __LINE__)
332+
#define STR_(s) #s
333+
#define STR(s) STR_(s)
334+
335+
struct profile {
336+
struct timespec start;
337+
char *id;
338+
bool exit;
339+
};
340+
341+
#define PROF() UID(prof)
342+
#define PROF_ID(Name) __FILE__ ":" STR(__LINE__) ":" Name
343+
344+
#define agent_profile_scope(Name) \
345+
for (struct profile PROF() = agent_profile_start(PROF_ID(Name)); \
346+
!PROF().exit; PROF().exit = true, agent_profile_print(&PROF()))
347+
348+
struct profile agent_profile_start(char *id);
349+
void agent_profile_print(struct profile *p);
350+
327351
#endif // AGENT_TEST_H

0 commit comments

Comments
 (0)