forked from TechEmpower/FrameworkBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,581 additions
and
814 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use sea_orm::entity::prelude::*; | ||
use serde::Serialize; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize)] | ||
#[sea_orm(table_name = "World")] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
|
||
#[sea_orm(column_name = "randomnumber")] | ||
pub random_number: i32, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter)] | ||
pub enum Relation {} | ||
|
||
impl RelationTrait for Relation { | ||
fn def(&self) -> RelationDef { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
mod application; | ||
mod db; | ||
mod routes; | ||
|
||
use application::application; | ||
use trillium::HttpConfig; | ||
|
||
fn main() { | ||
trillium_async_std::run(application()) | ||
#[cfg(debug_assertions)] | ||
env_logger::init(); | ||
|
||
let http_config = HttpConfig::default() | ||
.with_response_buffer_len(256) | ||
.with_request_buffer_initial_len(256) | ||
.with_response_header_initial_capacity(5); | ||
|
||
trillium_smol::config() | ||
.with_http_config(http_config) | ||
.run(application()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::db::{ | ||
cached_world::{Entity as CachedWorlds, Model as CachedWorld}, | ||
Db, DbConnExt, | ||
}; | ||
use futures_lite::StreamExt; | ||
use moka::future::Cache; | ||
use sea_orm::{DatabaseConnection, DbErr, EntityTrait}; | ||
use std::{iter, sync::Arc}; | ||
use trillium::{Conn, Handler, Info, Status}; | ||
use trillium_api::ApiConnExt; | ||
use trillium_router::RouterConnExt; | ||
use unicycle::FuturesUnordered; | ||
|
||
pub fn handler() -> CachedWorldHandler { | ||
CachedWorldHandler { | ||
cache: Cache::new(20_000), | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct CachedWorldHandler { | ||
cache: Cache<i32, CachedWorld>, | ||
} | ||
|
||
impl CachedWorldHandler { | ||
#[inline(always)] | ||
fn count_param(conn: &Conn) -> usize { | ||
conn.param("count") | ||
.and_then(|s| s.parse().ok()) | ||
.unwrap_or(1) | ||
.min(500) | ||
.max(1) | ||
} | ||
|
||
#[inline(always)] | ||
async fn fetch_cached( | ||
&self, | ||
db: &DatabaseConnection, | ||
id: i32, | ||
) -> Result<CachedWorld, Arc<DbErr>> { | ||
self.cache | ||
.try_get_with(id, async { | ||
CachedWorlds::find_by_id(id) | ||
.one(db) | ||
.await? | ||
.ok_or_else(|| DbErr::RecordNotFound(String::from("not found"))) | ||
}) | ||
.await | ||
} | ||
} | ||
|
||
#[trillium::async_trait] | ||
impl Handler for CachedWorldHandler { | ||
async fn init(&mut self, _info: &mut Info) { | ||
if self.cache.entry_count() == 0 { | ||
let db = Db::connection().await; | ||
let mut stream = CachedWorlds::find().stream(&db).await.unwrap(); | ||
while let Some(Ok(world)) = stream.next().await { | ||
self.cache.insert(world.id, world).await | ||
} | ||
self.cache.run_pending_tasks().await; | ||
} | ||
} | ||
|
||
async fn run(&self, conn: Conn) -> Conn { | ||
let count = Self::count_param(&conn); | ||
let db = conn.db(); | ||
let worlds: Result<Vec<_>, _> = | ||
iter::repeat_with(|| self.fetch_cached(db, fastrand::i32(1..=10000))) | ||
.take(count) | ||
.collect::<FuturesUnordered<_>>() | ||
.try_collect() | ||
.await; | ||
|
||
match worlds { | ||
Ok(worlds) => conn.with_json(&worlds), | ||
Err(_) => conn.with_status(Status::InternalServerError), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
use crate::db::{world::Entity as World, DbConnExt}; | ||
use sea_orm::entity::prelude::*; | ||
use trillium::{conn_unwrap, Conn}; | ||
use trillium::{Conn, Status}; | ||
use trillium_api::ApiConnExt; | ||
|
||
pub async fn handler(conn: Conn) -> Conn { | ||
let random = fastrand::i32(1..10000); | ||
let world = conn_unwrap!( | ||
World::find_by_id(random) | ||
.one(conn.db()) | ||
.await | ||
.ok() | ||
.flatten(), | ||
conn | ||
); | ||
|
||
conn.with_json(&world) | ||
let id = fastrand::i32(1..10000); | ||
match World::find_by_id(id).one(conn.db()).await { | ||
Ok(Some(world)) => conn.with_json(&world), | ||
Err(_) => conn.with_status(Status::InternalServerError), | ||
Ok(None) => conn.with_status(Status::NotFound), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
use serde_json::{json, to_string}; | ||
use trillium::{conn_try, Conn, KnownHeaderName}; | ||
use serde_json::json; | ||
use trillium::Conn; | ||
use trillium_api::ApiConnExt; | ||
|
||
pub async fn handler(conn: Conn) -> Conn { | ||
let body = conn_try!(to_string(&json!({"message": "Hello, World!"})), conn); | ||
conn.ok(body) | ||
.with_header(KnownHeaderName::ContentType, "application/json") | ||
conn.with_json(&json!({"message": "Hello, World!"})) | ||
} |
Oops, something went wrong.