Unigine.Profiler Class
The Profiler class is used to create counters for the engine Performance Profiler. Allows using counters in your code in the following manner:
Profiler.get().begin("my_counter");
// ...code to profile...
Profiler.get().end();
Counters can be nested.
Usage Example
The following example contains different approaches to creating counters:
- Two counters are added via the setValue() function: one shows the number of dynamic mesh vertices, the other shows the update time. This approach should be used when you need to show, for example, a value of a setting, the number of objects, and so on.
- Another two counters are added by using the begin()/end() construction. They shows the time spent for mesh grid modifying and the time spent for mesh normal vectors, tangent vectors and a mesh bounding box calculation. This approach should be used when you need to show time spent for executing a part of the code.
In AppWorldLogic.cs, a dynamic mesh is created and then modified on the engine update. All counters are created on update() too.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// declare variables
int size = 128;
ObjectMeshDynamic mesh;
public override int init()
{
// create a dynamic mesh
mesh = new ObjectMeshDynamic(ObjectMeshDynamic.DYNAMIC_VERTEX | ObjectMeshDynamic.IMMUTABLE_INDICES);
// release script ownership
mesh.release();
// pass ownership to UnigineEditor
Editor.get().addNode(mesh.getNode());
// set the mesh settings
mesh.setWorldTransform(MathLib.translate(new Vec3(0.0f,0.0f,2.0f)));
mesh.setMaterial("mesh_base","*");
mesh.setProperty("surface_base","*");
// create dynamic mesh vertices
for(int y = 0; y < size; y++) {
for (int x = 0; x < size; x++)
{
mesh.addVertex(new vec3(0));
mesh.addTexCoord(new vec4((float)x / size,(float)y / size,0.0f,0.0f));
}
}
// create dynamic mesh indices
for(int y = 0; y < size - 1; y++) {
int offset = size * y;
for(int x = 0; x < size - 1; x++) {
mesh.addIndex(offset);
mesh.addIndex(offset + 1);
mesh.addIndex(offset + size);
mesh.addIndex(offset + size);
mesh.addIndex(offset + 1);
mesh.addIndex(offset + size + 1);
offset++;
}
}
return 1;
}
public override int update()
{
// initialize the graph color
float[] color = new float [4] {0.0f,0.0f,0.0f,1.0f};
// add a counter that shows time spent between the previous frame update and the current one
Profiler.get().setValue("Frame update time", "sec", Game.get().getFTime(), 1.0f, color);
float time = Game.get().getTime();
float isize = 30.0f / size;
// start the counter that shows the time spent for dymanic mesh grid modifying
Profiler.get().begin("Grid", new vec4(1.0f));
for(int y = 0; y < size; y++)
{
for (int i = 0; i < size; i++)
{
float Y = y * isize - 15.0f;
float Z = MathLib.cos(Y + time);
for (int x = 0; x < size; x++)
{
float X = x * isize - 15.0f;
mesh.setVertex(i++, new vec3(X, Y, Z * MathLib.sin(X + time)));
}
}
}
// stop the counter
Profiler.get().end();
// start the counter that shows the time spent for
// dynamic mesh normal vectors, tangent vectors and a mesh bounding box calculation
Profiler.get().begin("Mesh");
mesh.updateBounds();
mesh.updateTangents();
mesh.flushVertex();
// stop the counter
Profiler.get().end();
// add the counter that shows the number of dynamic mesh vertices
Profiler.get().setValue("Num vertices", "", mesh.getNumVertex(), 20000, null);
return 1;
}
}
}
Profiler Class
Members
Profiler get()
Return the existing Profiler instance.Profiler.get().isEnabled();
Return value
Profiler instance.void setEnabled(int enabled)
Enables or disables the profiler.Arguments
- int enabled - 1 to enable the profiler, 0 to disable it.
int isEnabled()
Returns a value indicating if the profiler is enabled.Return value
1 if the profiler is enabled; otherwise, 0.void setValue(string name, string units, int value, int max_value, float[] arg5)
Updates settings of the integer counter.// graph color
float[] color = new float [4] {1.0f,1.0f,1.0f,1.0f};
// add a counter without a graph
Profiler.get().setValue("Random value 1", "", new Random().Next(0,5), 4, null);
// add a counter with a colored graph
Profiler.get().setValue("Random value 2", "", new Random().Next(0,10), 9, color);
Arguments
- string name - Name of the counter.
- string units - Counter units.
- int value - Value of the counter.
- int max_value - Counter maximum value.
- float[] arg5 - Color of the graph. Pass NULL if no graph is required.
void setValue(string name, string units, float value, float max_value, float[] arg5)
Updates settings of the float counter.// graph color
float[] color = new float [4] {1.0f,1.0f,1.0f,1.0f};
float rvalue = (float)new Random().NextDouble() * 1;
// add a counter without a graph
Profiler.get().setValue("Random value 1", "", rvalue, 1.0f, null);
// add a counter with a colored graph
Profiler.get().setValue("Random value 2", "", 1 + rvalue, 10.0f, color);
Arguments
- string name - Name of the counter.
- string units - Counter units.
- float value - Value of the counter.
- float max_value - Counter maximum value.
- float[] arg5 - Color of the graph. Pass NULL if no graph is required.
float getValue(string name)
Returns a value of the specified counter.Arguments
- string name - The name of the counter.
Return value
Value of the counter in milliseconds.void begin(string name, vec4 color)
Starts a counter with a given name and shows a colored graph (if the show_profiler 2 console variable is set). The counter shows the user how many millisecods have been spent for the operation that is peformed between the begin() and the end() functions.int size = 128;
ObjectMeshDynamic mesh;
// ...
float time = Game.get().getTime();
float isize = 30.0f / size;
// start the counter that shows the time spent for dymanic mesh grid modifying
Profiler.get().begin("grid", new vec4(1.0f));
// modify a mesh grid
for(int y = 0; y < size; y++)
{
for (int i = 0; i < size; i++)
{
float Y = y * isize - 15.0f;
float Z = MathLib.cos(Y + time);
for (int x = 0; x < size; x++)
{
float X = x * isize - 15.0f;
mesh.setVertex(i++, new vec3(X, Y, Z * MathLib.sin(X + time)));
}
}
}
// stop the counter
Profiler.get().end();
Arguments
- string name - Name of the counter.
- vec4 color - Color of the graph.
void begin(string name)
Starts a counter with a given name. The counter shows the user how many millisecods have been spent for the operation that is peformed between the begin() and the end() functions.ObjectMeshDynamic mesh;
// ...
// start the counter that shows the time spent for
// dynamic mesh normal vectors, tangent vectors and a mesh bounding box calculation
Profiler.get().begin("mesh");
mesh.updateBounds();
mesh.updateTangents();
mesh.flushVertex();
// stop the counter
Profiler.get().end();
Arguments
- string name - Name of the counter.
float end()
Stops the last activated counter and returns its value.Return value
Value of the counter in milliseconds.string getMicroprofileUrl()
Returns the microprofile web server url.Return value
Microprofile web server url represented in the following way:http://localhost:p/, where p is the local port.
Gui getGui()
Returns a pointer to the GUI of the engine Performance Profiler.Return value
Pointer to the GUI.void setGui(Gui gui)
Sets the GUI for the engine Performance ProfilerArguments
- Gui gui - A pointer to the GUI class instance.
Last update: 2018-08-10
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)