|
| 1 | +// Copyright (C) 2020 Alibaba Cloud. All rights reserved. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause |
| 4 | + |
| 5 | +pub use criterion::{black_box, Criterion}; |
| 6 | +use vm_memory::volatile_memory::VolatileMemory; |
| 7 | + |
| 8 | +pub fn benchmark_for_volatile(c: &mut Criterion) { |
| 9 | + let mut a = [0xa5u8; 1024]; |
| 10 | + let a_ref = &mut a[..]; |
| 11 | + let v_ref8 = a_ref.get_slice(0, a_ref.len()).unwrap(); |
| 12 | + let v_ref16 = a_ref.get_slice(0, a_ref.len() / 2).unwrap(); |
| 13 | + let mut d8 = [0u8; 1024]; |
| 14 | + let mut d16 = [0u16; 512]; |
| 15 | + |
| 16 | + // Check performance for read operations. |
| 17 | + c.bench_function("VolatileSlice::copy_to_u8", |b| { |
| 18 | + b.iter(|| black_box(v_ref8.copy_to(&mut d8[..]))) |
| 19 | + }); |
| 20 | + c.bench_function("VolatileSlice::copy_to_u16", |b| { |
| 21 | + b.iter(|| black_box(v_ref16.copy_to(&mut d16[..]))) |
| 22 | + }); |
| 23 | + benchmark_volatile_copy_to_volatile_slice(c); |
| 24 | + |
| 25 | + // Check performance for write operations. |
| 26 | + c.bench_function("VolatileSlice::copy_from_u8", |b| { |
| 27 | + b.iter(|| black_box(v_ref8.copy_from(&d8[..]))) |
| 28 | + }); |
| 29 | + c.bench_function("VolatileSlice::copy_from_u16", |b| { |
| 30 | + b.iter(|| black_box(v_ref16.copy_from(&d16[..]))) |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +fn benchmark_volatile_copy_to_volatile_slice(c: &mut Criterion) { |
| 35 | + let mut a = [0xa5u8; 10240]; |
| 36 | + let a_ref = &mut a[..]; |
| 37 | + let a_slice = a_ref.get_slice(0, a_ref.len()).unwrap(); |
| 38 | + let mut d = [0u8; 10240]; |
| 39 | + let d_ref = &mut d[..]; |
| 40 | + let d_slice = d_ref.get_slice(0, d_ref.len()).unwrap(); |
| 41 | + |
| 42 | + c.bench_function("VolatileSlice::copy_to_volatile_slice", |b| { |
| 43 | + b.iter(|| black_box(a_slice.copy_to_volatile_slice(d_slice))) |
| 44 | + }); |
| 45 | +} |
0 commit comments