aboutsummaryrefslogtreecommitdiff
path: root/src/test/Profiling.cpp
blob: 9767cb937388ac110d2016ff8d2047f3cc160e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <crepe/api/Asset.h>
#include <crepe/manager/Mediator.h>
#include <crepe/system/ParticleSystem.h>
#include <crepe/system/PhysicsSystem.h>
#include <crepe/system/RenderSystem.h>
#include <crepe/manager/ResourceManager.h>
#include <chrono>
#include <cmath>
#include <gtest/gtest.h>

#define private public
#define protected public

#include <crepe/api/Event.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/ParticleEmitter.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Script.h>
#include <crepe/api/Transform.h>
#include <crepe/manager/ComponentManager.h>
#include <crepe/manager/EventManager.h>
#include <crepe/system/CollisionSystem.h>
#include <crepe/system/ScriptSystem.h>
#include <crepe/types.h>
#include <crepe/util/Log.h>

using namespace std;
using namespace std::chrono_literals;
using namespace crepe;
using namespace testing;

class TestScript : public Script {
	bool oncollision(const CollisionEvent & test) {
		Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id);
		return true;
	}
	void init() {
		subscribe<CollisionEvent>(
			[this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
	}
	void update() {
		// Retrieve component from the same GameObject this script is on
	}
};

class DISABLED_ProfilingTest : public Test {
public:
	// Config for test
	// Minimum amount to let test pass
	const int min_gameobject_count = 100;
	// Maximum amount to stop test
	const int max_gameobject_count = 150;
	// Amount of times a test runs to calculate average
	const int average = 5;
	// Maximum duration to stop test
	const std::chrono::microseconds duration = 16000us;

	Mediator m;
	SDLContext sdl_context{m};
	ResourceManager resman{m};
	ComponentManager mgr{m};
	// Add system used for profling tests
	CollisionSystem collision_sys{m};
	PhysicsSystem physics_sys{m};
	ParticleSystem particle_sys{m};
	RenderSystem render_sys{m};
	ScriptSystem script_sys{m};

	// Test data
	std::map<std::string, std::chrono::microseconds> timings;
	int game_object_count = 0;
	std::chrono::microseconds total_time = 0us;

	void SetUp() override {

		GameObject do_not_use = mgr.new_object("DO_NOT_USE", "", {0, 0});
		do_not_use.add_component<Camera>(ivec2{1080, 720}, vec2{2000, 2000},
										 Camera::Data{
											 .bg_color = Color::WHITE,
											 .zoom = 1.0f,
										 });
		// initialize systems here:
		//calls init
		script_sys.update();
		//creates window
		render_sys.update();
	}

	// Helper function to time an update call and store its duration
	template <typename Func>
	std::chrono::microseconds time_function(const std::string & name, Func && func) {
		auto start = std::chrono::steady_clock::now();
		func();
		auto end = std::chrono::steady_clock::now();
		std::chrono::microseconds duration
			= std::chrono::duration_cast<std::chrono::microseconds>(end - start);
		timings[name] += duration;
		return duration;
	}

	// Run and profile all systems, return the total time in milliseconds
	std::chrono::microseconds run_all_systems() {
		std::chrono::microseconds total_microseconds = 0us;
		total_microseconds += time_function("PhysicsSystem", [&]() { physics_sys.update(); });
		total_microseconds
			+= time_function("CollisionSystem", [&]() { collision_sys.update(); });
		total_microseconds
			+= time_function("ParticleSystem", [&]() { particle_sys.update(); });
		total_microseconds += time_function("RenderSystem", [&]() { render_sys.update(); });
		return total_microseconds;
	}

	// Print timings of all functions
	void log_timings() const {
		std::string result = "\nFunction timings:\n";

		for (const auto & [name, duration] : timings) {
			result += name + " took " + std::to_string(duration.count() / 1000.0 / average)
					  + " ms (" + std::to_string(duration.count() / average) + " µs).\n";
		}

		result += "Total time: " + std::to_string(this->total_time.count() / 1000.0 / average)
				  + " ms (" + std::to_string(this->total_time.count() / average) + " µs)\n";

		result += "Amount of gameobjects: " + std::to_string(game_object_count) + "\n";

		GTEST_LOG_(INFO) << result;
	}

	void clear_timings() {
		for (auto & [key, value] : timings) {
			value = std::chrono::microseconds(0);
		}
	}
};

TEST_F(DISABLED_ProfilingTest, Profiling_1) {
	while (this->total_time / this->average < this->duration) {

		{
			//define gameobject used for testing
			GameObject gameobject = mgr.new_object("gameobject", "", {0, 0});
		}

		this->game_object_count++;

		this->total_time = 0us;
		clear_timings();

		for (int amount = 0; amount < this->average; amount++) {
			this->total_time += run_all_systems();
		}

		if (this->game_object_count >= this->max_gameobject_count) break;
	}
	log_timings();
	EXPECT_GE(this->game_object_count, this->min_gameobject_count);
}

TEST_F(DISABLED_ProfilingTest, Profiling_2) {
	while (this->total_time / this->average < this->duration) {

		{
			//define gameobject used for testing
			GameObject gameobject = mgr.new_object(
				"gameobject", "", {static_cast<float>(game_object_count * 2), 0});
			gameobject.add_component<Rigidbody>(Rigidbody::Data{
				.gravity_scale = 0.0,
				.body_type = Rigidbody::BodyType::STATIC,
			});
			gameobject.add_component<BoxCollider>(vec2{0, 0}, vec2{1, 1});

			gameobject.add_component<BehaviorScript>().set_script<TestScript>();
			Sprite & test_sprite = gameobject.add_component<Sprite>(
				Asset{"asset/texture/square.png"}, Sprite::Data{
						 .color = {0, 0, 0, 0},
						 .flip = {.flip_x = false, .flip_y = false},
						 .sorting_in_layer = 1,
						 .order_in_layer = 1,
						 .size = {.y = 500},
					 });
		}

		this->game_object_count++;

		this->total_time = 0us;
		clear_timings();
		for (int amount = 0; amount < this->average; amount++) {
			this->total_time += run_all_systems();
		}

		if (this->game_object_count >= this->max_gameobject_count) break;
	}
	log_timings();
	EXPECT_GE(this->game_object_count, this->min_gameobject_count);
}

TEST_F(DISABLED_ProfilingTest, Profiling_3) {
	while (this->total_time / this->average < this->duration) {

		{
			//define gameobject used for testing
			GameObject gameobject = mgr.new_object(
				"gameobject", "", {static_cast<float>(game_object_count * 2), 0});
			gameobject.add_component<Rigidbody>(Rigidbody::Data{
				.gravity_scale = 0,
				.body_type = Rigidbody::BodyType::STATIC,
			});
			gameobject.add_component<BoxCollider>(vec2{0, 0}, vec2{1, 1});
			gameobject.add_component<BehaviorScript>().set_script<TestScript>();
			Sprite & test_sprite = gameobject.add_component<Sprite>(
				Asset{"asset/texture/square.png"}, Sprite::Data{
						 .color = {0, 0, 0, 0},
						 .flip = {.flip_x = false, .flip_y = false},
						 .sorting_in_layer = 1,
						 .order_in_layer = 1,
						 .size = {.y = 500},
					 });
			auto & test = gameobject.add_component<ParticleEmitter>(ParticleEmitter::Data{
				.max_particles = 10,
				.emission_rate = 100,
				.end_lifespan = 100000,
				.boundary{
					.width = 1000,
					.height = 1000,
					.offset = vec2{0, 0},
					.reset_on_exit = false,
				},
				.sprite = test_sprite,
			});
		}
		render_sys.update();
		this->game_object_count++;

		this->total_time = 0us;
		clear_timings();
		for (int amount = 0; amount < this->average; amount++) {
			this->total_time += run_all_systems();
		}

		if (this->game_object_count >= this->max_gameobject_count) break;
	}
	log_timings();
	EXPECT_GE(this->game_object_count, this->min_gameobject_count);
}