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 #[cfg(unix)] get_memory_info()17fn get_memory_info() { 18 use std::process; 19 use std::process::Command; 20 21 let pid = process::id(); 22 println!("pid {pid}"); 23 let cmd = format!("{pid}"); 24 let result = Command::new("pmap") 25 .arg("-x") 26 .arg(cmd) 27 .output() 28 .expect("fail to execute"); 29 let mut out = String::from_utf8(result.stdout).unwrap(); 30 let pos1 = out.find("Mapping").unwrap(); 31 let pos2 = out.find("total").unwrap(); 32 out.drain(pos1 + 8..pos2); 33 println!("status: \n{out}"); 34 } 35 36 #[cfg(unix)] ylong_memory()37fn ylong_memory() { 38 println!("Runtime Memory Test:"); 39 println!("=================Before================="); 40 get_memory_info(); 41 let handler = ylong_runtime::spawn(async move {}); 42 let _ = ylong_runtime::block_on(handler); 43 44 println!("=================After================="); 45 get_memory_info(); 46 } 47 48 #[cfg(unix)] tokio_memory()49fn tokio_memory() { 50 println!("Runtime Memory Test:"); 51 println!("=================Before================="); 52 get_memory_info(); 53 let _runtime = tokio::runtime::Runtime::new().unwrap(); 54 55 println!("=================After================="); 56 get_memory_info(); 57 } 58 main()59fn main() { 60 #[cfg(unix)] 61 tokio_memory(); 62 #[cfg(unix)] 63 ylong_memory(); 64 } 65