sp1-eval 5.0.0

A performance evaluation tool for SP1 programs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use anyhow::Result;
use clap::{command, Parser};
use reqwest::Client;
use serde::Serialize;
use serde_json::json;
use slack_rust::{
    chat::post_message::{post_message, PostMessageRequest},
    http_client::default_client,
};
use sp1_prover::{components::SP1ProverComponents, utils::get_cycles, SP1Prover};
use sp1_sdk::{SP1Context, SP1Stdin};
use sp1_stark::SP1ProverOpts;
use std::time::{Duration, Instant};

use program::load_program;

use crate::program::{TesterProgram, PROGRAMS};

mod program;

#[derive(Parser, Clone)]
#[command(about = "Evaluate the performance of SP1 on programs.")]
struct EvalArgs {
    /// The programs to evaluate, specified by name. If not specified, all programs will be
    /// evaluated.
    #[arg(long, use_value_delimiter = true, value_delimiter = ',')]
    pub programs: Vec<String>,

    /// The shard size to use for the prover.
    #[arg(long)]
    pub shard_size: Option<usize>,

    /// Whether to post results to Slack.
    #[arg(long, default_missing_value="true", num_args=0..=1)]
    pub post_to_slack: Option<bool>,

    /// The Slack channel ID to post results to, only used if post_to_slack is true.
    #[arg(long)]
    pub slack_channel_id: Option<String>,

    /// The Slack bot token to post results to, only used if post_to_slack is true.
    #[arg(long)]
    pub slack_token: Option<String>,

    /// Whether to post results to GitHub PR.
    #[arg(long, default_missing_value="true", num_args=0..=1)]
    pub post_to_github: Option<bool>,

    /// The GitHub token for authentication, only used if post_to_github is true.
    #[arg(long)]
    pub github_token: Option<String>,

    /// The GitHub repository owner.
    #[arg(long)]
    pub repo_owner: Option<String>,

    /// The GitHub repository name.
    #[arg(long)]
    pub repo_name: Option<String>,

    /// The GitHub PR number.
    #[arg(long)]
    pub pr_number: Option<String>,

    /// The name of the branch.
    #[arg(long)]
    pub branch_name: Option<String>,

    /// The commit hash.
    #[arg(long)]
    pub commit_hash: Option<String>,

    /// The author of the commit.
    #[arg(long)]
    pub author: Option<String>,
}

pub async fn evaluate_performance<C: SP1ProverComponents>(
    opts: SP1ProverOpts,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("opts: {:?}", opts);

    let args = EvalArgs::parse();

    // Set environment variables to configure the prover.
    if let Some(shard_size) = args.shard_size {
        std::env::set_var("SHARD_SIZE", format!("{}", 1 << shard_size));
    }

    // Choose which programs to evaluate.
    let programs: Vec<&TesterProgram> = if args.programs.is_empty() {
        PROGRAMS.iter().collect()
    } else {
        PROGRAMS
            .iter()
            .filter(|p| args.programs.iter().any(|arg| arg.eq_ignore_ascii_case(p.name)))
            .collect()
    };

    sp1_sdk::utils::setup_logger();

    // Run the evaluations on each program.
    let mut reports = Vec::new();
    for program in &programs {
        println!("Evaluating program: {}", program.name);
        let (elf, stdin) = load_program(program.elf, program.input);
        let report = run_evaluation::<C>(program.name, &elf, &stdin, opts);
        reports.push(report);
        println!("Finished Program: {}", program.name);
    }

    // Prepare and format the results.
    let reports_len = reports.len();
    let success_count = reports.iter().filter(|r| r.success).count();
    let results_text = format_results(&args, &reports);

    // Print results
    println!("{}", results_text.join("\n"));

    // Post to Slack if applicable
    if args.post_to_slack.unwrap_or(false) {
        match (&args.slack_token, &args.slack_channel_id) {
            (Some(token), Some(channel)) => {
                for message in &results_text {
                    post_to_slack(token, channel, message).await?;
                }
            }
            _ => println!("Warning: post_to_slack is true, required Slack arguments are missing."),
        }
    }

    // Post to GitHub PR if applicable
    if args.post_to_github.unwrap_or(false) {
        match (&args.repo_owner, &args.repo_name, &args.pr_number, &args.github_token) {
            (Some(owner), Some(repo), Some(pr_number), Some(token)) => {
                let message = format_github_message(&results_text);
                post_to_github_pr(owner, repo, pr_number, token, &message).await?;
            }
            _ => {
                println!("Warning: post_to_github is true, required GitHub arguments are missing.")
            }
        }
    }

    // Exit with an error if any programs failed.
    let all_successful = success_count == reports_len;
    if !all_successful {
        println!("Some programs failed. Please check the results above.");
        std::process::exit(1);
    }

    Ok(())
}

