Files
cproof/tests/bench/bench_stubs.c
jabber.developer2 b1d820462a feat(bench): synthetic load harness for flat-file backend (P1-P5)
End-to-end performance and correctness harness for the flat-file +
SQLite database backends. Lives in tests/bench/, built only on
demand (`make bench`); not part of `make check`.

Components

  gen_history (P1)
    Deterministic corpus generator. Knobs: lines, contacts, years,
    seed, stanza-id mode (uuid/libpurple/conversations/mixed), LMC
    rate, MAM-OOO rate, resources/contact, length profile
    (short/mixed/long/extreme). Emits the canonical
    flatlog/<account>/<contact>/history.log layout used by
    ff_verify_integrity. ~340 LOC.

  bench_runner (P1, P2.5)
    S1 cold tail-access via sparse index
    S2 warm tail-access (page cache hot)
    S3 deep pagination (1000 binary-search lookups)
    S4 first-time index build (cold file -> ff_state_ensure_fresh)
    S5 incremental extend (asserts no full rebuild path)
    S6 real ff_verify_integrity over the contact tree
    Reports total/err/warn/info issue counts in the CSV note.

  bench_long_messages (P2)
    L1-L14 long-message stress: 1KB up to 9.9MB bodies, plus
    oversized line rejection (10MB+1 -> ff_readline returns ""),
    embedded-newline / pipe / emoji body patterns, full parse on
    100x1MB, 1000x100KB sustained append.

  bench_failure_modes (P3)
    F1-F15 failure-injection: truncated last line, mid-file CRLF,
    mid-file BOM, LMC cycle, LMC depth>FF_MAX_LMC_DEPTH, manual
    ': ' in resource, RTL/ZWSP, Latin-1 byte, empty body,
    mtime/inode flip, empty file. Each test asserts expected issue
    levels and reports PASS/FAIL.

  bench_export_import (P5)
    Links real database_export.c + database_sqlite.c + database.c
    and drives log_database_export_to_flatfile /
    log_database_import_from_flatfile under load.
    Subcommands: seed, export, import, roundtrip, verify.
    S7a/b export, S8a/b import, S8e roundtrip with full byte-by-byte
    content diff of every row in (from_jid, to_jid, message,
    timestamp, type, stanza_id, archive_id, encryption, replace_id).

Make targets

  bench-quick / bench / bench-full
  bench-longmsg, bench-failure
  bench-multicontact, bench-lmc, bench-ooo
  bench-export, bench-import, bench-roundtrip
  bench-pipeline, bench-pipeline-max (1M rows)
  bench-compare, bench-update-baseline

Volume controls: BENCH_VOLUME (small/medium/max), BENCH_PIPE_ROWS,
BENCH_PIPE_ROWS_MAX, BENCH_DATA_DIR, BENCH_CSV.

Baseline + regression checking (P4)

  tests/bench/baseline.csv       51 rows: S1-S6 x {small,lmc,ooo}
                                 + L1-L14 + F1-F15 (11 of 15) +
                                 S7/S8 x {pipe100k, pipe1M}.
  compare_baseline.py            median over duplicate rows;
                                 exits 1 on any (scenario, volume)
                                 slowdown >= threshold (default 25%).

Verified at scale

  bench-pipeline-max: 1,000,000 rows, full content diff
    seed   17 s, export 304 s, import 31 s, idempotent re-import 10 s,
    diff 2.7 s -- mismatches=0.

