Skip to content

Commit

Permalink
optimize fortunes benchmark (#4043)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 authored and michaelhixson committed Sep 15, 2018
1 parent cffabe9 commit f86dee7
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 369 deletions.
619 changes: 336 additions & 283 deletions frameworks/Rust/actix/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion frameworks/Rust/actix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix"
version = "0.7.1"
version = "0.7.2"
build = "build.rs"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/actix/actix-diesel.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.28
FROM rust:1.29.0

ADD ./ /actix
WORKDIR /actix
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/actix/actix-pg.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.28
FROM rust:1.29.0

ADD ./ /actix
WORKDIR /actix
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/actix/actix-raw.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.28
FROM rust:1.29.0

ADD ./ /actix
WORKDIR /actix
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/actix/actix.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.28
FROM rust:1.29.0

ADD ./ /actix
WORKDIR /actix
Expand Down
6 changes: 2 additions & 4 deletions frameworks/Rust/actix/src/db_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ impl PgConnection {
act.cl = Some(cl);
Arbiter::spawn(conn.map_err(|e| panic!("{}", e)));
fut::ok(())
})
.wait(ctx);
}).wait(ctx);

act
})
Expand Down Expand Up @@ -228,8 +227,7 @@ impl Handler<TellFortune> for PgConnection {
message: row.get(1),
});
Ok::<_, io::Error>(items)
})
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}).map_err(|e| io::Error::new(io::ErrorKind::Other, e))
.and_then(|mut items| {
items.sort_by(|it, next| it.message.cmp(&next.message));
Ok(items)
Expand Down
35 changes: 19 additions & 16 deletions frameworks/Rust/actix/src/db_pg_direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct PgConnection {
}

impl PgConnection {
pub fn connect(db_url: &str) -> impl Future<Item=PgConnection, Error=()> {
pub fn connect(db_url: &str) -> impl Future<Item = PgConnection, Error = ()> {
let hs = connect(db_url.parse().unwrap(), TlsMode::None);

hs.map_err(|_| panic!("can not connect to postgresql"))
Expand All @@ -32,12 +32,11 @@ impl PgConnection {
cl.prepare("SELECT id FROM world WHERE id=$1")
.map_err(|_| ())
.and_then(|update| {

Ok(PgConnection {
cl,
fortune: st.0,
world: st.1,
update
update,
})
})
})
Expand All @@ -47,8 +46,7 @@ impl PgConnection {
}

impl PgConnection {

pub fn get_world(&self) -> impl Future<Item=World, Error=io::Error> {
pub fn get_world(&self) -> impl Future<Item = World, Error = io::Error> {
let random_id = thread_rng().gen_range::<i32>(1, 10_001);

self.cl
Expand All @@ -64,7 +62,9 @@ impl PgConnection {
})
}

pub fn get_worlds(&self, num: usize) -> impl Future<Item=Vec<World>, Error=io::Error> {
pub fn get_worlds(
&self, num: usize,
) -> impl Future<Item = Vec<World>, Error = io::Error> {
let mut worlds = Vec::with_capacity(num);
for _ in 0..num {
let w_id: i32 = thread_rng().gen_range(1, 10_001);
Expand All @@ -86,7 +86,9 @@ impl PgConnection {
stream::futures_unordered(worlds).collect()
}

pub fn update(&self, num: usize) -> impl Future<Item=Vec<World>, Error=io::Error> {
pub fn update(
&self, num: usize,
) -> impl Future<Item = Vec<World>, Error = io::Error> {
let mut worlds = Vec::with_capacity(num);
for _ in 0..num {
let id: i32 = thread_rng().gen_range(1, 10_001);
Expand All @@ -111,25 +113,27 @@ impl PgConnection {
.collect()
.and_then(move |mut worlds| {
let mut update = String::with_capacity(120 + 6 * num as usize);
update
.push_str("UPDATE world SET randomnumber = temp.randomnumber FROM (VALUES ");
update.push_str(
"UPDATE world SET randomnumber = temp.randomnumber FROM (VALUES ",
);

for w in &worlds {
update.push_str(&format!("({}, {}),", w.id, w.randomnumber));
}
worlds.sort_by_key(|w| w.id);

update.pop();
update
.push_str(" ORDER BY 1) AS temp(id, randomnumber) WHERE temp.id = world.id");
update.push_str(
" ORDER BY 1) AS temp(id, randomnumber) WHERE temp.id = world.id",
);

cl.batch_execute(&update)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
.and_then(|_| Ok(worlds))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
.and_then(|_| Ok(worlds))
})
}

pub fn tell_fortune(&self) -> impl Future<Item=Vec<Fortune>, Error=io::Error> {
pub fn tell_fortune(&self) -> impl Future<Item = Vec<Fortune>, Error = io::Error> {
let mut items = Vec::new();
items.push(Fortune {
id: 0,
Expand All @@ -144,8 +148,7 @@ impl PgConnection {
message: row.get(1),
});
Ok::<_, io::Error>(items)
})
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}).map_err(|e| io::Error::new(io::ErrorKind::Other, e))
.and_then(|mut items| {
items.sort_by(|it, next| it.message.cmp(&next.message));
Ok(items)
Expand Down
6 changes: 3 additions & 3 deletions frameworks/Rust/actix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ fn main() {
.resource("/json", |r| r.f(json))
.resource("/plaintext", |r| r.f(plaintext))
}).backlog(8192)
.bind("0.0.0.0:8080")
.unwrap()
.start();
.bind("0.0.0.0:8080")
.unwrap()
.start();

println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
Expand Down
18 changes: 7 additions & 11 deletions frameworks/Rust/actix/src/main_diesel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ fn world_row(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
.body(body))
}
Err(_) => Ok(HttpResponse::InternalServerError().into()),
})
.responder()
}).responder()
}

fn queries(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
Expand Down Expand Up @@ -79,8 +78,7 @@ fn queries(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
} else {
Ok(HttpResponse::InternalServerError().into())
}
})
.responder()
}).responder()
}

fn updates(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
Expand Down Expand Up @@ -109,8 +107,7 @@ fn updates(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
} else {
Ok(HttpResponse::InternalServerError().into())
}
})
.responder()
}).responder()
}

