Skip to content

Commit 2eb46cf

Browse files
perf(mcp): report search phase timings
Signed-off-by: Ertan <ertan.kucukoglu@gmail.com>
1 parent 076c8ae commit 2eb46cf

2 files changed

Lines changed: 90 additions & 15 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,9 @@ static const tool_def_t TOOLS[] = {
597597
"signatures+metadata (default). full: with source. files: just file list.\"},"
598598
"\"context\":{\"type\":\"integer\",\"description\":\"Lines of context around each match "
599599
"(like grep -C). Only used in compact mode.\"},"
600-
"\"regex\":{\"type\":\"boolean\",\"default\":false},\"limit\":{\"type\":\"integer\","
600+
"\"regex\":{\"type\":\"boolean\",\"default\":false},\"debug\":{\"type\":\"boolean\","
601+
"\"default\":false,\"description\":\"Include scope_ms, scan_ms, and enrich_ms phase timing "
602+
"diagnostics.\"},\"limit\":{\"type\":\"integer\","
601603
"\"description\":\"Max enriched results per call. Default 10. Response includes "
602604
"'total_grep_matches' and 'total_results' so callers can detect truncation. No "
603605
"offset parameter — raise limit or narrow with file_pattern / path_filter to see more."
@@ -8722,6 +8724,14 @@ typedef struct {
87228724
int match_count;
87238725
} search_result_t;
87248726

8727+
typedef struct {
8728+
uint64_t scope_ms;
8729+
uint64_t scan_ms;
8730+
uint64_t enrich_ms;
8731+
uint64_t elapsed_ms;
8732+
bool include_phase_timings;
8733+
} search_metrics_t;
8734+
87258735
/* Score a result for ranking: project source first, vendored last, tests lowest */
87268736
enum { SCORE_FUNC = 10, SCORE_ROUTE = 15, SCORE_VENDORED = -50, SCORE_TEST = -5 };
87278737
enum { MAX_LINE_SPAN = 999999 };
@@ -8980,7 +8990,7 @@ static yyjson_mut_val *build_dir_distribution(yyjson_mut_doc *doc, search_result
89808990
* distribution table, and the summary scalars. */
89818991
static char *assemble_search_output_toon(search_result_t *sr, int sr_count, grep_match_t *raw,
89828992
int raw_count, int gm_count, int limit,
8983-
bool warn_literal_pipe, uint64_t elapsed_ms) {
8993+
bool warn_literal_pipe, const search_metrics_t *metrics) {
89848994
enum { MAX_RAW = 20, SEARCH_SLOW_MS = 5000 };
89858995
cbm_sb_t sb;
89868996
cbm_sb_init(&sb);
@@ -9050,14 +9060,19 @@ static char *assemble_search_output_toon(search_result_t *sr, int sr_count, grep
90509060
cbm_tree_scalar_int(&sb, "total_grep_matches", gm_count);
90519061
cbm_tree_scalar_int(&sb, "total_results", sr_count);
90529062
cbm_tree_scalar_int(&sb, "raw_match_count", raw_count);
9053-
cbm_tree_scalar_int(&sb, "elapsed_ms", (long long)elapsed_ms);
9063+
if (metrics->include_phase_timings) {
9064+
cbm_tree_scalar_int(&sb, "scope_ms", (long long)metrics->scope_ms);
9065+
cbm_tree_scalar_int(&sb, "scan_ms", (long long)metrics->scan_ms);
9066+
cbm_tree_scalar_int(&sb, "enrich_ms", (long long)metrics->enrich_ms);
9067+
}
9068+
cbm_tree_scalar_int(&sb, "elapsed_ms", (long long)metrics->elapsed_ms);
90549069
if (warn_literal_pipe) {
90559070
cbm_tree_scalar_str(&sb, "warning",
90569071
"pattern contains '|' but regex=false, so it is matched literally "
90579072
"(not as alternation). Pass regex=true for 'foo|bar' to mean "
90589073
"'foo OR bar'.");
90599074
}
9060-
if (elapsed_ms >= SEARCH_SLOW_MS) {
9075+
if (metrics->elapsed_ms >= SEARCH_SLOW_MS) {
90619076
cbm_tree_scalar_str(&sb, "warning_slow",
90629077
"search was slow; narrow file_pattern/path_filter or use a more "
90639078
"specific pattern");
@@ -9069,7 +9084,7 @@ static char *assemble_search_output_toon(search_result_t *sr, int sr_count, grep
90699084
static char *assemble_search_output(search_result_t *sr, int sr_count, grep_match_t *raw,
90709085
int raw_count, int gm_count, int limit, int mode,
90719086
int context_lines, const char *root_path,
9072-
bool warn_literal_pipe, uint64_t elapsed_ms) {
9087+
bool warn_literal_pipe, const search_metrics_t *metrics) {
90739088
enum { MODE_COMPACT = 0, MODE_FULL = 1, MODE_FILES = 2, SEARCH_SLOW_MS = 5000 };
90749089

90759090
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
@@ -9153,7 +9168,12 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matc
91539168
yyjson_mut_obj_add_int(doc, root_obj, "total_grep_matches", gm_count);
91549169
yyjson_mut_obj_add_int(doc, root_obj, "total_results", sr_count);
91559170
yyjson_mut_obj_add_int(doc, root_obj, "raw_match_count", raw_count);
9156-
yyjson_mut_obj_add_int(doc, root_obj, "elapsed_ms", (int)elapsed_ms);
9171+
if (metrics->include_phase_timings) {
9172+
yyjson_mut_obj_add_uint(doc, root_obj, "scope_ms", metrics->scope_ms);
9173+
yyjson_mut_obj_add_uint(doc, root_obj, "scan_ms", metrics->scan_ms);
9174+
yyjson_mut_obj_add_uint(doc, root_obj, "enrich_ms", metrics->enrich_ms);
9175+
}
9176+
yyjson_mut_obj_add_uint(doc, root_obj, "elapsed_ms", metrics->elapsed_ms);
91579177
if (sr_count > 0 && gm_count > 0) {
91589178
char ratio[CBM_SZ_32];
91599179
snprintf(ratio, sizeof(ratio), "%.1fx", (double)gm_count / (double)(sr_count + raw_count));
@@ -9168,15 +9188,15 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matc
91689188
"pattern contains '|' but regex=false, so it is matched literally (not as "
91699189
"alternation). Pass regex=true for 'foo|bar' to mean 'foo OR bar'.");
91709190
}
9171-
if (elapsed_ms >= SEARCH_SLOW_MS) {
9191+
if (metrics->elapsed_ms >= SEARCH_SLOW_MS) {
91729192
char slow[CBM_SZ_128];
91739193
snprintf(slow, sizeof(slow),
91749194
"search took %dms (>%ds); narrow file_pattern/path_filter or use a more "
91759195
"specific pattern",
9176-
(int)elapsed_ms, SEARCH_SLOW_MS / 1000);
9196+
(int)metrics->elapsed_ms, SEARCH_SLOW_MS / 1000);
91779197
yyjson_mut_arr_add_strcpy(doc, warnings, slow);
91789198
char ems[CBM_SZ_32];
9179-
snprintf(ems, sizeof(ems), "%d", (int)elapsed_ms);
9199+
snprintf(ems, sizeof(ems), "%d", (int)metrics->elapsed_ms);
91809200
cbm_log_warn("search.slow", "elapsed_ms", ems); /* visibility in logs */
91819201
}
91829202
if (yyjson_mut_arr_size(warnings) > 0) {
@@ -9652,6 +9672,8 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
96529672
int context_lines = cbm_mcp_get_int_arg(args, "context", 0);
96539673
bool use_regex = cbm_mcp_get_bool_arg(args, "regex");
96549674
uint64_t search_t0 = cbm_now_ms();
9675+
search_metrics_t metrics = {0};
9676+
metrics.include_phase_timings = cbm_mcp_get_bool_arg(args, "debug");
96559677
/* In literal (non-regex) mode a '|' is matched as a byte, not alternation —
96569678
* a common silent 0-match trap; flagged in the result warnings (#282). */
96579679
bool pat_has_pipe = pattern && strchr(pattern, '|') != NULL;
@@ -9787,17 +9809,22 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
97879809
bool scoped = false;
97889810
int scoped_written = 0;
97899811

9812+
uint64_t scope_t0 = metrics.include_phase_timings ? cbm_now_ms() : 0;
97909813
scoped = write_scoped_filelist(srv, project, root_path, scratch.filelist, has_path_filter,
97919814
has_path_filter ? &path_regex : NULL, &scoped_written);
97929815
/* Close before grep runs: this is what flushes the records the helper wrote
97939816
* through the descriptor. Clearing the field hands ownership to
97949817
* search_scratch_close, which still unlinks the file itself. */
97959818
(void)fclose(scratch.filelist);
97969819
scratch.filelist = NULL;
9820+
if (metrics.include_phase_timings) {
9821+
metrics.scope_ms = cbm_now_ms() - scope_t0;
9822+
}
97979823

97989824
/* Collect grep matches into array */
97999825
int gm_count = 0;
98009826
grep_match_t *gm = NULL;
9827+
uint64_t scan_t0 = metrics.include_phase_timings ? cbm_now_ms() : 0;
98019828
if (scoped && scoped_written == 0) {
98029829
/* The path_filter excluded every indexed file — nothing to scan.
98039830
* Skip the grep subprocess: xargs on an empty filelist is
@@ -9827,11 +9854,15 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
98279854
* code, the file list is removed even when the scan was not scoped. */
98289855
search_scratch_close(&scratch);
98299856
}
9857+
if (metrics.include_phase_timings) {
9858+
metrics.scan_ms = cbm_now_ms() - scan_t0;
9859+
}
98309860

98319861
/* ── Phase 2+3: Block expansion + graph ranking ──────────── */
98329862
/* Sort grep matches by file for contiguous processing.
98339863
* Then: one SQL query per unique file for nodes, one batch query for all degrees. */
98349864

9865+
uint64_t enrich_t0 = metrics.include_phase_timings ? cbm_now_ms() : 0;
98359866
cbm_store_t *store = resolve_store(srv, project);
98369867

98379868
int sr_cap = CBM_SZ_32;
@@ -9875,6 +9906,10 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
98759906
if (sr_count > SKIP_ONE) {
98769907
qsort(sr, sr_count, sizeof(search_result_t), search_result_cmp);
98779908
}
9909+
if (metrics.include_phase_timings) {
9910+
metrics.enrich_ms = cbm_now_ms() - enrich_t0;
9911+
}
9912+
metrics.elapsed_ms = cbm_now_ms() - search_t0;
98789913

98799914
/* ── Phase 4: Context assembly (extracted helper) ─────────── */
98809915

@@ -9887,15 +9922,14 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
98879922

98889923
char *result = NULL;
98899924
if (mode == 0 && !sc_legacy_json) {
9890-
char *toon_text =
9891-
assemble_search_output_toon(sr, sr_count, raw, raw_count, gm_count, limit,
9892-
pat_has_pipe && !use_regex, cbm_now_ms() - search_t0);
9925+
char *toon_text = assemble_search_output_toon(sr, sr_count, raw, raw_count, gm_count, limit,
9926+
pat_has_pipe && !use_regex, &metrics);
98939927
result = cbm_mcp_text_result(toon_text ? toon_text : "out of memory", toon_text == NULL);
98949928
free(toon_text);
98959929
} else {
9896-
result = assemble_search_output(sr, sr_count, raw, raw_count, gm_count, limit, mode,
9897-
context_lines, root_path, pat_has_pipe && !use_regex,
9898-
cbm_now_ms() - search_t0);
9930+
result =
9931+
assemble_search_output(sr, sr_count, raw, raw_count, gm_count, limit, mode,
9932+
context_lines, root_path, pat_has_pipe && !use_regex, &metrics);
98999933
}
99009934
free(gm);
99019935
free(sr);

‎tests/test_mcp.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,6 +4557,46 @@ TEST(search_code_literal_pipe_warns_issue282) {
45574557
PASS();
45584558
}
45594559

4560+
TEST(search_code_reports_phase_timings_only_in_debug_mode) {
4561+
char tmp[512];
4562+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
4563+
ASSERT_NOT_NULL(srv);
4564+
4565+
char *response = cbm_mcp_handle_tool(
4566+
srv, "search_code", "{\"pattern\":\"HandleRequest\",\"project\":\"test-project\"}");
4567+
ASSERT_NOT_NULL(response);
4568+
ASSERT_NULL(strstr(response, "scope_ms"));
4569+
ASSERT_NULL(strstr(response, "scan_ms"));
4570+
ASSERT_NULL(strstr(response, "enrich_ms"));
4571+
ASSERT_NOT_NULL(strstr(response, "elapsed_ms"));
4572+
free(response);
4573+
4574+
response = cbm_mcp_handle_tool(
4575+
srv, "search_code",
4576+
"{\"pattern\":\"HandleRequest\",\"project\":\"test-project\",\"debug\":true}");
4577+
ASSERT_NOT_NULL(response);
4578+
ASSERT_NOT_NULL(strstr(response, "scope_ms"));
4579+
ASSERT_NOT_NULL(strstr(response, "scan_ms"));
4580+
ASSERT_NOT_NULL(strstr(response, "enrich_ms"));
4581+
ASSERT_NOT_NULL(strstr(response, "elapsed_ms"));
4582+
free(response);
4583+
4584+
response = cbm_mcp_handle_tool(
4585+
srv, "search_code",
4586+
"{\"pattern\":\"HandleRequest\",\"project\":\"test-project\",\"format\":\"json\","
4587+
"\"debug\":true}");
4588+
ASSERT_NOT_NULL(response);
4589+
ASSERT_NOT_NULL(strstr(response, "\\\"scope_ms\\\":"));
4590+
ASSERT_NOT_NULL(strstr(response, "\\\"scan_ms\\\":"));
4591+
ASSERT_NOT_NULL(strstr(response, "\\\"enrich_ms\\\":"));
4592+
ASSERT_NOT_NULL(strstr(response, "\\\"elapsed_ms\\\":"));
4593+
free(response);
4594+
4595+
cleanup_snippet_dir(tmp);
4596+
cbm_mcp_server_free(srv);
4597+
PASS();
4598+
}
4599+
45604600
/* issue #272: '&' in a path / file_pattern is neutralised by the command's
45614601
* quoting and must no longer be rejected as "invalid characters". */
45624602
TEST(search_code_ampersand_accepted_issue272) {
@@ -10724,6 +10764,7 @@ SUITE(mcp) {
1072410764
RUN_TEST(search_code_path_filter_matches_nothing);
1072510765
RUN_TEST(search_code_invalid_regex_errors_issue283);
1072610766
RUN_TEST(search_code_literal_pipe_warns_issue282);
10767+
RUN_TEST(search_code_reports_phase_timings_only_in_debug_mode);
1072710768
RUN_TEST(search_code_ampersand_accepted_issue272);
1072810769
RUN_TEST(tool_detect_changes_no_project);
1072910770
RUN_TEST(tool_manage_adr_no_project);

0 commit comments

Comments
 (0)