Findings surfaced by the harness

  Export scales super-linearly: 4 s @ 100k -> 304 s @ 1M (76x for
    10x rows). Cause: g_slist_sort on the merged list + per-row
    ProfMessage/ff_parsed_line_t allocations. RSS peaks at 1.4 GB
    on 1M. Worth a follow-up.
  Export progress reporting only fires during the write phase --
    the merge+sort phase (~95% of wall time at 1M) is silent.
  /history export and /history import are blocking on the main
    UI thread; profanity is frozen for the duration (~5 min @ 1M).
  ff_readline sets *truncated=TRUE on partial-write tail but
    ff_verify_integrity does not surface this -- partial writes
    go unflagged (failure-injection F1).
  Parser silently truncates body at first unescaped ': ' if a
    resource was manually edited to contain it (F9).
  In gen_history (caught by the bench's own real-verify pass):
    g_strndup mid-codepoint truncation on UTF-8 bank strings -- fixed.

Linkage strategy

  database_flatfile.c + parser + verify + common.c are linked
  unconditionally. The export/import bench additionally links
  database.c + database_sqlite.c + database_export.c. bench_stubs.c
  provides minimal stubs for log_*, prefs_*, connection_get_jid,
  jid_create, files_*, message_*, ui hooks. integrity_issue_free
  is a weak symbol so it falls back to the real database.c
  implementation when that file is linked.
2026-04-30 15:39:30 +03:00

340 lines
8.2 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of CProof.
// See LICENSE for the full GPLv3 text and the special OpenSSL linking exception.
/*
* bench_stubs.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Minimal stubs so we can link the flat-file backend (database_flatfile*.c)
* into the bench harness without pulling in the rest of profanity (xmpp,
* ui, prefs, connection state, ...).
*
* Only ff_* helpers and ff_state_* / ff_verify_integrity are exercised
* by the harness, so most code paths inside database_flatfile.c that
* reference these symbols are never reached. The stubs exist purely to
* resolve the linker.
*
* log_* writes to stderr when BENCH_LOG=1 in the environment, otherwise
* silent — keeps the harness output clean while still letting us debug.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glib.h>
#include "log.h"
#include "common.h"
#include "config/files.h"
#include "config/preferences.h"
#include "database.h"
#include "database_flatfile.h" // for ff_jid_to_dir() in stub
#include "xmpp/xmpp.h"
#include "xmpp/jid.h"
#include "xmpp/message.h"
#include "ui/ui.h"
// ---------------------------------------------------------------------------
// log_* (real impl, gated by BENCH_LOG=1)
static int
_bench_log_enabled(void)
{
static int cached = -1;
if (cached == -1) {
const char* env = getenv("BENCH_LOG");
cached = (env && env[0] && env[0] != '0') ? 1 : 0;
}
return cached;
}
static void
_bench_log(const char* level, const char* fmt, va_list ap)
{
if (!_bench_log_enabled())
return;
fprintf(stderr, "[%s] ", level);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
}
void
log_debug(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("DEBUG", msg, ap);
va_end(ap);
}
void
log_info(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("INFO", msg, ap);
va_end(ap);
}
void
log_warning(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("WARN", msg, ap);
va_end(ap);
}
void
log_error(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("ERROR", msg, ap);
va_end(ap);
}
void log_init(log_level_t filter, const char* const log_file) { (void)filter; (void)log_file; }
void log_close(void) {}
void log_msg(log_level_t level, const char* const area, const char* const msg) { (void)level; (void)area; (void)msg; }
const char* get_log_file_location(void) { return ""; }
log_level_t log_get_filter(void) { return PROF_LEVEL_INFO; }
int log_level_from_string(char* log_level, log_level_t* level) { (void)log_level; if (level) *level = PROF_LEVEL_INFO; return 0; }
void log_stderr_init(log_level_t level) { (void)level; }
void log_stderr_handler(void) {}
// ---------------------------------------------------------------------------
// prefs / files / xmpp / ui — never reached by the harness, but database_flatfile.c
// references them, so we resolve the symbols.
gchar*
prefs_get_string(preference_t pref)
{
(void)pref;
return NULL;
}
gboolean
prefs_get_boolean(preference_t pref)
{
(void)pref;
return FALSE;
}
void
prefs_set_string(preference_t pref, const gchar* new_value)
{
(void)pref;
(void)new_value;
}
void
prefs_set_boolean(preference_t pref, gboolean value)
{
(void)pref;
(void)value;
}
gchar*
files_get_data_path(const char* const location)
{
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench";
if (location && location[0])
return g_strdup_printf("%s/%s", base, location);
return g_strdup(base);
}
gchar*
files_get_config_path(const char* const location)
{
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench";
if (location && location[0])
return g_strdup_printf("%s/%s", base, location);
return g_strdup(base);
}
// $BENCH_DATA_DIR/$location/$jid_dir/$filename — mirrors the canonical
// per-account layout. Caller g_free's. Required by _get_db_filename in
// database_sqlite.c during _sqlite_init.
char*
files_file_in_account_data_path(const char* const location, const char* const account_jid,
const char* const filename)
{
if (!account_jid || !filename)
return NULL;
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench";
auto_gchar gchar* jid_dir = ff_jid_to_dir(account_jid);
auto_gchar gchar* parent = g_strdup_printf("%s/%s/%s", base,
location && location[0] ? location : "",
jid_dir);
g_mkdir_with_parents(parent, 0755);
return g_strdup_printf("%s/%s", parent, filename);
}
// jid_destroy is the public name; bench doesn't define a jid_destroy stub
// because src/xmpp/jid.c isn't linked in. We emulate just enough for cleanup.
static void
_bench_jid_free(Jid* j)
{
if (!j)
return;
g_free(j->barejid);
g_free(j->fulljid);
g_free(j->resourcepart);
g_free(j);
}
Jid*
jid_create(const gchar* const fulljid)
{
if (!fulljid)
return NULL;
Jid* j = g_malloc0(sizeof(Jid));
j->fulljid = g_strdup(fulljid);
const char* slash = strchr(fulljid, '/');
if (slash) {
j->barejid = g_strndup(fulljid, slash - fulljid);
j->resourcepart = g_strdup(slash + 1);
} else {
j->barejid = g_strdup(fulljid);
}
return j;
}
void
jid_destroy(Jid* jid)
{
_bench_jid_free(jid);
}
// Weak so that bench targets which also link real database.c (which defines
// the same symbol) take the strong version. bench_runner / bench_long_messages /
// bench_failure_modes don't link database.c and rely on this stub.
__attribute__((weak)) void
integrity_issue_free(integrity_issue_t* issue)
{
if (!issue)
return;
g_free(issue->file);
g_free(issue->message);
g_free(issue);
}
void
message_free(ProfMessage* m)
{
if (!m)
return;
_bench_jid_free(m->from_jid);
_bench_jid_free(m->to_jid);
g_free(m->id);
g_free(m->originid);
g_free(m->replace_id);
g_free(m->stanzaid);
g_free(m->body);
g_free(m->encrypted);
g_free(m->plain);
if (m->timestamp)
g_date_time_unref(m->timestamp);
g_free(m);
}
// connection_get_jid: returns a process-wide stub Jid built from
// $BENCH_ACCOUNT_JID (default "bench@bench.example"). Allocated lazily on
// first call, freed at exit via atexit().
static Jid* g_bench_jid;
static void
_bench_jid_atexit(void)
{
if (!g_bench_jid)
return;
g_free(g_bench_jid->barejid);
g_free(g_bench_jid->fulljid);
g_free(g_bench_jid->resourcepart);
g_free(g_bench_jid);
g_bench_jid = NULL;
}
const Jid*
connection_get_jid(void)
{
if (g_bench_jid)
return g_bench_jid;
const char* env = getenv("BENCH_ACCOUNT_JID");
if (!env || !env[0])
env = "bench@bench.example";
g_bench_jid = g_malloc0(sizeof(Jid));
g_bench_jid->barejid = g_strdup(env);
g_bench_jid->fulljid = g_strdup(env);
g_bench_jid->resourcepart = NULL;
atexit(_bench_jid_atexit);
return g_bench_jid;
}
ProfMessage*
message_init(void)
{
return g_malloc0(sizeof(ProfMessage));
}
Jid*
jid_create_from_bare_and_resource(const char* const barejid, const char* const resource)
{
if (!barejid)
return NULL;
Jid* j = g_malloc0(sizeof(Jid));
j->barejid = g_strdup(barejid);
j->resourcepart = resource ? g_strdup(resource) : NULL;
j->fulljid = resource ? g_strdup_printf("%s/%s", barejid, resource) : g_strdup(barejid);
return j;
}
void
cons_show(const char* const msg, ...)
{
(void)msg;
}
void
cons_show_error(const char* const cmd, ...)
{
(void)cmd;
}
// session/account stubs — used by database.c when wiring up backend switch.
// Bench never goes through the dispatcher's switch path; just satisfy linker.
const char*
session_get_account_name(void)
{
return NULL;
}
ProfAccount*
accounts_get_account(const char* const name)
{
(void)name;
return NULL;
}
void
account_free(ProfAccount* account)
{
if (!account)
return;
g_free(account->jid);
g_free(account);
}