diff --git a/crates/lib/http_server_common/src/lib.rs b/crates/lib/http_server_common/src/lib.rs index 3867702b0b..bb70593098 100644 --- a/crates/lib/http_server_common/src/lib.rs +++ b/crates/lib/http_server_common/src/lib.rs @@ -15,3 +15,4 @@ pub mod endpoints; pub mod error; pub mod request; pub mod response; +pub mod util; diff --git a/crates/lib/http_server_common/src/util/mod.rs b/crates/lib/http_server_common/src/util/mod.rs new file mode 100644 index 0000000000..44757340c1 --- /dev/null +++ b/crates/lib/http_server_common/src/util/mod.rs @@ -0,0 +1 @@ +pub mod timer; \ No newline at end of file diff --git a/crates/lib/http_server_common/src/util/timer.rs b/crates/lib/http_server_common/src/util/timer.rs new file mode 100644 index 0000000000..78d634ab34 --- /dev/null +++ b/crates/lib/http_server_common/src/util/timer.rs @@ -0,0 +1,8 @@ +use std::future::Future; + +pub async fn time_section(callback: F) -> T + where F: Fn() -> Fut, + Fut: Future, +{ + callback().await +} \ No newline at end of file diff --git a/crates/service/storyteller_web/src/http_server/endpoints/users/get_profile.rs b/crates/service/storyteller_web/src/http_server/endpoints/users/get_profile.rs index b838e2ed10..7340fa6604 100644 --- a/crates/service/storyteller_web/src/http_server/endpoints/users/get_profile.rs +++ b/crates/service/storyteller_web/src/http_server/endpoints/users/get_profile.rs @@ -23,6 +23,8 @@ use sqlx::error::Error::Database; use sqlx::mysql::MySqlDatabaseError; use std::fmt; use std::sync::Arc; +use time::Instant; +use http_server_common::util::timer::time_section; // TODO: This is duplicated in query_user_profile // TODO: This handler has embedded queries. @@ -126,19 +128,31 @@ impl fmt::Display for ProfileError { } } + +struct Timing { + +} + pub async fn get_profile_handler( http_request: HttpRequest, path: Path, server_state: web::Data>) -> Result { - let maybe_user_session = server_state - .session_checker - .maybe_get_user_session(&http_request, &server_state.mysql_pool) - .await - .map_err(|e| { - warn!("Session checker error: {:?}", e); - ProfileError::ServerError - })?; + let time_at_request_start = Instant::now(); + + let maybe_user_session = time_section(|| async { + server_state + .session_checker + .maybe_get_user_session(&http_request, &server_state.mysql_pool) + .await + .map_err(|e| { + warn!("Session checker error: {:?}", e); + ProfileError::ServerError + }) + } + ).await?; + + let time_after_session_check = Instant::now(); let mut is_mod = false; @@ -146,6 +160,8 @@ pub async fn get_profile_handler( is_mod = user_session.can_ban_users; } + let time_before_profile_query = Instant::now(); + // NB: Lookup failure is Err(RowNotFound). // NB: Since this is publicly exposed, we don't query sensitive data. let maybe_profile_record = sqlx::query_as!( @@ -184,6 +200,8 @@ WHERE .fetch_one(&server_state.mysql_pool) .await; // TODO: This will return error if it doesn't exist + let time_after_profile_query = Instant::now(); + let profile_record : RawUserProfileRecord = match maybe_profile_record { Ok(profile_record) => profile_record, Err(err) => { @@ -207,6 +225,8 @@ WHERE return Err(ProfileError::NotFound); } + let time_before_badges_list = Instant::now(); + let badges = list_user_badges(&server_state.mysql_pool, &profile_record.user_token) .await .unwrap_or_else(|err| { @@ -214,6 +234,8 @@ WHERE return Vec::new(); // NB: Fine if this fails. Not sure why it would. }); + let time_after_badges_list = Instant::now(); + let maybe_mod_fields = if is_mod { Some(UserProfileModeratorFields { is_banned, @@ -252,6 +274,8 @@ WHERE user: Some(profile_for_response), }; + let time_at_request_end = Instant::now(); + let body = serde_json::to_string(&response) .map_err(|e| ProfileError::ServerError)?;