#[derive(Template)]
Expand All @@ -136,8 +133,7 @@ fn fortune(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
.body(res))
}
Err(_) => Ok(HttpResponse::InternalServerError().into()),
})
.responder()
}).responder()
}

fn main() {
Expand All @@ -164,9 +160,9 @@ fn main() {
.resource("/queries", |r| r.route().f(queries))
.resource("/updates", |r| r.route().f(updates))
}).backlog(8192)
.bind("0.0.0.0:8080")
.unwrap()
.start();
.bind("0.0.0.0:8080")
.unwrap()
.start();

println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
Expand Down
18 changes: 7 additions & 11 deletions frameworks/Rust/actix/src/main_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ fn world_row(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
.body(body))
}
Err(_) => Ok(HttpResponse::InternalServerError().into()),
})
.responder()
}).responder()
}

fn queries(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
Expand All @@ -75,8 +74,7 @@ fn queries(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
} else {
Ok(HttpResponse::InternalServerError().into())
}
})
.responder()
}).responder()
}

fn updates(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
Expand All @@ -100,8 +98,7 @@ fn updates(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
} else {
Ok(HttpResponse::InternalServerError().into())
}
})
.responder()
}).responder()
}

#[derive(Template)]
Expand All @@ -127,8 +124,7 @@ fn fortune(req: &HttpRequest<State>) -> FutureResponse<HttpResponse> {
.body(res))
}
Err(_) => Ok(HttpResponse::InternalServerError().into()),
})
.responder()
}).responder()
}

fn main() {
Expand All @@ -145,9 +141,9 @@ fn main() {
.resource("/fortune", |r| r.route().f(fortune))
.resource("/updates", |r| r.route().f(updates))
}).backlog(8192)
.bind("0.0.0.0:8080")
.unwrap()
.start();
.bind("0.0.0.0:8080")
.unwrap()
.start();

println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
Expand Down
Loading

0 comments on commit f86dee7

Please sign in to comment.