#[derive(Debug, Serialize)]
pub struct PerformanceReport {
    program: String,
    cycles: u64,
    exec_khz: f64,
    core_khz: f64,
    compressed_khz: f64,
    time: f64,
    success: bool,
}

fn run_evaluation<C: SP1ProverComponents>(
    program_name: &str,
    elf: &[u8],
    stdin: &SP1Stdin,
    opts: SP1ProverOpts,
) -> PerformanceReport {
    let cycles = get_cycles(elf, stdin);

    let prover = SP1Prover::<C>::new();
    let (_, pk_d, program, vk) = prover.setup(elf);

    let context = SP1Context::default();

    let (_, exec_duration) = time_operation(|| prover.execute(elf, stdin, context.clone()));

    let (core_proof, core_duration) =
        time_operation(|| prover.prove_core(&pk_d, program, stdin, opts, context).unwrap());

    let (_, compress_duration) =
        time_operation(|| prover.compress(&vk, core_proof, vec![], opts).unwrap());

    let total_duration = exec_duration + core_duration + compress_duration;

    PerformanceReport {
        program: program_name.to_string(),
        cycles,
        exec_khz: calculate_khz(cycles, exec_duration),
        core_khz: calculate_khz(cycles, core_duration),
        compressed_khz: calculate_khz(cycles, compress_duration + core_duration),
        time: total_duration.as_secs_f64(),
        success: true,
    }
}

fn format_results(args: &EvalArgs, results: &[PerformanceReport]) -> Vec<String> {
    let mut detail_text = String::new();
    if let Some(branch_name) = &args.branch_name {
        detail_text.push_str(&format!("*Branch*: {}\n", branch_name));
    }
    if let Some(commit_hash) = &args.commit_hash {
        detail_text.push_str(&format!("*Commit*: {}\n", &commit_hash[..8]));
    }
    if let Some(author) = &args.author {
        detail_text.push_str(&format!("*Author*: {}\n", author));
    }

    let mut table_text = String::new();
    table_text.push_str("```\n");
    table_text.push_str("| program           | cycles      | execute (mHz)  | core (kHZ)     | compress (KHz) | time   | success  |\n");
    table_text.push_str("|-------------------|-------------|----------------|----------------|----------------|--------|----------|");

    for result in results.iter() {
        table_text.push_str(&format!(
            "\n| {:<17} | {:>11} | {:>14.2} | {:>14.2} | {:>14.2} | {:>6} | {:<7} |",
            result.program,
            result.cycles,
            result.exec_khz / 1000.0,
            result.core_khz,
            result.compressed_khz,
            format_duration(result.time),
            if result.success { "" } else { "" }
        ));
    }
    table_text.push_str("\n```");

    vec!["*SP1 Performance Test Results*\n".to_string(), detail_text, table_text]
}

pub fn time_operation<T, F: FnOnce() -> T>(operation: F) -> (T, Duration) {
    let start = Instant::now();
    let result = operation();
    let duration = start.elapsed();
    (result, duration)
}

fn calculate_khz(cycles: u64, duration: Duration) -> f64 {
    let duration_secs = duration.as_secs_f64();
    if duration_secs > 0.0 {
        (cycles as f64 / duration_secs) / 1_000.0
    } else {
        0.0
    }
}

fn format_duration(duration: f64) -> String {
    let secs = duration.round() as u64;
    let minutes = secs / 60;
    let seconds = secs % 60;

    if minutes > 0 {
        format!("{}m{}s", minutes, seconds)
    } else if seconds > 0 {
        format!("{}s", seconds)
    } else {
        format!("{}ms", (duration * 1000.0).round() as u64)
    }
}

async fn post_to_slack(slack_token: &str, slack_channel_id: &str, message: &str) -> Result<()> {
    let slack_api_client = default_client();
    let request = PostMessageRequest {
        channel: slack_channel_id.to_string(),
        text: Some(message.to_string()),
        ..Default::default()
    };

    post_message(&slack_api_client, &request, slack_token).await.expect("slack api call error");

    Ok(())
}

