File size: 1,713 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::{borrow::Cow, fs};

use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize};
use tabled::{Style, Table, Tabled};

#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
struct BenchSuite {
    suite: String,
    node_duration: String,
    rust_duration: String,
    rust_speedup: String,
    is_faster: bool,
}

impl Tabled for BenchSuite {
    const LENGTH: usize = 4;

    fn fields(&self) -> Vec<Cow<'_, str>> {
        fn g(s: &str) -> Cow<'_, str> {
            Cow::Owned(s.green().to_string())
        }
        fn r(s: &str) -> Cow<'_, str> {
            Cow::Owned(s.red().to_string())
        }
        if self.is_faster {
            [
                g(&self.suite),
                r(&self.node_duration),
                g(&self.rust_duration),
                g(&self.rust_speedup),
            ]
        } else {
            [
                r(&self.suite),
                g(&self.node_duration),
                r(&self.rust_duration),
                r(&self.rust_speedup),
            ]
        }
        .into_iter()
        .collect()
    }

    fn headers() -> Vec<Cow<'static, str>> {
        ["Suite", "@vercel/nft duration", "Rust duration", "Speedup"]
            .map(Cow::Borrowed)
            .into_iter()
            .collect()
    }
}

pub fn show_result() {
    let bench_result_raw = fs::read_to_string("crates/turbopack/bench.json").unwrap();
    let mut results = bench_result_raw
        .lines()
        .flat_map(|line| {
            let suite: Vec<BenchSuite> = serde_json::from_str(line).unwrap();
            suite
        })
        .collect::<Vec<_>>();
    results.sort();
    println!("{}", Table::new(results).with(Style::modern()));
}