forked from rust-vmm/vm-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolatile.rs
More file actions
50 lines (41 loc) · 1.68 KB
/
volatile.rs
File metadata and controls
50 lines (41 loc) · 1.68 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
// Copyright (C) 2020 Alibaba Cloud. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
use std::hint::black_box;
pub use criterion::Criterion;
use vm_memory::volatile_memory::VolatileMemory;
use vm_memory::VolatileSlice;
pub fn benchmark_for_volatile(c: &mut Criterion) {
let mut a = [0xa5u8; 1024];
let vslice = VolatileSlice::from(&mut a[..]);
let v_ref8 = vslice.get_slice(0, vslice.len()).unwrap();
let mut d8 = [0u8; 1024];
// Check performance for read operations.
c.bench_function("VolatileSlice::copy_to_u8", |b| {
b.iter(|| v_ref8.copy_to(black_box(&mut d8[..])))
});
let v_ref16 = vslice.get_slice(0, vslice.len() / 2).unwrap();
let mut d16 = [0u16; 512];
c.bench_function("VolatileSlice::copy_to_u16", |b| {
b.iter(|| v_ref16.copy_to(black_box(&mut d16[..])))
});
benchmark_volatile_copy_to_volatile_slice(c);
// Check performance for write operations.
c.bench_function("VolatileSlice::copy_from_u8", |b| {
b.iter(|| v_ref8.copy_from(black_box(&d8[..])))
});
c.bench_function("VolatileSlice::copy_from_u16", |b| {
b.iter(|| v_ref16.copy_from(black_box(&d16[..])))
});
}
fn benchmark_volatile_copy_to_volatile_slice(c: &mut Criterion) {
let mut a = [0xa5u8; 10240];
let vslice = VolatileSlice::from(&mut a[..]);
let a_slice = vslice.get_slice(0, vslice.len()).unwrap();
let mut d = [0u8; 10240];
let vslice2 = VolatileSlice::from(&mut d[..]);
let d_slice = vslice2.get_slice(0, vslice2.len()).unwrap();
c.bench_function("VolatileSlice::copy_to_volatile_slice", |b| {
b.iter(|| black_box(a_slice).copy_to_volatile_slice(d_slice))
});
}