From 929ed54e562d50eb1f58720f9522d8379456a76d Mon Sep 17 00:00:00 2001
From: Avi Weinstock <aweinstock314@gmail.com>
Date: Sat, 26 Jun 2021 20:10:58 -0400
Subject: [PATCH] Add some tracy plots about network usage to the client.

---
 client/Cargo.toml  |  1 +
 client/src/lib.rs  | 44 +++++++++++++++++++++++++++++++++++++++++++-
 voxygen/Cargo.toml |  2 +-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/client/Cargo.toml b/client/Cargo.toml
index 7e3877a9e8..c43090cecc 100644
--- a/client/Cargo.toml
+++ b/client/Cargo.toml
@@ -8,6 +8,7 @@ edition = "2018"
 simd = ["vek/platform_intrinsics"]
 plugins = ["common-state/plugins"]
 bin_bot = ["common-ecs", "serde", "ron", "clap", "rustyline", "common-frontend", "async-channel"]
+tracy = ["common-base/tracy"]
 
 default = ["simd"]
 
diff --git a/client/src/lib.rs b/client/src/lib.rs
index 66fb721872..32ef2f25bc 100644
--- a/client/src/lib.rs
+++ b/client/src/lib.rs
@@ -74,6 +74,16 @@ use tokio::runtime::Runtime;
 use tracing::{debug, error, trace, warn};
 use vek::*;
 
+#[cfg(feature = "tracy")]
+mod tracy_plots {
+    use common_base::tracy_client::{create_plot, Plot};
+    pub static TERRAIN_SENDS: Plot = create_plot!("terrain_sends");
+    pub static TERRAIN_RECVS: Plot = create_plot!("terrain_recvs");
+    pub static INGAME_SENDS: Plot = create_plot!("ingame_sends");
+    pub static INGAME_RECVS: Plot = create_plot!("ingame_recvs");
+}
+#[cfg(feature = "tracy")] use tracy_plots::*;
+
 const PING_ROLLING_AVERAGE_SECS: usize = 10;
 
 #[derive(Debug)]
@@ -782,6 +792,8 @@ impl Client {
             ClientMsg::Type(msg) => self.register_stream.send(msg),
             ClientMsg::Register(msg) => self.register_stream.send(msg),
             ClientMsg::General(msg) => {
+                #[cfg(feature = "tracy")]
+                let (mut ingame, mut terrain) = (0.0, 0.0);
                 let stream = match msg {
                     ClientGeneral::RequestCharacterList
                     | ClientGeneral::CreateCharacter { .. }
@@ -803,15 +815,30 @@ impl Client {
                     | ClientGeneral::UnlockSkillGroup(_)
                     | ClientGeneral::RequestPlayerPhysics { .. }
                     | ClientGeneral::RequestLossyTerrainCompression { .. } => {
+                        #[cfg(feature = "tracy")]
+                        {
+                            ingame = 1.0;
+                        }
                         &mut self.in_game_stream
                     },
                     //Only in game, terrain
-                    ClientGeneral::TerrainChunkRequest { .. } => &mut self.terrain_stream,
+                    ClientGeneral::TerrainChunkRequest { .. } => {
+                        #[cfg(feature = "tracy")]
+                        {
+                            terrain = 1.0;
+                        }
+                        &mut self.terrain_stream
+                    },
                     //Always possible
                     ClientGeneral::ChatMsg(_)
                     | ClientGeneral::Command(_, _)
                     | ClientGeneral::Terminate => &mut self.general_stream,
                 };
+                #[cfg(feature = "tracy")]
+                {
+                    INGAME_SENDS.point(ingame);
+                    TERRAIN_SENDS.point(terrain);
+                }
                 stream.send(msg)
             },
             ClientMsg::Ping(msg) => self.ping_stream.send(msg),
@@ -2067,6 +2094,8 @@ impl Client {
 
     fn handle_messages(&mut self, frontend_events: &mut Vec<Event>) -> Result<u64, Error> {
         let mut cnt = 0;
+        #[cfg(feature = "tracy")]
+        let (mut terrain_cnt, mut ingame_cnt) = (0, 0);
         loop {
             let cnt_start = cnt;
 
@@ -2084,14 +2113,27 @@ impl Client {
             }
             while let Some(msg) = self.in_game_stream.try_recv()? {
                 cnt += 1;
+                #[cfg(feature = "tracy")]
+                {
+                    ingame_cnt += 1;
+                }
                 self.handle_server_in_game_msg(frontend_events, msg)?;
             }
             while let Some(msg) = self.terrain_stream.try_recv()? {
                 cnt += 1;
+                #[cfg(feature = "tracy")]
+                {
+                    terrain_cnt += 1;
+                }
                 self.handle_server_terrain_msg(msg)?;
             }
 
             if cnt_start == cnt {
+                #[cfg(feature = "tracy")]
+                {
+                    TERRAIN_RECVS.point(terrain_cnt as f64);
+                    INGAME_RECVS.point(ingame_cnt as f64);
+                }
                 return Ok(cnt);
             }
         }
diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml
index 30e7e380bf..6de4bde135 100644
--- a/voxygen/Cargo.toml
+++ b/voxygen/Cargo.toml
@@ -25,7 +25,7 @@ buildInputs = ["xorg.libxcb"]
 hot-anim = ["anim/use-dyn-lib"]
 singleplayer = ["server"]
 simd = ["vek/platform_intrinsics"]
-tracy = ["profiling", "profiling/profile-with-tracy", "common-frontend/tracy"]
+tracy = ["profiling", "profiling/profile-with-tracy", "common-frontend/tracy", "client/tracy"]
 plugins = ["client/plugins"]
 
 # We don't ship egui with published release builds so a separate feature is required that excludes it.