Skip to content

Commit

Permalink
Start of benchmarking functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Jul 29, 2024
1 parent 3c0c92b commit 05ccabd
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 32 deletions.
1 change: 0 additions & 1 deletion Apps/GreenWorld/GreenWorldApp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "GreenWorldApp.hpp"
#include "GLRenderSettings.hpp"

namespace GW
{
Expand Down
4 changes: 3 additions & 1 deletion Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "LiquefiedApp.hpp"
#include "GlobalParams.hpp"
#include "ResourceManager.hpp"

namespace Liq
{
Expand Down Expand Up @@ -269,6 +268,9 @@ namespace Liq
if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_W) == GLFW_PRESS)
Engine::UIParams::wireframeRendering = !Engine::UIParams::wireframeRendering;

else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_B) == GLFW_PRESS)
Engine::UIParams::runBenchmark = !Engine::UIParams::runBenchmark;

else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_SPACE) == GLFW_PRESS)
Engine::LiquefiedParams::pauseSimulation = !Engine::LiquefiedParams::pauseSimulation;

Expand Down
12 changes: 12 additions & 0 deletions Apps/Liquefied/LiquefiedInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "LiquefiedInterface.hpp"
#include "GlobalParams.hpp"

namespace Liq
{
Expand All @@ -21,6 +22,9 @@ namespace Liq
ImGui::Separator();
ImGui::Text("Draw calls: %d", Engine::RenderStatistics::drawCalls);
ImGui::Text("Drawn vertices: %d", Engine::RenderStatistics::drawnVertices);
ImGui::Text("Simulation width: %d", Engine::LiquefiedParams::SIMULATION_WIDTH);
ImGui::Text("Simulation height: %d", Engine::LiquefiedParams::SIMULATION_HEIGHT);
ImGui::Text("Number of cells: %d", Engine::LiquefiedParams::SIMULATION_NUM_CELLS);
ImGui::Separator();

// --- Profiling/Timing-Results
Expand Down Expand Up @@ -157,6 +161,14 @@ namespace Liq
Engine::LiquefiedParams::resetSimulation = true;
}
AddVerticalBarDivider(500.0f);

ImGui::SetCursorPosY(10.0f);
ImGui::SetCursorPosX(520.0f);
if(ImGui::Button("Run Benchmark (B)"))
{
Engine::UIParams::runBenchmark = true;
}
AddVerticalBarDivider(685.0f);
}
ImGui::End();
}
Expand Down
7 changes: 4 additions & 3 deletions Engine/Application/GlobalParams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace Engine
inline static bool wireframeRendering = false;
inline static bool debugSprites = false;
inline static bool resetCamera = false;
inline static bool runBenchmark = false;

UIParams() = delete;
};
Expand Down Expand Up @@ -172,16 +173,16 @@ namespace Engine
{
inline static constexpr uint32 SIMULATION_WIDTH = 150;
inline static constexpr uint32 SIMULATION_HEIGHT = 100;
inline static constexpr uint32 LIQUID_NUM_CELLS = SIMULATION_WIDTH * SIMULATION_HEIGHT;
inline static constexpr uint32 SIMULATION_NUM_CELLS = SIMULATION_WIDTH * SIMULATION_HEIGHT;
inline static constexpr uint32 GAUSS_SEIDEL_ITERATIONS = 20;
inline static constexpr float GAUSS_SEIDEL_OVERRELAXATION = 1.9f;
inline static int32 turbinePower = 50;
inline static bool pauseSimulation = true;
inline static bool resetSimulation = false;
inline static bool activateDebugging = false;
inline static bool renderObjects = true;
inline static Integrator integratorChoice = Integrator::ForwardEuler;
inline static Visualization visualizationChoice = Visualization::Greyscale;
inline static Integrator integratorChoice = Integrator::RungeKutta2;
inline static Visualization visualizationChoice = Visualization::BlackBody;

LiquefiedParams() = delete;
};
Expand Down
64 changes: 64 additions & 0 deletions Engine/Core/Benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "Benchmark.hpp"

