• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 memory usage, computed by difference between virtual rss
15 //! printed.
16 use std::thread::{sleep, spawn};
17 use std::time::Duration;
18 
19 #[cfg(unix)]
get_memory_info()20 fn get_memory_info() {
21     use std::process;
22     use std::process::Command;
23 
24     let pid = process::id();
25     println!("pid {pid}");
26     let cmd = format!("{pid}");
27     let result = Command::new("pmap")
28         .arg("-x")
29         .arg(cmd)
30         .output()
31         .expect("fail to execute");
32     let mut out = String::from_utf8(result.stdout).unwrap();
33     let pos1 = out.find("Mapping").unwrap();
34     let pos2 = out.find("total").unwrap();
35     out.drain(pos1 + 8..pos2);
36     println!("status: \n{out}");
37 }
38 
async_task()39 async fn async_task() {
40     sleep(Duration::from_secs(5));
41 }
42 
task()43 fn task() {
44     sleep(Duration::from_secs(5));
45 }
46 
47 #[cfg(unix)]
ylong_spawn()48 fn ylong_spawn() {
49     let mut handles = Vec::with_capacity(1000);
50     println!("Ylong Runtime Memory Test:");
51     println!("=================Before=================");
52     get_memory_info();
53     for _ in 0..1000 {
54         let handle = ylong_runtime::spawn(async_task());
55         handles.push(handle);
56     }
57     println!("=================After=================");
58     get_memory_info();
59     for handle in handles {
60         let _ = ylong_runtime::block_on(handle);
61     }
62 }
63 
64 #[cfg(unix)]
tokio_spawn()65 fn tokio_spawn() {
66     let mut handles = Vec::with_capacity(1000);
67     println!("Tokio Runtime Memory Test:");
68     println!("=================Before=================");
69     get_memory_info();
70     let runtime = tokio::runtime::Runtime::new().unwrap();
71     for _ in 0..1000 {
72         let handle = runtime.spawn(async_task());
73         handles.push(handle);
74     }
75     println!("=================After=================");
76     get_memory_info();
77     for handle in handles {
78         let _ = runtime.block_on(handle);
79     }
80 }
81 
82 #[cfg(unix)]
std_spawn()83 fn std_spawn() {
84     let mut handles = Vec::with_capacity(1000);
85     println!("Std Runtime Memory Test:");
86     println!("=================Before=================");
87     get_memory_info();
88     for _ in 0..1000 {
89         let handle = spawn(task);
90         handles.push(handle);
91     }
92     println!("=================After=================");
93     get_memory_info();
94     for handle in handles {
95         handle.join().unwrap();
96     }
97 }
98 
main()99 fn main() {
100     tokio_spawn();
101     ylong_spawn();
102     std_spawn();
103 }
104