-
Notifications
You must be signed in to change notification settings - Fork 1.2k
grpc-benchmark: Worker and server implementation #2706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
975f147
78f6c3d
df57997
c55fa21
b9bce39
0dcf683
16a11dd
74f0cd1
7af7498
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| * IN THE SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| use std::net::IpAddr; | ||
| use std::net::Ipv4Addr; | ||
| use std::net::SocketAddr; | ||
| use std::time::Duration; | ||
|
|
||
| use grpc_benchmark::generated::services::grpc::testing::worker_service_server::WorkerServiceServer; | ||
| use grpc_benchmark::worker::WorkerServer; | ||
| use tokio::sync::mpsc; | ||
| use tokio::time; | ||
| use tonic::transport::Server; | ||
|
|
||
| pub async fn run_worker(worker_port: u16) -> Result<(), Box<dyn std::error::Error>> { | ||
| let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), worker_port); | ||
| let (tx, mut rx) = mpsc::channel(1); | ||
|
|
||
| let svc = WorkerServiceServer::new(WorkerServer::new(tx)); | ||
|
|
||
| Server::builder() | ||
| .add_service(svc) | ||
| .serve_with_shutdown(addr, async { | ||
| rx.recv().await; | ||
| // Wait for the quit_worker response to be sent. | ||
| time::sleep(Duration::from_secs(1)).await; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we sleep 1 second here? Will the client fail if the server hard shuts down before the call competes successfully or something? |
||
| }) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| // The default Tokio runtime uses 1 thread per logical processor. While the | ||
| // testing framework supports specifying the thread count in the test config, | ||
| // the tests that run on k8s use specific machine sizes and don't depend on | ||
| // the clients/servers to restrict their resource usage. Tokio doesn't | ||
| // support nested runtimes, so support for per test thread config is not | ||
| // presently supported. | ||
|
|
||
| let mut driver_port = None; | ||
|
|
||
| // Skip the first argument (the binary name itself). | ||
| for arg in std::env::args().skip(1) { | ||
| if let Some(port_str) = arg.strip_prefix("--driver_port=") { | ||
| driver_port = Some(port_str.parse::<u16>().unwrap_or_else(|_| { | ||
| eprintln!("Error: --driver_port must be a valid u16 integer."); | ||
| std::process::exit(1); | ||
| })); | ||
| } else { | ||
| eprintln!("Warning: Unrecognized argument '{}'", arg); | ||
| } | ||
| } | ||
|
|
||
| let Some(dp) = driver_port else { | ||
| eprintln!("Usage: worker --driver_port=<port>"); | ||
| std::process::exit(1); | ||
| }; | ||
|
|
||
| run_worker(dp).await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| * IN THE SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct Rusage { | ||
| user_time_ns: i64, | ||
| system_time_ns: i64, | ||
| } | ||
|
|
||
| impl Rusage { | ||
| #[cfg(unix)] | ||
| pub(crate) fn now() -> Result<Self, String> { | ||
| use nix::sys::resource::UsageWho; | ||
| use nix::sys::resource::getrusage; | ||
| use nix::sys::time::TimeValLike; | ||
|
Comment on lines
+34
to
+36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the philosophy for this? When do we |
||
|
|
||
| let usage = | ||
| getrusage(UsageWho::RUSAGE_SELF).map_err(|e| format!("failed to get rusage: {}", e))?; | ||
|
|
||
| Ok(Rusage { | ||
| user_time_ns: usage.user_time().num_nanoseconds(), | ||
| system_time_ns: usage.system_time().num_nanoseconds(), | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| pub(crate) fn now() -> Result<Rusage, String> { | ||
| Ok(Rusage { | ||
| user_time_ns: 0, | ||
| system_time_ns: 0, | ||
| }) | ||
| } | ||
|
|
||
| pub(crate) fn user_time_nanos(&self) -> i64 { | ||
| self.user_time_ns | ||
| } | ||
|
|
||
| pub(crate) fn system_time_nanos(&self) -> i64 { | ||
| self.system_time_ns | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
quit_tx, mut quit_rx?