Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions frameworks/Rust/khttp/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frameworks/Rust/khttp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2024"
khttp = { version = "0.2", features = ["epoll"] }
yarte = { version = "0.15", features = ["json"] }
pq-sys = "0.7"
mimalloc = "0.1"

[profile.release]
opt-level = 3
Expand Down
5 changes: 0 additions & 5 deletions frameworks/Rust/khttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Benchmark for [khttp](https://github.com/karlivory/khttp) - a low-level HTTP/1.1
### Test Type Implementation Source Code

* [JSON](./src/main.rs)
* [PLAINTEXT](./src/main.rs)
* [FORTUNES](./src/main.rs)

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

http://localhost:8080/json

### PLAINTEXT

http://localhost:8080/plaintext

### FORTUNES

http://localhost:8080/fortunes
1 change: 0 additions & 1 deletion frameworks/Rust/khttp/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{
"default": {
"json_url": "/json",
"plaintext_url": "/plaintext",
"fortune_url": "/fortunes",
"port": 8080,
"approach": "Realistic",
Expand Down
45 changes: 19 additions & 26 deletions frameworks/Rust/khttp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use khttp::{Headers, Method::*, RequestContext, ResponseHandle, Server, Status};
use std::{ffi::CStr, io, ptr};
use pq_sys::{
ConnStatusType, ExecStatusType, PGconn, PQclear, PQconnectdb, PQerrorMessage, PQexecPrepared,
PQfinish, PQgetlength, PQgetvalue, PQntuples, PQprepare, PQresultStatus, PQstatus,
};
use std::{ffi::CStr, io, ptr, sync::LazyLock};
use yarte::{Serialize, ywrite_html};

#[derive(Serialize)]
struct HelloMessage {
message: &'static str,
}

static JSON_HEADERS: LazyLock<Headers<'static>> = LazyLock::new(|| {
let mut headers = Headers::new();
headers.add(Headers::CONTENT_TYPE, b"application/json");
headers.add("server", b"khttp");
headers
});

fn main() {
let mut app = Server::builder("0.0.0.0:8080").unwrap();

app.route(Get, "/plaintext", |_ctx, res| {
// headers
let mut headers = Headers::new();
headers.add(Headers::CONTENT_TYPE, b"text/plain");
headers.add("server", b"khttp");

// response
res.ok(&headers, "Hello, World!")
});

app.route(Get, "/json", |_ctx, res| {
// headers
let mut headers = Headers::new();
headers.add(Headers::CONTENT_TYPE, b"application/json");
headers.add("server", b"khttp");

// body
let msg = HelloMessage {
message: "Hello, World!",
Expand All @@ -34,7 +33,7 @@ fn main() {
msg.to_bytes_mut(&mut buf);

// response
res.ok(&headers, buf)
res.ok(&JSON_HEADERS, buf)
});

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

use pq_sys::{
ConnStatusType, ExecStatusType, PGconn, PQclear, PQconnectdb, PQerrorMessage, PQexecPrepared,
PQfinish, PQgetlength, PQgetvalue, PQntuples, PQprepare, PQresultStatus, PQstatus,
};

const DB_CONNINFO: &CStr = c"postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
const PG_FORTUNES_SQL: &CStr = c"SELECT id, message FROM fortune";
const PG_FORTUNES_PREPARED_STMT: &CStr = c"s_fortunes";
Expand All @@ -78,7 +72,7 @@ struct Fortune<'a> {
message: &'a str,
}

fn fetch_fortunes_html() -> Result<Vec<u8>, String> {
fn fetch_fortunes_html() -> Result<Vec<u8>, &'static str> {
PG_CONN.with(|pg| unsafe {
let res = PQexecPrepared(
pg.conn,
Expand All @@ -90,11 +84,11 @@ fn fetch_fortunes_html() -> Result<Vec<u8>, String> {
1, // resultFormat = 1 (binary)
);
if res.is_null() {
return Err("PQexecPrepared returned null".to_owned());
return Err("PQexecPrepared returned null");
}
if PQresultStatus(res) != ExecStatusType::PGRES_TUPLES_OK {
PQclear(res);
return Err("PQexecPrepared non-ok result status".to_owned());
return Err("PQexecPrepared non-ok result status");
}

let rows = PQntuples(res);
Expand Down Expand Up @@ -164,7 +158,6 @@ impl PgConnection {
PQfinish(conn);
panic!("PQprepare returned null");
}

let st = PQresultStatus(res);
PQclear(res);
if st != ExecStatusType::PGRES_COMMAND_OK {
Expand Down
Loading