1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 //! Benchmarks for the tcp. 15 16 #![feature(test)] 17 18 extern crate core; 19 20 mod task_helpers; 21 22 #[macro_export] 23 macro_rules! tokio_task_creation_global { 24 ($runtime: expr, $bench: ident, $task_num: literal) => { 25 #[bench] 26 fn $bench(b: &mut Bencher) { 27 let runtime = $runtime; 28 b.iter(black_box(|| { 29 let mut handlers = Vec::new(); 30 for _ in 0..$task_num { 31 handlers.push(runtime.spawn(async move { 1 })); 32 } 33 })); 34 } 35 }; 36 } 37 38 #[macro_export] 39 macro_rules! ylong_task_creation_global { 40 ($bench: ident, $task_num: literal) => { 41 #[bench] 42 fn $bench(b: &mut Bencher) { 43 ylong_runtime_init(); 44 b.iter(black_box(|| { 45 let mut handlers = Vec::new(); 46 for _ in 0..$task_num { 47 handlers.push(ylong_runtime::spawn(async move { 1 })); 48 } 49 })); 50 } 51 }; 52 } 53 54 #[macro_export] 55 macro_rules! tokio_task_creation_local { 56 ($runtime: expr, $bench: ident, $task_num: literal) => { 57 #[bench] 58 fn $bench(b: &mut Bencher) { 59 let runtime = $runtime; 60 b.iter(black_box(|| { 61 let handle = runtime.spawn(async move { 62 let mut handlers = Vec::new(); 63 for _ in 0..$task_num { 64 handlers.push(tokio::spawn(async move { 1 })); 65 } 66 }); 67 runtime.block_on(handle).unwrap(); 68 })); 69 } 70 }; 71 } 72 73 #[macro_export] 74 macro_rules! ylong_task_creation_local { 75 ($bench: ident, $task_num: literal) => { 76 #[bench] 77 fn $bench(b: &mut Bencher) { 78 ylong_runtime_init(); 79 b.iter(black_box(|| { 80 let handle = ylong_runtime::spawn(async move { 81 let mut handlers = Vec::new(); 82 for _ in 0..$task_num { 83 handlers.push(ylong_runtime::spawn(async move { 1 })); 84 } 85 }); 86 ylong_runtime::block_on(handle).unwrap(); 87 })); 88 } 89 }; 90 } 91 92 #[cfg(test)] 93 mod task_creation { 94 extern crate test; 95 96 use std::hint::black_box; 97 98 use test::Bencher; 99 100 use crate::task_helpers::{tokio_runtime, ylong_runtime_init}; 101 102 ylong_task_creation_global!(ylong_task_10_global, 10); 103 ylong_task_creation_global!(ylong_task_100_global, 100); 104 ylong_task_creation_global!(ylong_task_1000_global, 1000); 105 106 tokio_task_creation_global!(tokio_runtime(), tokio_task_10_global, 10); 107 tokio_task_creation_global!(tokio_runtime(), tokio_task_100_global, 100); 108 tokio_task_creation_global!(tokio_runtime(), tokio_task_1000_global, 1000); 109 110 ylong_task_creation_local!(ylong_task_10_local, 10); 111 ylong_task_creation_local!(ylong_task_100_local, 100); 112 ylong_task_creation_local!(ylong_task_1000_local, 1000); 113 114 tokio_task_creation_local!(tokio_runtime(), tokio_task_10_local, 10); 115 tokio_task_creation_local!(tokio_runtime(), tokio_task_100_local, 100); 116 tokio_task_creation_local!(tokio_runtime(), tokio_task_1000_local, 1000); 117 } 118