namespace Engine
{
// ----- Private -----

void Benchmark::End()
{
//If benchmark file doesn't exist, create it

//Append monitored values

Logger::Info("Logged", "File", _filename);
}

// ----- Public -----

void Benchmark::Start(const std::string& appName, const uint32 time_msec)
{
//Init variables
_isRunning = true;
_appName = appName;
_count = 0;
_avg = 0.0;
_min = FLT_MAX;
_max = FLT_MIN;
_timer = MakeScope<Timer>(time_msec);

Logger::Info("Started", "Benchmark", _appName);
}

void Benchmark::AddFrame(const double dt_msec)
{
_timer->Update(dt_msec);

//Check if timer has elapsed, else monitor values
if(_timer->CheckElapsedAndReset() == true)
{
//Calculate average, end benchmark and reset states
_avg /= (double)_count;
End();
_isRunning = false;
UIParams::runBenchmark = false;

Logger::Info("Finished", "Benchmark", _appName);
}
else
{
_count++;
_avg += dt_msec;

if(_min > dt_msec)
_min = dt_msec;

if(_max < dt_msec)
_max = dt_msec;
}
}

bool Benchmark::IsRunning()
{
return _isRunning;
}
}
31 changes: 31 additions & 0 deletions Engine/Core/Benchmark.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "Timer.hpp"
#include "Logger.hpp"
#include "GlobalParams.hpp"

#include <string>

namespace Engine
{
class Benchmark
{
private:
inline static constexpr const char* _filename = "Benchmark.md";

inline static bool _isRunning = bool();
inline static std::string _appName = std::string();
inline static uint64 _count = uint64();
inline static double _avg = double(), _min = double(), _max = double();
inline static Scope<Timer> _timer;

static void End();

public:
Benchmark() = delete;
static void Start(const std::string& appName, uint32 time_msec);
static void AddFrame(double dt_msec);

[[nodiscard]] static bool IsRunning();
};
}
6 changes: 3 additions & 3 deletions Engine/Core/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Engine

return string;
}

