[BMX] A Function To Create Process Time Debug Console

Started by Filax, April 09, 2025, 07:44:53

Previous topic - Next topic

Filax

Hey Guys! :)
I very old of my code i use all the time when i create a blitzmax program! It allow to to know what is the function/process inside your code, take the most of time! For debugging... I hope you like it!? It's very simple, but very usefull!


The using is very simple :
Code: BASIC
Local MyTest01:DBG_Debug
MyTest01=DBG_Debug.Create("DEBUG01")

MainLoop

Cls
MyTest01.Start()
TheSuperFunctionEatingYourCPU()
MyTest01.Stop()

DBG_RefreshDBG()

Flip
EndLoop



Example:
Code: BASIC
' -----------------------------------------------
' Name : Function Execution Time Debugger
' Date : (C)2025
' Site : https://github.com/BlackCreepyCat
' -----------------------------------------------
Strict

Include "Inc_Debug.bmx"

Graphics 800, 600, 0, 0

Local MyTest01:DBG_Debug
MyTest01=DBG_Debug.Create("TEMPO01")

Local MyTest02:DBG_Debug
MyTest02=DBG_Debug.Create("TEMPO02")

Local MyTest03:DBG_Debug
MyTest03=DBG_Debug.Create("TEMPO03")

Local MyTest06:DBG_Debug
MyTest06=DBG_Debug.Create("TEMPO06")

Local MyTest07:DBG_Debug
MyTest07=DBG_Debug.Create("TEMPO07")

While Not KeyHit(KEY_ESCAPE)
	MyTest07.Start()
	Cls
	MyTest07.Stop()


	MyTest01.Start()
	Test1()
	MyTest01.Stop()


	MyTest02.Start()
	Test2()
	MyTest02.Stop()


	MyTest03.Start()
	DBG_RefreshDBG()
	MyTest03.Stop()
	
	
	MyTest06.Start()
	Flip(0)
	MyTest06.Stop()
Wend

DBG_Clear()

Function Test1()
	For Local I=1 To 150000
		Local T=Sin(I)
	Next
End Function

Function Test2()
	For Local I = 1 To 200000
		Local T=ATan(Cos(Sin(I)))
	Next
End Function


The Lib:
Code: BASIC
' -----------------------------------------------
' Name : Function Execution Time Debugger
' Date : (C)2025
' Site : https://github.com/BlackCreepyCat
' -----------------------------------------------
Global DBG_DebugList:TList = CreateList()
Global DBG_SecTimer:Int=MilliSecs()

' --------------------------
' Type of debug informations
' --------------------------
Type DBG_Debug
	Field Name:String
	Field TimerStart:Int
	Field TimeOperation:Int
	Field Percentage:Float	
	
	' ----------------
	' Create the debug
	' ----------------
	Function Create:DBG_Debug(Name:String)
		Local NewDebug:DBG_Debug = New DBG_Debug
		NewDebug.Name=Name
		
		ListAddLast DBG_DebugList,NewDebug	
		
		Return NewDebug
	End Function
	
	' ---------------
	' Start the debug
	' ---------------
	Method Start()
		TimerStart=MilliSecs()
	End Method
	
	' --------------
	' Stop the debug
	' --------------
	Method Stop()
		TimeOperation=TimeOperation+MilliSecs()-TimerStart
	End Method
	
	' -----------------
	' Destroy the debug
	' -----------------
	Method Destroy()
		DBG_DebugList.Remove( Self )
	End Method
End Type

' -------------------------
' Refresh the debug library
' -------------------------
Function DBG_RefreshDBG()
	While MilliSecs()-DBG_SecTimer>=1000
		Local temp:Float=MilliSecs()-DBG_SecTimer
		DBG_SecTimer=MilliSecs()
		
		For Local D:DBG_Debug = EachIn DBG_DebugList
			D.Percentage=D.TimeOperation*100/Temp
			D.TimeOperation=0
		Next
	Wend
	
	DBG_Display()
