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 task scheduling.
15 
16 #![feature(test)]
17 
18 mod task_helpers;
19 
20 #[macro_export]
21 macro_rules! tokio_schedule_task {
22     ($runtime: expr, $bench: ident, $num: literal, $upper: literal) => {
23         #[bench]
24         fn $bench(b: &mut Bencher) {
25             let runtime = $runtime;
26             b.iter(black_box(|| {
27                 let mut handlers = Vec::with_capacity($num);
28                 for _ in 0..$num {
29                     handlers.push(runtime.spawn(async move {
30                         fibbo($upper);
31                         yield_now().await;
32                     }));
33                 }
34 
35                 for handler in handlers {
36                     let _ = runtime.block_on(handler).unwrap();
37                 }
38             }));
39         }
40     };
41 }
42 
43 #[macro_export]
44 macro_rules! ylong_schedule_task {
45     ($bench: ident, $num: literal, $upper: literal) => {
46         #[bench]
47         fn $bench(b: &mut Bencher) {
48             b.iter(black_box(|| {
49                 let mut handlers = Vec::with_capacity($num);
50                 for _ in 0..$num {
51                     handlers.push(ylong_runtime::spawn(async move {
52                         fibbo($upper);
53                         yield_now().await;
54                     }));
55                 }
56 
57                 for handler in handlers {
58                     let _ = ylong_runtime::block_on(handler).unwrap();
59                 }
60             }));
61         }
62     };
63 }
64 
65 #[cfg(test)]
66 mod tokio_schedule_bench {
67     extern crate test;
68 
69     use std::hint::black_box;
70 
71     use test::Bencher;
72     use ylong_runtime::task::yield_now;
73 
74     pub use crate::task_helpers::{fibbo, tokio_runtime};
75 
76     tokio_schedule_task!(tokio_runtime(), tokio_task_10_15, 10, 15);
77     tokio_schedule_task!(tokio_runtime(), tokio_task_120_15, 120, 15);
78     tokio_schedule_task!(tokio_runtime(), tokio_task_10_30, 10, 30);
79     tokio_schedule_task!(tokio_runtime(), tokio_task_120_30, 120, 30);
80 }
81 
82 #[cfg(test)]
83 mod ylong_schedule_bench {
84     extern crate test;
85 
86     use std::hint::black_box;
87 
88     use test::Bencher;
89     use ylong_runtime::task::yield_now;
90 
91     pub use crate::task_helpers::fibbo;
92 
93     ylong_schedule_task!(ylong_task_10_15, 10, 15);
94     ylong_schedule_task!(ylong_task_120_15, 120, 15);
95     ylong_schedule_task!(ylong_task_10_30, 10, 30);
96     ylong_schedule_task!(ylong_task_120_30, 120, 30);
97 }
98