fn format_github_message(results_text: &[String]) -> String {
    let mut formatted_message = String::new();

    if let Some(title) = results_text.first() {
        // Add an extra asterisk for GitHub bold formatting
        formatted_message.push_str(&title.replace('*', "**"));
        formatted_message.push('\n');
    }

    if let Some(details) = results_text.get(1) {
        // Add an extra asterisk for GitHub bold formatting
        formatted_message.push_str(&details.replace('*', "**"));
        formatted_message.push('\n');
    }

    if let Some(table) = results_text.get(2) {
        // Remove the triple backticks as GitHub doesn't require them for table formatting
        let cleaned_table = table.trim_start_matches("```").trim_end_matches("```");
        formatted_message.push_str(cleaned_table);
    }

    formatted_message
}

async fn post_to_github_pr(
    owner: &str,
    repo: &str,
    pr_number: &str,
    token: &str,
    message: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    let base_url = format!("https://api.github.com/repos/{}/{}", owner, repo);

    // Get all comments on the PR
    let comments_url = format!("{}/issues/{}/comments", base_url, pr_number);
    let comments_response = client
        .get(&comments_url)
        .header("Authorization", format!("token {}", token))
        .header("User-Agent", "sp1-perf-bot")
        .send()
        .await?;

    let comments: Vec<serde_json::Value> = comments_response.json().await?;

    // Look for an existing comment from our bot
    let bot_comment = comments.iter().find(|comment| {
        comment["user"]["login"]
            .as_str()
            .map(|login| login == "github-actions[bot]")
            .unwrap_or(false)
    });

    if let Some(existing_comment) = bot_comment {
        // Update the existing comment
        let comment_url = existing_comment["url"].as_str().unwrap();
        let response = client
            .patch(comment_url)
            .header("Authorization", format!("token {}", token))
            .header("User-Agent", "sp1-perf-bot")
            .json(&json!({
                "body": message
            }))
            .send()
            .await?;

        if !response.status().is_success() {
            return Err(format!("Failed to update comment: {:?}", response.text().await?).into());
        }
    } else {
        // Create a new comment
        let response = client
            .post(&comments_url)
            .header("Authorization", format!("token {}", token))
            .header("User-Agent", "sp1-perf-bot")
            .json(&json!({
                "body": message
            }))
            .send()
            .await?;

        if !response.status().is_success() {
            return Err(format!("Failed to post comment: {:?}", response.text().await?).into());
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_results() {
        let dummy_reports = vec![
            PerformanceReport {
                program: "fibonacci".to_string(),
                cycles: 11291,
                exec_khz: 29290.0,
                core_khz: 30.0,
                compressed_khz: 0.1,
                time: 622.385,
                success: true,
            },
            PerformanceReport {
                program: "super-program".to_string(),
                cycles: 275735600,
                exec_khz: 70190.0,
                core_khz: 310.0,
                compressed_khz: 120.0,
                time: 812.285,
                success: true,
            },
        ];

        let args = EvalArgs {
            programs: vec!["fibonacci".to_string(), "super-program".to_string()],
            shard_size: None,
            post_to_slack: Some(false),
            slack_channel_id: None,
            slack_token: None,
            post_to_github: Some(true),
            github_token: Some("abcdef1234567890".to_string()),
            repo_owner: Some("succinctlabs".to_string()),
            repo_name: Some("sp1".to_string()),
            pr_number: Some("123456".to_string()),
            branch_name: Some("feature-branch".to_string()),
            commit_hash: Some("abcdef1234567890".to_string()),
            author: Some("John Doe".to_string()),
        };

        let formatted_results = format_results(&args, &dummy_reports);

        for line in &formatted_results {
            println!("{}", line);
        }

        assert_eq!(formatted_results.len(), 3);
        assert!(formatted_results[0].contains("SP1 Performance Test Results"));
        assert!(formatted_results[1].contains("*Branch*: feature-branch"));
        assert!(formatted_results[1].contains("*Commit*: abcdef12"));
        assert!(formatted_results[1].contains("*Author*: John Doe"));
        assert!(formatted_results[2].contains("fibonacci"));
        assert!(formatted_results[2].contains("super-program"));
    }
}