tasm_lib/
snippet_bencher.rs

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
use std::fs::create_dir_all;
use std::fs::File;
use std::path::Path;
use std::path::PathBuf;

use serde::Deserialize;
use serde::Serialize;
use serde_json::to_writer_pretty;
use triton_vm::aet::AlgebraicExecutionTrace;
use triton_vm::prelude::TableId;

use crate::traits::deprecated_snippet::DeprecatedSnippet;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BenchmarkResult {
    pub clock_cycle_count: usize,
    pub hash_table_height: usize,
    pub u32_table_height: usize,
    pub op_stack_table_height: usize,
    pub ram_table_height: usize,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NamedBenchmarkResult {
    pub name: String,
    pub benchmark_result: BenchmarkResult,
    pub case: BenchmarkCase,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum BenchmarkCase {
    CommonCase,
    WorstCase,
}

impl BenchmarkResult {
    pub fn new(aet: &AlgebraicExecutionTrace) -> Self {
        BenchmarkResult {
            clock_cycle_count: aet.height_of_table(TableId::Processor),
            hash_table_height: aet.height_of_table(TableId::Hash),
            u32_table_height: aet.height_of_table(TableId::U32),
            op_stack_table_height: aet.height_of_table(TableId::OpStack),
            ram_table_height: aet.height_of_table(TableId::Ram),
        }
    }
}

#[allow(dead_code)]
pub fn benchmark_snippet_deprecated<T: DeprecatedSnippet>(snippet: T) -> Vec<NamedBenchmarkResult> {
    let mut benchmarks = Vec::with_capacity(2);

    for (case, mut execution_state) in [
        (BenchmarkCase::CommonCase, snippet.common_case_input_state()),
        (BenchmarkCase::WorstCase, snippet.worst_case_input_state()),
    ] {
        let benchmark_result = snippet
            .link_and_run_tasm_from_state_for_bench(&mut execution_state)
            .unwrap();
        let benchmark = NamedBenchmarkResult {
            name: snippet.entrypoint_name(),
            benchmark_result,
            case,
        };
        benchmarks.push(benchmark);
    }

    benchmarks
}

#[allow(dead_code)]
pub fn write_benchmarks(benchmarks: Vec<NamedBenchmarkResult>) {
    let mut path = PathBuf::new();
    path.push("benchmarks");
    create_dir_all(&path).expect("benchmarks directory should exist");

    let function_name = &benchmarks[0].name;
    for fnname in benchmarks.iter().map(|x| &x.name) {
        assert_eq!(
            function_name, fnname,
            "all fn names must agree for benchmark writing to disk"
        );
    }

    path.push(Path::new(&function_name).with_extension("json"));
    let output = File::create(&path).expect("open file for writing");
    to_writer_pretty(output, &benchmarks).expect("write json to file");
}

#[allow(dead_code)]
pub fn bench_and_write<T: DeprecatedSnippet>(snippet: T) {
    write_benchmarks(benchmark_snippet_deprecated(snippet));
}