added useless benchmark and improved deserialisation logic

This commit is contained in:
TotallyNot 2022-09-08 19:32:51 +02:00
parent 2390da4e54
commit 306db74d85
6 changed files with 197 additions and 62 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "torn-api" name = "torn-api"
version = "0.3.2" version = "0.4.0"
edition = "2021" edition = "2021"
authors = ["Pyrit [2111649]"] authors = ["Pyrit [2111649]"]
license = "MIT" license = "MIT"
@ -8,6 +8,10 @@ repository = "https://github.com/TotallyNot/torn-api.rs.git"
homepage = "https://github.com/TotallyNot/torn-api.rs.git" homepage = "https://github.com/TotallyNot/torn-api.rs.git"
description = "Torn API bindings for rust" description = "Torn API bindings for rust"
[[bench]]
name = "deserialisation_benchmark"
harness = false
[features] [features]
default = [ "reqwest" ] default = [ "reqwest" ]
reqwest = [ "dep:reqwest" ] reqwest = [ "dep:reqwest" ]
@ -33,3 +37,4 @@ tokio = { version = "1.20.1", features = ["test-util", "rt", "macros"] }
tokio-test = "0.4.2" tokio-test = "0.4.2"
reqwest = { version = "0.11", default-features = true } reqwest = { version = "0.11", default-features = true }
awc = { version = "3", features = [ "rustls" ] } awc = { version = "3", features = [ "rustls" ] }
criterion = "0.3"

View file

@ -0,0 +1,65 @@
use criterion::{criterion_group, criterion_main, Criterion};
use torn_api::{faction, user, ThreadSafeApiClient};
pub fn user_benchmark(c: &mut Criterion) {
dotenv::dotenv().unwrap();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.unwrap();
let response = rt.block_on(async {
let key = std::env::var("APIKEY").expect("api key");
let client = reqwest::Client::default();
client
.torn_api(key)
.user(|b| {
b.selections(&[
user::Selection::Basic,
user::Selection::Discord,
user::Selection::Profile,
user::Selection::PersonalStats,
])
})
.await
.unwrap()
});
c.bench_function("user deserialize", |b| {
b.iter(|| {
response.basic().unwrap();
response.discord().unwrap();
response.profile().unwrap();
response.personal_stats().unwrap();
})
});
}
pub fn faction_benchmark(c: &mut Criterion) {
dotenv::dotenv().unwrap();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.unwrap();
let response = rt.block_on(async {
let key = std::env::var("APIKEY").expect("api key");
let client = reqwest::Client::default();
client
.torn_api(key)
.faction(|b| b.selections(&[faction::Selection::Basic]))
.await
.unwrap()
});
c.bench_function("user deserialize", |b| {
b.iter(|| {
response.basic().unwrap();
})
});
}
criterion_group!(benches, user_benchmark, faction_benchmark);
criterion_main!(benches);

View file

@ -1,5 +1,4 @@
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use num_traits::{PrimInt, Zero};
use serde::de::{Deserialize, Deserializer, Error, Unexpected}; use serde::de::{Deserialize, Deserializer, Error, Unexpected};
pub fn empty_string_is_none<'de, D>(deserializer: D) -> Result<Option<String>, D::Error> pub fn empty_string_is_none<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
@ -14,13 +13,18 @@ where
} }
} }
pub fn string_is_long<'de, D>(deserializer: D) -> Result<i64, D::Error> pub fn string_is_long<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let s = String::deserialize(deserializer)?; let s = String::deserialize(deserializer)?;
s.parse() if s.is_empty() {
.map_err(|_e| Error::invalid_type(Unexpected::Str(&s), &"i64")) Ok(None)
} else {
s.parse()
.map(Some)
.map_err(|_e| Error::invalid_type(Unexpected::Str(&s), &"i64"))
}
} }
pub fn zero_date_is_none<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error> pub fn zero_date_is_none<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error>
@ -35,28 +39,3 @@ where
Ok(Some(DateTime::from_utc(naive, Utc))) Ok(Some(DateTime::from_utc(naive, Utc)))
} }
} }
pub fn zero_is_none<'de, D, I>(deserializer: D) -> Result<Option<I>, D::Error>
where
D: Deserializer<'de>,
I: PrimInt + Zero + Deserialize<'de>,
{
let i = I::deserialize(deserializer)?;
if i == I::zero() {
Ok(None)
} else {
Ok(Some(i))
}
}
pub fn none_is_none<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
if s == "None" {
Ok(None)
} else {
Ok(Some(s))
}
}

View file

@ -13,6 +13,7 @@ mod de_util;
use async_trait::async_trait; use async_trait::async_trait;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use num_traits::{AsPrimitive, PrimInt};
use serde::de::{DeserializeOwned, Error as DeError}; use serde::de::{DeserializeOwned, Error as DeError};
use thiserror::Error; use thiserror::Error;
@ -363,8 +364,11 @@ where
} }
#[must_use] #[must_use]
pub fn id(mut self, id: u64) -> Self { pub fn id<I>(mut self, id: I) -> Self
self.request.id = Some(id); where
I: PrimInt + AsPrimitive<u64>,
{
self.request.id = Some(id.as_());
self self
} }

View file

