mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-28 15:22:30 +01:00
* Rewrite tarpc on top of tokio. * Add examples * Move error types to their own module. Also, cull unused error variants. * Remove unused fn * Remove CanonicalRpcError* types. They're 100% useless. * Track tokio master (WIP) * The great error revamp. Removed the canonical rpc error type. Instead, the user declares the error type for each rpc: In the above example, the error type is Baz. Declaring an error is optional; if none is specified, it defaults to Never, a convenience struct that wraps the never type (exclamation mark) to impl Serialize, Deserialize, Error, etc. Also adds the convenience type StringError for easily using a String as an error type. * Add missing license header * Minor cleanup * Rename StringError => Message * Create a sync::Connect trait. Along with this, the existing Connect trait moves to future::Connect. The future and sync modules are reexported from the crate root. Additionally, the utility errors Never and Message are no longer reexported from the crate root. * Update readme * Track tokio/futures master. Add a Spawn utility trait to replace the removed forget. * Fix pre-push hook * Add doc comment to SyncServiceExt. * Fix up some documentation * Track tokio-proto master * Don't set tcp nodelay * Make future::Connect take an associated type for the future. * Unbox FutureClient::connect return type * Use type alias instead of newtype struct for ClientFuture * Fix benches/latency.rs * Write a plugin to convert lower_snake_case idents/types to UpperCamelCase. Use it to add associated types to FutureService instead of boxing the return futures. * Specify plugin = true in snake_to_camel/Cargo.toml. Weird things happen otherwise. * Add clippy.toml
101 lines
2.7 KiB
Rust
101 lines
2.7 KiB
Rust
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
|
|
// This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
#![feature(conservative_impl_trait, plugin)]
|
|
#![plugin(snake_to_camel)]
|
|
|
|
#[macro_use]
|
|
extern crate lazy_static;
|
|
#[macro_use]
|
|
extern crate tarpc;
|
|
extern crate env_logger;
|
|
extern crate futures;
|
|
|
|
use std::sync::Arc;
|
|
use std::time;
|
|
use std::net;
|
|
use std::thread;
|
|
use std::io::{Read, Write, stdout};
|
|
use tarpc::util::Never;
|
|
use tarpc::sync::Connect;
|
|
|
|
lazy_static! {
|
|
static ref BUF: Arc<Vec<u8>> = Arc::new(gen_vec(CHUNK_SIZE as usize));
|
|
}
|
|
|
|
fn gen_vec(size: usize) -> Vec<u8> {
|
|
let mut vec: Vec<u8> = Vec::with_capacity(size);
|
|
for i in 0..size {
|
|
vec.push((i % 1 << 8) as u8);
|
|
}
|
|
vec
|
|
}
|
|
|
|
service! {
|
|
rpc read() -> Arc<Vec<u8>>;
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct Server;
|
|
|
|
impl FutureService for Server {
|
|
type Read = futures::Finished<Arc<Vec<u8>>, Never>;
|
|
|
|
fn read(&self) -> Self::Read {
|
|
futures::finished(BUF.clone())
|
|
}
|
|
}
|
|
|
|
const CHUNK_SIZE: u32 = 1 << 19;
|
|
|
|
fn bench_tarpc(target: u64) {
|
|
let handle = Server.listen("localhost:0").unwrap();
|
|
let client = SyncClient::connect(handle.local_addr()).unwrap();
|
|
let start = time::Instant::now();
|
|
let mut nread = 0;
|
|
while nread < target {
|
|
nread += client.read().unwrap().len() as u64;
|
|
print!(".");
|
|
stdout().flush().unwrap();
|
|
}
|
|
println!("done");
|
|
let duration = time::Instant::now() - start;
|
|
println!("TARPC: {}MB/s",
|
|
(target as f64 / (1024f64 * 1024f64)) /
|
|
(duration.as_secs() as f64 + duration.subsec_nanos() as f64 / 10E9));
|
|
}
|
|
|
|
fn bench_tcp(target: u64) {
|
|
let l = net::TcpListener::bind("localhost:0").unwrap();
|
|
let addr = l.local_addr().unwrap();
|
|
thread::spawn(move || {
|
|
let (mut stream, _) = l.accept().unwrap();
|
|
while let Ok(_) = stream.write_all(&*BUF) {
|
|
}
|
|
});
|
|
let mut stream = net::TcpStream::connect(&addr).unwrap();
|
|
let mut buf = vec![0; CHUNK_SIZE as usize];
|
|
let start = time::Instant::now();
|
|
let mut nread = 0;
|
|
while nread < target {
|
|
stream.read_exact(&mut buf[..]).unwrap();
|
|
nread += CHUNK_SIZE as u64;
|
|
print!(".");
|
|
stdout().flush().unwrap();
|
|
}
|
|
println!("done");
|
|
let duration = time::Instant::now() - start;
|
|
println!("TCP: {}MB/s",
|
|
(target as f64 / (1024f64 * 1024f64)) /
|
|
(duration.as_secs() as f64 + duration.subsec_nanos() as f64 / 10E9));
|
|
}
|
|
|
|
fn main() {
|
|
let _ = env_logger::init();
|
|
&*BUF; // to non-lazily initialize it.
|
|
bench_tcp(256 << 20);
|
|
bench_tarpc(256 << 20);
|
|
}
|