End Function

' -----------------------------
' Display the debug information
' -----------------------------
Function DBG_Display(Px:Int=0,Py:Int=0)
	Local Tmp_Py:Int
	
	Local Tmp_String:String
	Local Tmp_Total:Int
	
	For Local D:DBG_Debug = EachIn DBG_DebugList
		Tmp_String="Process name : "+D.Name+" / Take "+D.Percentage+"% time"
		
		DBG_DrawText(Tmp_String,Px,Py+Tmp_Py)
		
		SetAlpha 0.5
		SetColor 10,10,10
		DrawRect GraphicsWidth()-200,Py+Tmp_Py+1,200,TextHeight(Tmp_String)-2
		
		SetColor(50, Int(155 + D.Percentage), 50)
		DrawRect GraphicsWidth()-(D.Percentage*2),Py+Tmp_Py+1,(D.Percentage*2),TextHeight(Tmp_String)-2
				
		Tmp_Py=Tmp_Py+TextHeight(Tmp_String)+1
		Tmp_Total=Tmp_Total+D.Percentage
	Next

	DBG_DrawText("The operations take a total of : "+Tmp_Total+"% of a cycle",Px,Py+Tmp_Py)
End Function

' ----------------------------
' Display text with background
' ----------------------------
Function DBG_DrawText(Caption:String,Px:Int,Py:Int)
	Local Tmp_Width:Int=GraphicsWidth() 'TextWidth(Caption)+10
	Local Tmp_Height:Int=TextHeight(Caption)
		
	SetBlend Alphablend
	SetAlpha 0.2
		
	SetColor 80,200,255
	DrawRect Px,Py,Tmp_Width,Tmp_Height	

	SetColor 0,64,128
	DrawRect Px+1,Py+1,Tmp_Width-2,Tmp_Height-2	
	
	SetAlpha 1		
	SetColor 10,20,30
	DrawText Caption,Px+6,Py+2	
	
	SetColor 0,200,255
	DrawText Caption,Px+5,Py+1	
End Function

' -----------------------
' Clear all debug process
' -----------------------
Function DBG_Clear()
	For Local D:DBG_Debug = EachIn DBG_DebugList
		D.Destroy()
	Next
End Function



Derron

For profiling your application you can use tools (gprof etc) - sometimes it can be helpful too. 

Nonetheless I have something in your lines written too -- a "call profiler" (counting how often and how long my run certain functions)
https://github.com/TVTower/TVTower/blob/master/source/Dig/base.util.profiler.bmx

Maybe this gives you some idea how to extend your code.


bye
Ron

Filax

Thanks of this Github, i'll explore it! :) I writed this sh..t a long time ago. To check some multiple fractals mandelbrot rendering.
It can be usefull and graphical! :)  

PixelOutlaw

#3

Edit: Just saw Derron's mention of gprof too further up.


Not to detract from your hacking (It's an interesting idea) ...

It's worth noting that BltizMax already supports high precision profiling.
To do this just check GProf Profiling in MaxIDE.
You can then use gprof and run the program to get highly accurate profiling information.
Ubuntu MATE 20.04: i5-3570K CPU @ 3.40GHz, 8GB RAM, GeForce GTX 1060 3GB

One DEFUN to rule them all, One DEFUN to find them, One DEFUN to RETURN them all, and in the darkness MULTIPLE-VALUE-BIND them.

Filax

Thanks for those informations! I'll explore this way :)

PixelOutlaw

Your syntax might change slightly for gprof.
I'm using the Bash shell to call it on Linux. It might be different on Mac or Windows...
Ubuntu MATE 20.04: i5-3570K CPU @ 3.40GHz, 8GB RAM, GeForce GTX 1060 3GB

One DEFUN to rule them all, One DEFUN to find them, One DEFUN to RETURN them all, and in the darkness MULTIPLE-VALUE-BIND them.