Main Content

Automate C/C++ Code Profiling Using Polyspace Platform Projects

Using Polyspace® Test™, you can calculate C/C++ code profiling metrics, such as code coverage, execution time, and memory use:

  • The metric Code Coverage measures how much of your code is validated by the existing test cases. Measure the value of different code coverage metrics by using Polyspace Test and add appropriate test cases to achieve your desired level of coverage. Polyspace Test calculates code coverage for standard metrics. See Review Code Profiling Results.

  • The metric Execution Time computes the time required to execute the different callable entities of your code. See Execution Time.

  • The metric Memory Use measures how the callable entities use the available stack memory. See Memory Use.

The steps for calculating these metrics depend on your test authoring workflow:

  • If you create your tests graphically, you can:

    • Calculate these metrics in the Polyspace Platform user interface.

    • Automate the calculation by using the Polyspace Test command-line interface.

  • If you author your tests by using the Polyspace Test xUnit API, you can calculate these metrics by using your own toolchain and the polyspace-code-profiler command.

This example shows how to automate the calculation of code profile metrics by using the Polyspace Test command line interface.

Configure Project

To calculate the code profiling metrics using the Polyspace Test command line interface, you require a Polyspace Test project (.psprjx) file containing C/C++ source files and corresponding tests. To follow this example, open the Polyspace Platform user interface and create a new project.

After creating the project, find the file example.c in the folder <polyspaceroot>\polyspace\examples\doc_pstest\coverage_data_collection\src and add the source file to your project.

To calculate the code profiling metrics, you require at least one test case. Analyze the source files and add these test cases:

  • foo(2,0)==2

  • bar(1,1,0)==0

  • bar(1,1,1)==1

  • bar(1,0,1)==1

  • bar(0,1,1)==1

For details about creating a project, adding source files, and creating tests graphically, see Write C/C++ Unit Tests in Polyspace Platform User Interface.

Calculate Code Profiling Metrics

Once you configure your project, you can calculate the code coverage, execution time, or memory use metrics at the command line.

  1. Calculate the code profiling metric you want. You can calculate one of the three code profiling metrics at a time.

    • To calculate the code coverage programmatically, at the command line, enter:

      polyspace-test -run -project <project>.psprjx -results-dir <Results> -profiling-mode coverage

    • To calculate the execution time programmatically, enter:

      polyspace-test -run -project <project>.psprjx -results-dir <Results> -profiling-mode execution-profile

    • To calculate the memory use programmatically, enter:

      polyspace-test -run -project <project>.psprjx -results-dir <Results> -profiling-mode stack-profile

    Here:

    • <project>.psprjx is the Polyspace Test project that you want to run.

    • <Results> is the folder where Polyspace Test stores the results.

    • The option -profiling-mode specifies which metric to calculate:

      • coverage — Activates code coverage calculation

      • execution-profile — Activates execution time calculation

      • stack-profile — Activates memory use calculation

    Verify that a .psprof file is stored in the Results folder. This file contains the code profiling results.

  2. Review the results. You can review the results in the Polyspace Platform user interface or in an HTML report.

    • To review code profiling results using the Polyspace platform user interface, open the .psprof file using the Polyspace Platform user interface. For instance, review the code coverage of the five added tests:

      Code coverage results.

      For more information about reviewing code coverage results, see Review C/C++ Code Coverage Results in Polyspace Platform User Interface.

      For execution time and memory use results, Polyspace platform converts the results in the .psprof into an HTML report and opens the HTML in your default web browser.

    • You can convert the results in the .psprof file into an HTML report at the command-line. To create an HTML report, at the command line, enter:

      polyspace-test -report -html -report-dir <reportFolder> <Results>
      Verify that Polyspace Test creates the HTML report in <reportFolder>. For more information about reviewing the HTML report, see Structure of HTML Reports Generated from C/C++ Code Profiling Results.

      You can upload the results to Polyspace Access™ directly at the command line using the command polyspace-access.

See Also

| |

Topics