Skip to content
Merged
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "octane"
version = "0.2.0"
version = "0.3.0"
authors = ["weegee <41485688+Daksh14@users.noreply.github.com>", "aplet123 <aplet@aplet.me>"]
description = "A web server which is express-like, intuitive and high-powered"
license-file = "LICENSE"
Expand Down Expand Up @@ -34,6 +34,8 @@ default = ["std", "faithful", "query_strings", "cookies", "url_variables", "raw_
std = ["tokio", "socket2", "octane_http/std"]
faithful = ["std"]
test_crate = ["std"]
# no effect: header names are always kept as they were sent. Kept so that
# feature lists naming it still build
raw_headers = ["std"]
query_strings = ["std"]
extended_queries = ["std"]
Expand Down
1 change: 1 addition & 0 deletions benchmarks/httparena/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
22 changes: 22 additions & 0 deletions benchmarks/httparena/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Octane's entry for HttpArena (https://github.com/MDA2AV/HttpArena), where
# this directory is submitted as frameworks/octane.
[package]
name = "httparena-octane"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
# keep-alive, chunked request bodies and async handlers are in octane 0.3
octane = { version = "0.3", features = ["rustls", "json"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[profile.release]
opt-level = 3
codegen-units = 1
lto = "thin"

# not part of the octane workspace while it lives in the octane repository
[workspace]
11 changes: 11 additions & 0 deletions benchmarks/httparena/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM rust:1.94 AS build
WORKDIR /app
COPY Cargo.toml .
RUN mkdir src && echo "fn main() {}" > src/main.rs && cargo build --release && rm -rf src/ target/release/httparena-octane* target/release/deps/httparena_octane*
COPY src ./src
RUN cargo build --release

FROM debian:bookworm-slim
COPY --from=build /app/target/release/httparena-octane /server
EXPOSE 8080 8081
CMD ["/server"]
55 changes: 55 additions & 0 deletions benchmarks/httparena/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# octane

[Octane](https://github.com/OctaneWeb/Octane) on the multi-threaded Tokio
runtime, default configuration. Octane parses HTTP/1.x itself rather than
sitting on hyper.

## Stack

- **Language:** Rust 1.94
- **Framework:** Octane 0.2
- **TLS:** rustls (ring)
- **Build:** Multi-stage, `debian:bookworm-slim` runtime

## Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/pipeline` | GET | Returns `ok` (plain text) |
| `/baseline11` | GET | Sums query parameter values |
| `/baseline11` | POST | Sums query parameters + request body |
| `/delay/{ms}` | GET | Waits `ms` milliseconds and returns the delay |
| `/json/{count}?m=N` | GET | First `count` dataset items with `total = price * quantity * m` |
| `/echo` | POST | Returns the request body back verbatim |

The same routes are served over TLS on port 8081 (`json-tls`, `8gbit`).

## Notes

- Routing and path parameters (`/delay/:ms`, `/json/:count`) through the octane
router, query parameters through `req.get_query()`
- Request bodies arrive whole in `req.body`, read by octane from either a
`Content-Length` or chunked framing
- JSON through `res.json`, serialized by serde_json per request
- The dataset is leaked once at startup so responses borrow it instead of
cloning
- Connections are persistent (octane's default 5 s idle keep-alive) and
pipelined requests are answered in order
- `max_connections` is raised from octane's default of 1,024 to 65,536, since
`async` holds 32,000 connections open and `baseline` 4,096
- `/delay/{ms}` is an async handler (`route_async_stop!`) waiting on
`tokio::time::sleep`, so a pending request holds a timer, not a worker
thread. Every other handler is synchronous and runs straight on the
connection's task
- json-tls and 8gbit share the TLS listener on 8081, which octane runs next to
the plaintext one and which advertises ALPN `http/1.1`. Without `/certs` it
logs a warning and only plaintext is served
- No `json-comp`: octane has no response compression, and standard mode
requires the framework's own
- Completeness: routes with path parameters are matched by the router;
`app.add` / `add_route` middleware runs before or after the
route handlers in registration order, app-wide or per path, and can stop
the chain; the request
carries path and query parameters, headers, cookies and the buffered body;
responses are declared with `res.status(..).set(..).send(..)`, `res.json`
and `res.send_file`
28 changes: 28 additions & 0 deletions benchmarks/httparena/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"display_name": "octane",
"language": "Rust",
"type": "emerging",
"mode": "standard",
"completeness": {
"routing": true,
"middleware": true,
"request": true,
"response": true
},
"engine": "octane",
"description": "Octane on the multi-threaded Tokio runtime with its own HTTP/1.x parser, default configuration. Routes and path parameters through the octane router, query parameters through req.get_query(), bodies read whole from req.body (Content-Length or chunked), JSON serialized per request with serde_json through res.json. TLS on :8081 through rustls (ring), ALPN http/1.1. /delay is an async handler waiting on tokio::time::sleep, the rest are synchronous handlers. max_connections raised to 65,536 for the held-connection profiles.",
"repo": "https://github.com/OctaneWeb/Octane",
"enabled": true,
"tests": [
"baseline",
"async",
"latency-1m",
"latency-10k",
"latency-500k-8cpu",
"pipelined",
"limited-conn",
"json-tls",
"8gbit"
],
"maintainers": ["Daksh14"]
}
191 changes: 191 additions & 0 deletions benchmarks/httparena/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//! Octane's entry for HttpArena: every endpoint goes through octane's router,
//! request and response API, on the multi-threaded tokio runtime.

use octane::prelude::*;
use octane::StatusCode;
use serde::{Deserialize, Serialize};
use octane::request::Query;
use std::error::Error;
use std::time::Duration;

#[derive(Deserialize)]
struct Rating {
score: i64,
count: i64,
}

#[derive(Deserialize)]
struct DatasetItem {
id: i64,
name: String,
category: String,
price: i64,
quantity: i64,
active: bool,
tags: Vec<String>,
rating: Rating,
}

#[derive(Serialize)]
struct RatingOut {
score: i64,
count: i64,
}

// Field order is the wire order: id..rating, then the computed total.
#[derive(Serialize)]
struct ProcessedItem<'a> {
id: i64,
name: &'a str,
category: &'a str,
price: i64,
quantity: i64,
active: bool,
tags: &'a [String],
rating: RatingOut,
total: i64,
}

#[derive(Serialize)]
struct JsonResponse<'a> {
items: Vec<ProcessedItem<'a>>,
count: usize,
}

fn load_dataset() -> Vec<DatasetItem> {
let path = std::env::var("DATASET_PATH").unwrap_or_else(|_| "/data/dataset.json".to_string());
match std::fs::read_to_string(path) {
Ok(data) => serde_json::from_str(&data).unwrap_or_default(),
Err(_) => Vec::new(),
}
}

// A query parameter as an integer, 0 when it is missing or isn't one
fn int(query: &Query, name: &str) -> i64 {
query
.get(name)
.and_then(|value| value.parse().ok())
.unwrap_or(0)
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Leaked once at startup so handlers borrow the items instead of cloning
// every string into each response.
let dataset: &'static [DatasetItem] = Box::leak(load_dataset().into_boxed_slice());

let mut app = Octane::new();
// The async profile holds 32,000 connections open at once, octane serves
// 1,024 at a time by default.
app.settings.max_connections = 65_536;

app.get(
"/pipeline",
route_stop!(|req, res| {
res.with_type("text/plain").send("ok");
}),
)?;

app.get(
"/baseline11",
route_stop!(|req, res| {
let query = req.get_query();
let sum = int(&query, "a") + int(&query, "b");
res.with_type("text/plain").send_owned(sum.to_string());
}),
)?;

// The body arrives whole, whether it was sent with a Content-Length or
// chunked.
app.post(
"/baseline11",
route_stop!(|req, res| {
let query = req.get_query();
let body = std::str::from_utf8(req.body)
.ok()
.and_then(|body| body.trim().parse::<i64>().ok())
.unwrap_or(0);
let sum = int(&query, "a") + int(&query, "b") + body;
res.with_type("text/plain").send_owned(sum.to_string());
}),
)?;

// An async handler: the request waits on a tokio timer, its worker thread
// goes on serving other connections.
app.get(
"/delay/:ms",
route_async_stop!(|req, res| {
match req.vars.get("ms").and_then(|ms| ms.parse::<u64>().ok()) {
Some(ms) => {
tokio::time::sleep(Duration::from_millis(ms)).await;
res.with_type("text/plain").send_owned(ms.to_string());
}
None => {
res.status(StatusCode::BadRequest)
.with_type("text/plain")
.send("bad delay");
}
}
}),
)?;

// The first `count` items with their totals, serialized per request.
app.get(
"/json/:count",
route_stop!(|req, res| {
match req.vars.get("count").and_then(|count| count.parse::<usize>().ok()) {
Some(count) => {
let m = req
.get_query()
.get("m")
.and_then(|m| m.parse::<i64>().ok())
.unwrap_or(1);
let count = count.min(dataset.len());
let items = dataset[..count]
.iter()
.map(|item| ProcessedItem {
id: item.id,
name: &item.name,
category: &item.category,
price: item.price,
quantity: item.quantity,
active: item.active,
tags: &item.tags,
rating: RatingOut {
score: item.rating.score,
count: item.rating.count,
},
total: item.price * item.quantity * m,
})
.collect();
if res.json(JsonResponse { items, count }).is_err() {
res.status(StatusCode::InternalServerError).send("");
}
}
None => {
res.status(StatusCode::BadRequest)
.with_type("text/plain")
.send("bad count");
}
}
}),
)?;

// The bytes that arrived, unchanged.
app.post(
"/echo",
route_stop!(|req, res| {
res.with_type("application/octet-stream").send(req.body);
}),
)?;

// json-tls and 8gbit: the same routes over HTTP/1.1 + TLS on 8081 with
// the certificate the harness mounts. Without it octane logs a warning
// and serves plaintext only.
let cert = std::env::var("TLS_CERT").unwrap_or_else(|_| "/certs/server.crt".to_string());
let key = std::env::var("TLS_KEY").unwrap_or_else(|_| "/certs/server.key".to_string());
app.ssl(8081).cert(&cert).key(&key);

app.listen(8080, || println!("octane listening on :8080, and on :8081 over TLS"))
.await
}
2 changes: 1 addition & 1 deletion guests/wasip3-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn routes() -> Result<Router, octane::wasip3::InvalidPathError> {
router.get(
"/hello/:name",
handler(async |req, res| {
let name = req.vars.get("name").copied().unwrap_or("there");
let name = req.vars.get("name").unwrap_or("there");
let _ = res
.send(
StatusCode::Ok,
Expand Down
Loading
Loading