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 #![allow(dead_code)]
15
16 use std::env;
17 use std::hint::black_box;
18 use std::path::PathBuf;
19 use std::sync::Arc;
20
21 use tokio::sync::RwLock;
22 #[cfg(feature = "multi_instance_runtime")]
23 use ylong_runtime::builder::RuntimeBuilder;
24 #[cfg(feature = "multi_instance_runtime")]
25 use ylong_runtime::executor::Runtime;
26 #[cfg(feature = "sync")]
27 use ylong_runtime::sync::RwLock as YlongRwlock;
28
29 pub const TASK_NUM: usize = 10;
30 pub const FIBBO_ANS: u64 = 1346268;
31
32 const BUFFER_SIZE: usize = 4096;
33 pub const READ_FILE: &str = "/dev/zero";
34 pub const WRITE_FILE: &str = "/dev/null";
35 pub static mut READ_BUFFER: [u8; BUFFER_SIZE] = [0_u8; BUFFER_SIZE];
36 pub static mut WRITE_BUFFER: [u8; BUFFER_SIZE] = [0_u8; BUFFER_SIZE];
37
38 pub const KB: usize = 1024;
39 pub const DIR_PATH: &str = "resources";
40 pub const FILE_NAME: &str = "test";
41
42 pub const YIELD_NUM: usize = 1_000;
43
44 pub type FA<R> = fn() -> R;
45
46 #[cfg(feature = "multi_instance_runtime")]
ylong_runtime() -> Runtime47 pub fn ylong_runtime() -> Runtime {
48 RuntimeBuilder::new_multi_thread().build().unwrap()
49 }
50
51 #[cfg(feature = "multi_instance_runtime")]
ylong_runtime_set_threads(threads_count: usize) -> Runtime52 pub fn ylong_runtime_set_threads(threads_count: usize) -> Runtime {
53 RuntimeBuilder::new_multi_thread()
54 .worker_num(threads_count)
55 .build()
56 .unwrap()
57 }
58
tokio_runtime() -> tokio::runtime::Runtime59 pub fn tokio_runtime() -> tokio::runtime::Runtime {
60 tokio::runtime::Builder::new_multi_thread()
61 .enable_all()
62 .build()
63 .unwrap()
64 }
65
ylong_runtime_init()66 pub fn ylong_runtime_init() {
67 let handle = ylong_runtime::spawn(async move { 1 });
68 let res = ylong_runtime::block_on(handle).unwrap();
69 assert_eq!(res, 1);
70 }
71
tokio_runtime_set_threads(threads_count: usize) -> tokio::runtime::Runtime72 pub fn tokio_runtime_set_threads(threads_count: usize) -> tokio::runtime::Runtime {
73 tokio::runtime::Builder::new_multi_thread()
74 .worker_threads(threads_count)
75 .enable_all()
76 .build()
77 .unwrap()
78 }
79
work() -> u6480 pub fn work() -> u64 {
81 black_box(3)
82 }
83
recur_fibbo(a: u64) -> u6484 pub fn recur_fibbo(a: u64) -> u64 {
85 if a == 0 || a == 1 {
86 return a;
87 }
88 recur_fibbo(a - 1) + recur_fibbo(a - 2)
89 }
90
fibbo(upper: usize) -> u6491 pub fn fibbo(upper: usize) -> u64 {
92 let mut ret = 0;
93 for j in 1..upper {
94 ret += black_box(recur_fibbo(j as u64));
95 }
96 black_box(ret)
97 }
98
init_write_buffer()99 pub fn init_write_buffer() {
100 let mut buffer = [0; BUFFER_SIZE];
101 for (i, tar) in buffer.iter_mut().enumerate().take(BUFFER_SIZE) {
102 *tar = (i % 256) as u8;
103 }
104 unsafe {
105 WRITE_BUFFER
106 .iter_mut()
107 .zip(buffer.as_slice().iter())
108 .for_each(|(tar, src)| *tar = *src);
109 }
110 }
111
112 #[cfg(feature = "sync")]
ylong_rwlock_write_task(lock: Arc<YlongRwlock<()>>)113 pub async fn ylong_rwlock_write_task(lock: Arc<YlongRwlock<()>>) {
114 let _read = lock.write().await;
115 }
116
117 #[cfg(feature = "sync")]
ylong_rwlock_task(lock: Arc<YlongRwlock<()>>)118 pub async fn ylong_rwlock_task(lock: Arc<YlongRwlock<()>>) {
119 let _read = lock.read().await;
120 }
121
tokio_rwlock_write_task(lock: Arc<RwLock<()>>)122 pub async fn tokio_rwlock_write_task(lock: Arc<RwLock<()>>) {
123 let _read = lock.write().await;
124 }
125
tokio_rwlock_task(lock: Arc<RwLock<()>>)126 pub async fn tokio_rwlock_task(lock: Arc<RwLock<()>>) {
127 let _read = lock.read().await;
128 }
129
get_writer_buffer(size: usize) -> Vec<u8>130 pub fn get_writer_buffer(size: usize) -> Vec<u8> {
131 let mut buffer = vec![0; size * KB];
132 for (i, tar) in buffer.iter_mut().enumerate() {
133 *tar = (i % 256) as u8;
134 }
135 buffer
136 }
137
get_file_dir(size: usize) -> PathBuf138 pub fn get_file_dir(size: usize) -> PathBuf {
139 let current_dir = env::current_dir().unwrap();
140 let file_path = current_dir
141 .join(DIR_PATH.to_string())
142 .join(format!("{}_{}.txt", FILE_NAME, size));
143 let _ = std::fs::create_dir_all(file_path.parent().unwrap());
144 file_path
145 }
146