std::string FileManager::FileToString(const std::string& filepath)
{
std::ifstream file(filepath);
Expand All @@ -41,5 +41,5 @@ namespace Engine

return "FILE_ERROR";
}
}
}
}
}
4 changes: 2 additions & 2 deletions Engine/Core/Monitoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Engine
{
//Add to normal monitoring
buffer[_index] = {val, x, y};
_index = (_index + 1) % LiquefiedParams::LIQUID_NUM_CELLS;
_index = (_index + 1) % LiquefiedParams::SIMULATION_NUM_CELLS;

Log(name, val, x, y);
}
Expand Down Expand Up @@ -42,7 +42,7 @@ namespace Engine
void Monitoring::Reset()
{
_index = 0;
std::memset(&buffer[0], 0, LiquefiedParams::LIQUID_NUM_CELLS);
std::memset(&buffer[0], 0, LiquefiedParams::SIMULATION_NUM_CELLS);
_max = FLT_MIN;
_min = FLT_MAX;
loggedValues.clear();
Expand Down
2 changes: 1 addition & 1 deletion Engine/Core/Monitoring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Engine
inline static float _min = FLT_MAX;

public:
inline static LogFloatXY buffer[LiquefiedParams::LIQUID_NUM_CELLS] = {};
inline static LogFloatXY buffer[LiquefiedParams::SIMULATION_NUM_CELLS] = {};
inline static std::map<std::string, LogFloatXY> loggedValues = std::map<std::string, LogFloatXY>();

Monitoring() = delete;
Expand Down
34 changes: 24 additions & 10 deletions Engine/Core/Window.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Window.hpp"
#include "GlobalParams.hpp"

namespace Engine
{
Expand Down Expand Up @@ -79,23 +80,36 @@ namespace Engine

void Window::CalcFrametime()
{
//Calculate frametime
const double currentFrame = glfwGetTime();
_deltaTime_sec = currentFrame - _lastFrame;
_lastFrame = currentFrame;
//Get current time
const double currTime_sec = glfwGetTime();

//Accumulate to average the fps
//Calculate delta
_dt_sec = currTime_sec - _lastTime_sec;
_lastTime_sec = currTime_sec;

//Accumulate for averaging
_frameCounter++;
_dtAccumulated += _deltaTime_sec;
_dtAccumulated += _dt_sec;

if(_frameCounter > 160)
if(_frameCounter > 180)
{
_fpsAvg = 1 / (_dtAccumulated / _frameCounter);

//Reset
_frameCounter = 0;
_frameCounter = 0;
_dtAccumulated = 0.0f;
}

//Check and run benchmark for 10 seconds
if(UIParams::runBenchmark == true)
{
if(Benchmark::IsRunning() == false)
{
Benchmark::Start(_windowName, 10000);
}

Benchmark::AddFrame(_dt_sec * 1000);
}
}

void Window::PollEvents()
Expand Down Expand Up @@ -129,12 +143,12 @@ namespace Engine

double Window::GetDeltaTime_sec()
{
return _deltaTime_sec;
return _dt_sec;
}

double Window::GetDeltaTime_msec()
{
return _deltaTime_sec * 1000.0f;
return _dt_sec * 1000.0f;
}

double Window::GetFps()
Expand Down
7 changes: 4 additions & 3 deletions Engine/Core/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Types.hpp"
#include "GlobalParams.hpp"
#include "GLRenderSettings.hpp"
#include "Benchmark.hpp"

#include <string>

Expand All @@ -15,8 +16,8 @@ namespace Engine
private:
inline static GLFWwindow* _window = nullptr;
inline static std::string _windowName = std::string();
inline static double _deltaTime_sec = double();
inline static double _lastFrame = double();
inline static double _dt_sec = double();
inline static double _lastTime_sec = double();
inline static double _dtAccumulated = double();
inline static double _fpsAvg = double();
inline static uint32 _frameCounter = uint32();
Expand All @@ -39,4 +40,4 @@ namespace Engine
[[nodiscard]] static GLFWwindow* GetWindow();
[[nodiscard]] static float GetAspectRatio();
};
}
}
14 changes: 7 additions & 7 deletions Engine/Physics/FluidSimulation/StaggeredGrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ namespace Engine
{
private:
//Horizontal velocity (u-component) is sampled at the centers of the vertical cell faces.
float _u[LiquefiedParams::LIQUID_NUM_CELLS] = {0.0f};
float _u_tmp[LiquefiedParams::LIQUID_NUM_CELLS] = {0.0f};
float _u[LiquefiedParams::SIMULATION_NUM_CELLS] = {0.0f};
float _u_tmp[LiquefiedParams::SIMULATION_NUM_CELLS] = {0.0f};

//Vertical velocity (v-component) is sampled at the centers of the horizontal cell faces.
float _v[LiquefiedParams::LIQUID_NUM_CELLS] = {0.0f};
float _v_tmp[LiquefiedParams::LIQUID_NUM_CELLS] = {0.0f};
float _v[LiquefiedParams::SIMULATION_NUM_CELLS] = {0.0f};
float _v_tmp[LiquefiedParams::SIMULATION_NUM_CELLS] = {0.0f};

//b-component set to 0.0 for border cells.
float _b[LiquefiedParams::LIQUID_NUM_CELLS] = {0.0f};
float _b[LiquefiedParams::SIMULATION_NUM_CELLS] = {0.0f};

//Density value for visualization of the flow.
float _d[LiquefiedParams::LIQUID_NUM_CELLS] = {0.0f};
float _d_tmp[LiquefiedParams::LIQUID_NUM_CELLS] = {0.0f};
float _d[LiquefiedParams::SIMULATION_NUM_CELLS] = {0.0f};
float _d_tmp[LiquefiedParams::SIMULATION_NUM_CELLS] = {0.0f};

public:
const uint32 width = LiquefiedParams::SIMULATION_WIDTH;
Expand Down
1 change: 0 additions & 1 deletion Engine/Rendering/Renderer/GridRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "GridRenderer.hpp"
#include "Utility.hpp"

namespace Engine
{
Expand Down
1 change: 1 addition & 0 deletions Engine/Rendering/Renderer/GridRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "GLRenderSettings.hpp"
#include "GlobalParams.hpp"
#include "ResourceManager.hpp"
#include "Utility.hpp"

#include <vector>

Expand Down

0 comments on commit 05ccabd

Please sign in to comment.