@ -34,19 +34,98 @@ pub struct LastAction {
pub timestamp: DateTime<Utc>, pub timestamp: DateTime<Utc>,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone)]
pub struct Faction { pub struct Faction {
#[serde(deserialize_with = "de_util::zero_is_none")] pub faction_id: i32,
pub faction_id: Option<i32>, pub faction_name: String,
#[serde(deserialize_with = "de_util::none_is_none")] pub days_in_faction: i16,
pub faction_name: Option<String>, pub position: String,
#[serde(deserialize_with = "de_util::zero_is_none")]
pub days_in_faction: Option<i16>,
#[serde(deserialize_with = "de_util::none_is_none")]
pub position: Option<String>,
pub faction_tag: Option<String>, pub faction_tag: Option<String>,
} }
fn deserialize_faction<'de, D>(deserializer: D) -> Result<Option<Faction>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum Field {
FactionId,
FactionName,
DaysInFaction,
Position,
FactionTag,
}
struct FactionVisitor;
impl<'de> Visitor<'de> for FactionVisitor {
type Value = Option<Faction>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("struct Faction")
}
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
where
V: MapAccess<'de>,
{
let mut faction_id = None;
let mut faction_name = None;
let mut days_in_faction = None;
let mut position = None;
let mut faction_tag = None;
while let Some(key) = map.next_key()? {
match key {
Field::FactionId => {
faction_id = Some(map.next_value()?);
}
Field::FactionName => {
faction_name = Some(map.next_value()?);
}
Field::DaysInFaction => {
days_in_faction = Some(map.next_value()?);
}
Field::Position => {
position = Some(map.next_value()?);
}
Field::FactionTag => {
faction_tag = map.next_value()?;
}
}
}
let faction_id = faction_id.ok_or_else(|| de::Error::missing_field("faction_id"))?;
let faction_name =
faction_name.ok_or_else(|| de::Error::missing_field("faction_name"))?;
let days_in_faction =
days_in_faction.ok_or_else(|| de::Error::missing_field("days_in_faction"))?;
let position = position.ok_or_else(|| de::Error::missing_field("position"))?;
if faction_id == 0 {
Ok(None)
} else {
Ok(Some(Faction {
faction_id,
faction_name,
days_in_faction,
position,
faction_tag,
}))
}
}
}
const FIELDS: &[&str] = &[
"faction_id",
"faction_name",
"days_in_faction",
"position",
"faction_tag",
];
deserializer.deserialize_struct("Faction", FIELDS, FactionVisitor)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
pub enum State { pub enum State {
Okay, Okay,
@ -92,7 +171,7 @@ pub struct Discord {
#[serde(rename = "userID")] #[serde(rename = "userID")]
pub user_id: i32, pub user_id: i32,
#[serde(rename = "discordID", deserialize_with = "de_util::string_is_long")] #[serde(rename = "discordID", deserialize_with = "de_util::string_is_long")]
pub discord_id: i64, pub discord_id: Option<i64>,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@ -160,10 +239,10 @@ where
where where
V: MapAccess<'de>, V: MapAccess<'de>,
{ {
let mut team: Option<EliminationTeam> = None; let mut team = None;
let mut score = None; let mut score = None;
let mut attacks = None; let mut attacks = None;
let mut name: Option<CompetitionName> = None; let mut name = None;
while let Some(key) = map.next_key()? { while let Some(key) = map.next_key()? {
match key { match key {
@ -217,21 +296,27 @@ where
} }
} }
match (name, team, score, attacks) { let name = name.ok_or_else(|| de::Error::missing_field("name"))?;
(Some(CompetitionName::Elimination), Some(team), Some(score), Some(attacks)) => {
Ok(Some(Competition::Elimination { match name {
team, CompetitionName::Elimination => {
score, if let Some(team) = team {
attacks, let score = score.ok_or_else(|| de::Error::missing_field("score"))?;
})) let attacks = attacks.ok_or_else(|| de::Error::missing_field("attacks"))?;
Ok(Some(Competition::Elimination {
team,
score,
attacks,
}))
} else {
Ok(None)
}
} }
_ => Ok(None),
} }
} }
} }
const FIELDS: &[&str] = &["name", "score", "team", "attacks"]; deserializer.deserialize_map(CompetitionVisitor)
deserializer.deserialize_struct("Competition", FIELDS, CompetitionVisitor)
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@ -245,7 +330,8 @@ pub struct Profile {
pub life: LifeBar, pub life: LifeBar,
pub last_action: LastAction, pub last_action: LastAction,
pub faction: Faction, #[serde(deserialize_with = "deserialize_faction")]
pub faction: Option<Faction>,
pub status: Status, pub status: Status,
#[serde(deserialize_with = "deserialize_comp")] #[serde(deserialize_with = "deserialize_comp")]
@ -325,11 +411,7 @@ mod tests {
let faction = response.profile().unwrap().faction; let faction = response.profile().unwrap().faction;
assert!(faction.faction_id.is_none()); assert!(faction.is_none());
assert!(faction.faction_name.is_none());
assert!(faction.faction_tag.is_none());
assert!(faction.days_in_faction.is_none());
assert!(faction.position.is_none());
} }
#[async_test] #[async_test]

View file

@ -1,6 +1,6 @@
[package] [package]
name = "torn-key-pool" name = "torn-key-pool"
version = "0.2.1" version = "0.3.0"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/TotallyNot/torn-api.rs.git" repository = "https://github.com/TotallyNot/torn-api.rs.git"
@ -16,7 +16,7 @@ reqwest = [ "dep:reqwest", "torn-api/reqwest" ]
awc = [ "dep:awc", "torn-api/awc" ] awc = [ "dep:awc", "torn-api/awc" ]
[dependencies] [dependencies]
torn-api = { path = "../torn-api", default-features = false, version = "0.3" } torn-api = { path = "../torn-api", default-features = false, version = "0.4" }
async-trait = "0.1" async-trait = "0.1"
thiserror = "1" thiserror = "1"