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 //! Sleep latency in ylong_runtime.
15 
16 use std::time::{Duration, Instant};
17 
main()18 fn main() {
19     let mut handlers = vec![];
20     for _ in 0..1000 {
21         let handle = ylong_runtime::spawn(async move {
22             let duration = Duration::from_millis(100);
23             let start = Instant::now();
24             ylong_runtime::time::sleep(duration).await;
25             let since = start.elapsed();
26             let latency = since.saturating_sub(duration).as_millis();
27             println!("since is {}", since.as_millis());
28             latency
29         });
30         handlers.push(handle);
31     }
32     let mut average = 0;
33     for handler in handlers {
34         let time = ylong_runtime::block_on(handler).unwrap();
35         average += time;
36     }
37     average /= 1000;
38     println!("ylong average latency is {} millisecond", average);
39 
40     let runtime = tokio::runtime::Runtime::new().unwrap();
41     let mut handlers = vec![];
42     for _ in 0..1000 {
43         let handle = runtime.spawn(async move {
44             let duration = Duration::from_millis(100);
45             let start = Instant::now();
46             tokio::time::sleep(duration).await;
47             let since = start.elapsed();
48             let latency = since.saturating_sub(duration).as_millis();
49             println!("since is {}", since.as_millis());
50             latency
51         });
52         handlers.push(handle);
53     }
54     let mut average = 0;
55     for handler in handlers {
56         let time = runtime.block_on(handler).unwrap();
57         average += time;
58     }
59     average /= 1000;
60     println!("average latency is {} millisecond", average);
61 }
62