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 memory usage, computed by difference between virtual rss 15 //! printed. 16 17 #[cfg(unix)] 18 use std::process; 19 #[cfg(unix)] 20 use std::process::Command; 21 22 #[cfg(unix)] 23 use ylong_runtime::builder::RuntimeBuilder; 24 #[cfg(unix)] 25 use ylong_runtime::executor::Runtime; 26 27 #[cfg(unix)] ylong_runtime() -> Runtime28fn ylong_runtime() -> Runtime { 29 RuntimeBuilder::new_multi_thread().build().unwrap() 30 } 31 32 #[cfg(unix)] get_memory_info()33fn get_memory_info() { 34 let pid = process::id(); 35 println!("pid {pid:}"); 36 let cmd = format!("/proc/{pid:}/status"); 37 println!("cmd {cmd}"); 38 let result = Command::new("cat") 39 .arg(cmd) 40 .output() 41 .expect("fail to execute"); 42 let out = std::str::from_utf8(&result.stdout).unwrap(); 43 println!("status: \n{out}"); 44 } 45 46 #[cfg(unix)] 47 macro_rules! memory { 48 ($runtime: ident) => { 49 println!("Before building runtime: "); 50 get_memory_info(); 51 let runtime = $runtime(); 52 println!("After building runtime: "); 53 get_memory_info(); 54 55 runtime.block_on(async move { 56 println!("hello world"); 57 }); 58 }; 59 } 60 main()61fn main() { 62 println!("ylong:"); 63 #[cfg(unix)] 64 memory!(ylong_runtime); 65 } 66