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 //! Tcp async benchmark
15 
16 use std::time::Instant;
17 
18 use ylong_runtime::builder::RuntimeBuilder;
19 use ylong_runtime::io::{AsyncReadExt, AsyncWriteExt};
20 use ylong_runtime::net::{TcpListener, TcpStream};
21 
run_client(addr: &str)22 async fn run_client(addr: &str) {
23     let mut client_stream = TcpStream::connect(addr).await;
24     while client_stream.is_err() {
25         client_stream = TcpStream::connect(addr).await
26     }
27     let mut client = client_stream.unwrap();
28     for _ in 0..100 {
29         let mut buf = [1; 16384];
30         let n = client.write(&buf).await.unwrap();
31         assert_eq!(n, 16384);
32 
33         let n = client.read(&mut buf).await.unwrap();
34         assert_eq!(n, 16384)
35     }
36 }
37 
run_server(addr: &str)38 async fn run_server(addr: &str) {
39     let socket = TcpListener::bind(addr).await.unwrap();
40     let mut server = socket.accept().await.unwrap().0;
41 
42     for _ in 0..100 {
43         let mut buf = [0; 16384];
44         let n = server.read(&mut buf).await.unwrap();
45         assert_eq!(n, 16384);
46 
47         let buf = [2; 16384];
48         let n = server.write(&buf).await.unwrap();
49         assert_eq!(n, 16384);
50     }
51 }
52 
main()53 fn main() {
54     let runtime = RuntimeBuilder::new_multi_thread()
55         .max_blocking_pool_size(5)
56         .build()
57         .unwrap();
58 
59     let st = Instant::now();
60     for _ in 0..100 {
61         let handle1 = runtime.spawn(async move {
62             run_client("127.0.0.1:2000").await;
63         });
64 
65         let handle2 = runtime.spawn(async move {
66             run_server("127.0.0.1:2000").await;
67         });
68 
69         let handle3 = runtime.spawn(async move {
70             run_client("127.0.0.1:2001").await;
71         });
72 
73         let handle4 = runtime.spawn(async move {
74             run_server("127.0.0.1:2001").await;
75         });
76 
77         let handle5 = runtime.spawn(async move {
78             run_client("127.0.0.1:2002").await;
79         });
80 
81         let handle6 = runtime.spawn(async move {
82             run_server("127.0.0.1:2002").await;
83         });
84 
85         let handle7 = runtime.spawn(async move {
86             run_client("127.0.0.1:2003").await;
87         });
88 
89         let handle8 = runtime.spawn(async move {
90             run_server("127.0.0.1:2003").await;
91         });
92 
93         let handle9 = runtime.spawn(async move {
94             run_client("127.0.0.1:2004").await;
95         });
96 
97         let handle10 = runtime.spawn(async move {
98             run_server("127.0.0.1:2004").await;
99         });
100 
101         let _ = runtime.block_on(handle1);
102         let _ = runtime.block_on(handle2);
103         let _ = runtime.block_on(handle3);
104         let _ = runtime.block_on(handle4);
105         let _ = runtime.block_on(handle5);
106         let _ = runtime.block_on(handle6);
107         let _ = runtime.block_on(handle7);
108         let _ = runtime.block_on(handle8);
109         let _ = runtime.block_on(handle9);
110         let _ = runtime.block_on(handle10);
111     }
112     let time = st.elapsed().as_secs_f64();
113     println!("time: {time:?}");
114 }
115