Skip to content

Commit

Permalink
added a profiling method to the engine for quicker debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
PaoloMazzon committed Nov 23, 2023
1 parent 440825f commit 769266f
Show file tree
Hide file tree
Showing 6 changed files with 720 additions and 630 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.14)
project(Astro)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -ffast-math -funroll-loops")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -ffast-math -funroll-loops")

find_package(Vulkan)
find_package(SDL2 REQUIRED)
Expand Down
32 changes: 32 additions & 0 deletions docs/classes/Engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ aspects of game development.
+ [process_frame](#process_frame)
+ [argv](#argv)
+ [using_pak](#using_pak)
+ [profile_error_estimate](#profile_error_estimate)
+ [profile_fn](#profile_fn)

### switch_level
`static switch_level(level)`
Expand Down Expand Up @@ -138,3 +140,33 @@ Read Only: `static using_pak`

Variable Type: `Bool` - True if Astro is using a `game.pak` file.

### profile_error_estimate
`static profile_error_estimate(trials)`

Parameters
+ `trials -> Num` Number of trials to perform.

This method attempts to find the overhead associated with a for loop on the host
machine. It will run an empty for loop a given amount of times and return the average
time per iteration in microseconds.

### profile_fn
`static profile_fn(function, trials)`

Parameters
+ `function -> Fn` Function to profile, should require no parameters.
+ `trials -> Num` Number of trials to perform.

Profiles a given function by running it trials amount of times - more trials will produce
more reliable numbers. This method will return a list in the format

[total time in seconds, average time in microseconds, trial count]

Because `function` is called in a for loop, you may use
[profile_error_estimate](#profile_error_estimate) to estimate how much of the average time
is just the overhead associated with the for loop. Because how fast you are able to run
something is dependent on the host machine, the results are only useful in comparison to
other results and because this is being executed in a VM there is a little bit of error
associated with all of the results. Do not take these numbers as law, they are a decent
estimate that is useful to compare algorithms but it is not a completely reliable software
testing tool.
22 changes: 1 addition & 21 deletions examples/testing/data/game/Game.wren
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,6 @@ class Game is Level {
}
rng { _rng }

profile_begin(title, trials) {
_title_length = title.count
_trials = trials
System.print("--------------------%(title)--------------------")
_start_time = Engine.time
}

profile_end() {
var total_time = Engine.time - _start_time
var average = (total_time * 1000000) / _trials
System.print("Trial Count: %(_trials)")
System.print("Total Time: %(total_time)")
System.print("Average Time/Trial: %(average)µs")
var s = "----------------------------------------"
for (i in 0..(_title_length - 1)) {
s = s + "-"
}
System.print(s)
}

create() {
super.create()
_rng = Random.new()
Expand All @@ -95,7 +75,7 @@ class Game is Level {
}

update() {
super.update()
//super.update()

Renderer.draw_font(null, Engine.fps.toString, 0, 0)

Expand Down
22 changes: 22 additions & 0 deletions prog/lib/Engine.wren
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ class Engine {
// Returns whether or not the engine is running out of a game pak
foreign static using_pak

// Returns an error estimate for profiling in microseconds
static profile_error_estimate(trials) {
var start_time = Engine.time
for (i in 0..(trials - 1)) {}
var estimate = ((Engine.time - start_time) * 1000000) / trials
return estimate
}

// Profiles a given Wren function, returns [total time in seconds, average time in microseconds, trial count]
static profile_fn(function, trials) {
var start_time = 0
start_time = Engine.time

for (i in 0..(trials - 1)) {
function.call()
}

var total_time = Engine.time - start_time
var average = (total_time * 1000000) / trials
return [total_time, average, trials]
}

// For internal use
foreign static report_debug(entity_count)

Expand Down
2 changes: 1 addition & 1 deletion src/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define ASTRO_VERSION_MAJOR 0
#define ASTRO_VERSION_MINOR 7
#define ASTRO_VERSION_PATCH 2
#define ASTRO_VERSION_PATCH 4

// Engine configuration
typedef struct VKSK_EngineConfig {
Expand Down
Loading

0 comments on commit 769266f

Please sign in to comment.