forked from denoland/eszip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_hash_function.rs
More file actions
194 lines (184 loc) · 5.28 KB
/
source_hash_function.rs
File metadata and controls
194 lines (184 loc) · 5.28 KB
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
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use deno_ast::{EmitOptions, TranspileOptions};
use deno_graph::{
source::{MemoryLoader, Source},
BuildOptions, CapturingModuleAnalyzer, GraphKind, ModuleGraph,
ModuleSpecifier,
};
use eszip::{v2::Checksum, EszipV2};
use futures::io::{AllowStdIo, BufReader};
#[cfg(feature = "sha256")]
fn into_bytes_sha256(mut eszip: EszipV2) -> Vec<u8> {
eszip.set_checksum(Checksum::Sha256);
eszip.into_bytes()
}
#[cfg(feature = "xxhash3")]
fn into_bytes_xxhash3(mut eszip: EszipV2) -> Vec<u8> {
eszip.set_checksum(Checksum::XxHash3);
eszip.into_bytes()
}
fn into_bytes_no_checksum(mut eszip: EszipV2) -> Vec<u8> {
eszip.set_checksum(Checksum::NoChecksum);
eszip.into_bytes()
}
async fn parse(bytes: &[u8]) -> EszipV2 {
let (eszip, fut) = EszipV2::parse(BufReader::new(AllowStdIo::new(bytes)))
.await
.unwrap();
fut.await.unwrap();
eszip
}
fn bench_into_bytes(c: &mut Criterion) {
let mut group = c.benchmark_group("into_bytes()");
group.sample_size(10);
for mb in (1..200).step_by(20) {
group.throughput(criterion::Throughput::Bytes((mb as u64) << 20));
#[cfg(feature = "sha256")]
group.bench_with_input(
BenchmarkId::new("SHA256", format!("{mb}MB")),
&mb,
|b, mb| {
b.iter_batched(
|| {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
rt.block_on(build_eszip(*mb))
},
into_bytes_sha256,
criterion::BatchSize::SmallInput,
)
},
);
#[cfg(feature = "xxhash3")]
group.bench_with_input(
BenchmarkId::new("XXHASH3", format!("{mb}MB")),
&mb,
|b, mb| {
b.iter_batched(
|| {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
rt.block_on(build_eszip(*mb))
},
into_bytes_xxhash3,
criterion::BatchSize::SmallInput,
)
},
);
group.bench_with_input(
BenchmarkId::new("NO-CHECkSUM", format!("{mb}MB")),
&mb,
|b, mb| {
b.iter_batched(
|| {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
rt.block_on(build_eszip(*mb))
},
into_bytes_no_checksum,
criterion::BatchSize::SmallInput,
)
},
);
}
group.finish();
}
fn bench_parse(c: &mut Criterion) {
let mut group = c.benchmark_group("parse()");
group.sample_size(10);
for mb in (1..200).step_by(20) {
group.throughput(criterion::Throughput::Bytes((mb as u64) << 20));
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
#[cfg(feature = "sha256")]
{
let mut eszip = rt.block_on(build_eszip(mb));
eszip.set_checksum(Checksum::Sha256);
let bytes = eszip.into_bytes();
group.bench_with_input(
BenchmarkId::new("SHA256", format!("{mb}MB")),
&bytes,
|b, bytes| b.to_async(&rt).iter(|| parse(bytes)),
);
}
#[cfg(feature = "xxhash3")]
{
let mut eszip = rt.block_on(build_eszip(mb));
eszip.set_checksum(Checksum::XxHash3);
let bytes = eszip.into_bytes();
group.bench_with_input(
BenchmarkId::new("XXHASH3", format!("{mb}MB")),
&bytes,
|b, bytes| b.to_async(&rt).iter(|| parse(bytes)),
);
}
let mut eszip = rt.block_on(build_eszip(mb));
eszip.set_checksum(Checksum::NoChecksum);
let bytes = eszip.into_bytes();
group.bench_with_input(
BenchmarkId::new("NO-CHECKSUM", format!("{mb}MB")),
&bytes,
|b, bytes| b.to_async(&rt).iter(|| parse(bytes)),
);
}
group.finish();
}
criterion_group!(benches, bench_into_bytes, bench_parse);
criterion_main!(benches);
async fn build_eszip(mb: usize) -> EszipV2 {
let roots = vec![ModuleSpecifier::parse("file:///module1.js").unwrap()];
let analyzer = CapturingModuleAnalyzer::default();
let mut graph = ModuleGraph::new(GraphKind::CodeOnly);
let mut sources = vec![(
String::from("file:///module1.js"),
Source::Module {
specifier: String::from("file:///module1.js"),
maybe_headers: None,
content: (2..=mb)
.map(|x| format!(r#"import "./module{x}.js";"#))
.chain([build_comment_module(1)])
.collect::<Vec<String>>()
.join("\n"),
},
)];
for x in 2..=mb {
let specifier = format!("file:///module{x}.js");
sources.push((
specifier.clone(),
Source::Module {
specifier,
maybe_headers: None,
content: build_comment_module(1),
},
))
}
let loader = MemoryLoader::new(sources, Vec::new());
graph
.build(
roots,
&loader,
BuildOptions {
module_analyzer: &analyzer,
..Default::default()
},
)
.await;
graph.valid().unwrap();
EszipV2::from_graph(eszip::FromGraphOptions {
graph,
module_kind_resolver: Default::default(),
parser: analyzer.as_capturing_parser(),
transpile_options: TranspileOptions::default(),
emit_options: EmitOptions::default(),
relative_file_base: None,
npm_packages: None,
})
.unwrap()
}
fn build_comment_module(mb: usize) -> String {
format!("// {}", "a".repeat(mb << 20))
}