Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T02:56:38.411Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

bench_utils.rs

90 lines · 3.6 KB · rust
1use rand::Rng;
2
3pub const RUST_MODULE_HEADER_LINES: usize = 10;
4pub const RUST_FUNCTION_LINES: usize = 12;
5pub const RUST_FUNCTION_BODY_LINES: usize = 11;
6pub const RUST_MODULE_FOOTER_LINES: usize = 5;
7
8pub fn rust_file_line_count(function_count: usize) -> usize {
9    RUST_MODULE_HEADER_LINES + function_count * RUST_FUNCTION_LINES + RUST_MODULE_FOOTER_LINES
10}
11
12pub fn random_rust_file(rng: &mut impl Rng, line_count: usize) -> Vec<String> {
13    if line_count < RUST_MODULE_HEADER_LINES + RUST_MODULE_FOOTER_LINES {
14        return (0..line_count)
15            .map(|line_index| format!("// generated benchmark line {line_index}"))
16            .collect();
17    }
18
19    let mut lines = vec![
20        "use anyhow::{Context as _, Result};".to_string(),
21        "use collections::HashMap;".to_string(),
22        "".to_string(),
23        "#[derive(Clone, Debug)]".to_string(),
24        "pub struct WorkspaceSnapshot {".to_string(),
25        "    buffers: HashMap<String, usize>,".to_string(),
26        "    version: usize,".to_string(),
27        "}".to_string(),
28        "".to_string(),
29        "impl WorkspaceSnapshot {".to_string(),
30    ];
31
32    let body_line_count = line_count - RUST_MODULE_HEADER_LINES - RUST_MODULE_FOOTER_LINES;
33    let function_count = body_line_count / RUST_FUNCTION_LINES;
34    let filler_line_count = body_line_count % RUST_FUNCTION_LINES;
35
36    for function_index in 0..function_count {
37        let function_name = rust_identifier(rng, function_index);
38        let argument_name = rust_identifier(rng, function_index + 1_000);
39        let local_name = rust_identifier(rng, function_index + 2_000);
40        let branch_name = rust_identifier(rng, function_index + 3_000);
41        let multiplier = rng.random_range(2..17);
42        let offset = rng.random_range(1..128);
43
44        lines.extend([
45            format!(
46                "    pub fn {function_name}(&mut self, {argument_name}: usize) -> Result<usize> {{"
47            ),
48            format!("        let mut {local_name} = {argument_name}.saturating_mul({multiplier});"),
49            format!("        if {local_name} % 2 == 0 {{"),
50            format!(
51                "            {local_name} = {local_name}.saturating_add(self.version + {offset});"
52            ),
53            "        } else {".to_string(),
54            format!("            {local_name} = {local_name}.saturating_sub({offset});"),
55            "        }".to_string(),
56            format!("        let {branch_name} = self.buffers.len().saturating_add({local_name});"),
57            format!("        self.version = self.version.saturating_add({branch_name});"),
58            format!("        Ok({branch_name})"),
59            "    }".to_string(),
60            "".to_string(),
61        ]);
62    }
63
64    for filler_index in 0..filler_line_count {
65        let filler_name = rust_identifier(rng, function_count + 4_000 + filler_index);
66        lines.push(format!("    // benchmark filler {filler_name}"));
67    }
68
69    lines.push("}".to_string());
70    lines.push("".to_string());
71    lines.push("pub fn normalize_path(path: &str) -> String {".to_string());
72    lines.push("    path.replace('\\\\', \"/\")".to_string());
73    lines.push("}".to_string());
74
75    debug_assert_eq!(lines.len(), line_count);
76    lines
77}
78
79pub fn rust_identifier(rng: &mut impl Rng, salt: usize) -> String {
80    const PARTS: &[&str] = &[
81        "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "theta", "lambda", "sigma", "omega",
82    ];
83    format!(
84        "{}_{}_{}",
85        PARTS[rng.random_range(0..PARTS.len())],
86        salt,
87        rng.random_range(0..10_000)
88    )
89}
90
Served at tenant.openagents/omega Member data and write actions are omitted.