Debugging using Time Measurement Tools
In your model, it is possible to measure time between two execution points on a same lifeline. This may assist you in debugging and identifying potential time-consuming issues.
LIBRARIES
You first need to use the chrono library from the standard library. The first step is to include it:
- Open the Model Explorer tab if it is not already open.
- Select the first package
<<ModelPackage, ListHint, Include>> <Model> MyModel
. - In the Properties section, find the C/C++ tab.

- In the Header text box, add the following at the end:
#include <chrono>

The chrono library will be included during the next code generation.
CREATE TIME POINT TYPE
Now, we need to create a new type in our model to handle certain variables:
- Open the type diagram

- Create a new Primitive Type, choose a name, and set the type as TEXT.

- Select the newly created Primitive Type, go to the Properties section, and C/C++ tab.

- Change the definition from
std::string
tostd::chrono::time_pointstd::chrono::system_clock
.

INSTRUMENTATION
To instrument the code with time measurement, we need three key components:
- A starting point from which the time will be measured. The variable carrying this information should be accessible across different activities, so we need a class-level variable on the lifeline using the newly created type. To create the variable:
- Right-click on a lifeline > Edit > Create Variable.

- Choose a name, use the newly created type, and initialize the variable to 0. Click OK.

-
Now, you can use the variable by initializing it at the desired location using the following line of code (in an activity for example):
start = std::chrono::system_clock::now();
-
A finishing point to measure the elapsed time from the starting point. This variable can be local and doesn't have to be created on the lifeline. We temporarily store the end time:
auto end = std::chrono::system_clock::now();
-
A display of the elapsed time, to be placed after retrieving the end point, for example:
std::cout << "Activity name, time (ms): " << std::chrono::duration_caststd::chrono::milliseconds(end - start).count() << std::endl;