Skip to content

Commit cac7817

Browse files
authored
[rust/khttp] update (#10156)
* khttp: remove /plaintext benchmark, use mimalloc * khttp: use static base headers for /json * cleanup
1 parent 2801d3d commit cac7817

File tree

5 files changed

+73
-58
lines changed

5 files changed

+73
-58
lines changed

frameworks/Rust/khttp/Cargo.lock

Lines changed: 53 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/khttp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2024"
77
khttp = { version = "0.2", features = ["epoll"] }
88
yarte = { version = "0.15", features = ["json"] }
99
pq-sys = "0.7"
10+
mimalloc = "0.1"
1011

1112
[profile.release]
1213
opt-level = 3

frameworks/Rust/khttp/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Benchmark for [khttp](https://github.com/karlivory/khttp) - a low-level HTTP/1.1
55
### Test Type Implementation Source Code
66

77
* [JSON](./src/main.rs)
8-
* [PLAINTEXT](./src/main.rs)
98
* [FORTUNES](./src/main.rs)
109

1110
## Test URLs
@@ -14,10 +13,6 @@ Benchmark for [khttp](https://github.com/karlivory/khttp) - a low-level HTTP/1.1
1413

1514
http://localhost:8080/json
1615

17-
### PLAINTEXT
18-
19-
http://localhost:8080/plaintext
20-
2116
### FORTUNES
2217

2318
http://localhost:8080/fortunes

frameworks/Rust/khttp/benchmark_config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{
55
"default": {
66
"json_url": "/json",
7-
"plaintext_url": "/plaintext",
87
"fortune_url": "/fortunes",
98
"port": 8080,
109
"approach": "Realistic",

frameworks/Rust/khttp/src/main.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1+
#[global_allocator]
2+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
3+
14
use khttp::{Headers, Method::*, RequestContext, ResponseHandle, Server, Status};
2-
use std::{ffi::CStr, io, ptr};
5+
use pq_sys::{
6+
ConnStatusType, ExecStatusType, PGconn, PQclear, PQconnectdb, PQerrorMessage, PQexecPrepared,
7+
PQfinish, PQgetlength, PQgetvalue, PQntuples, PQprepare, PQresultStatus, PQstatus,
8+
};
9+
use std::{ffi::CStr, io, ptr, sync::LazyLock};
310
use yarte::{Serialize, ywrite_html};
411

512
#[derive(Serialize)]
613
struct HelloMessage {
714
message: &'static str,
815
}
916

17+
static JSON_HEADERS: LazyLock<Headers<'static>> = LazyLock::new(|| {
18+
let mut headers = Headers::new();
19+
headers.add(Headers::CONTENT_TYPE, b"application/json");
20+
headers.add("server", b"khttp");
21+
headers
22+
});
23+
1024
fn main() {
1125
let mut app = Server::builder("0.0.0.0:8080").unwrap();
1226

13-
app.route(Get, "/plaintext", |_ctx, res| {
14-
// headers
15-
let mut headers = Headers::new();
16-
headers.add(Headers::CONTENT_TYPE, b"text/plain");
17-
headers.add("server", b"khttp");
18-
19-
// response
20-
res.ok(&headers, "Hello, World!")
21-
});
22-
2327
app.route(Get, "/json", |_ctx, res| {
24-
// headers
25-
let mut headers = Headers::new();
26-
headers.add(Headers::CONTENT_TYPE, b"application/json");
27-
headers.add("server", b"khttp");
28-
2928
// body
3029
let msg = HelloMessage {
3130
message: "Hello, World!",
@@ -34,7 +33,7 @@ fn main() {
3433
msg.to_bytes_mut(&mut buf);
3534

3635
// response
37-
res.ok(&headers, buf)
36+
res.ok(&JSON_HEADERS, buf)
3837
});
3938

4039
app.route(Get, "/fortunes", handle_fortunes);
@@ -63,11 +62,6 @@ fn handle_fortunes(_ctx: RequestContext, res: &mut ResponseHandle) -> io::Result
6362
// /fortunes query implementation using postgres (libpq)
6463
// ---------------------------------------------------------------------
6564

66-
use pq_sys::{
67-
ConnStatusType, ExecStatusType, PGconn, PQclear, PQconnectdb, PQerrorMessage, PQexecPrepared,
68-
PQfinish, PQgetlength, PQgetvalue, PQntuples, PQprepare, PQresultStatus, PQstatus,
69-
};
70-
7165
const DB_CONNINFO: &CStr = c"postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
7266
const PG_FORTUNES_SQL: &CStr = c"SELECT id, message FROM fortune";
7367
const PG_FORTUNES_PREPARED_STMT: &CStr = c"s_fortunes";
@@ -78,7 +72,7 @@ struct Fortune<'a> {
7872
message: &'a str,
7973
}
8074

81-
fn fetch_fortunes_html() -> Result<Vec<u8>, String> {
75+
fn fetch_fortunes_html() -> Result<Vec<u8>, &'static str> {
8276
PG_CONN.with(|pg| unsafe {
8377
let res = PQexecPrepared(
8478
pg.conn,
@@ -90,11 +84,11 @@ fn fetch_fortunes_html() -> Result<Vec<u8>, String> {
9084
1, // resultFormat = 1 (binary)
9185
);
9286
if res.is_null() {
93-
return Err("PQexecPrepared returned null".to_owned());
87+
return Err("PQexecPrepared returned null");
9488
}
9589
if PQresultStatus(res) != ExecStatusType::PGRES_TUPLES_OK {
9690
PQclear(res);
97-
return Err("PQexecPrepared non-ok result status".to_owned());
91+
return Err("PQexecPrepared non-ok result status");
9892
}
9993

10094
let rows = PQntuples(res);
@@ -164,7 +158,6 @@ impl PgConnection {
164158
PQfinish(conn);
165159
panic!("PQprepare returned null");
166160
}
167-
168161
let st = PQresultStatus(res);
169162
PQclear(res);
170163
if st != ExecStatusType::PGRES_COMMAND_OK {

0 commit comments

Comments
 (0)