Skip to main content
Engineering LibreTexts

16.3: Code Profiling in Pharo

  • Page ID
    43759
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The timeToRun method is useful to tell how long an expression takes to be executed. But it is not really adequate to understand how the execution time is distributed over the computation triggered by evaluating the expression. Pharo comes with MessageTally, a code profiler to precisely analyze the time distribution over a computation.

    MessageTally in Spy Results.
    Figure \(\PageIndex{1}\): MessageTally in action.

    MessageTally

    MessageTally is implemented as a unique class having the same name. Using it is quite simple. A message spyOn: needs to be sent to MessageTally with a block expression as argument to obtained a detailed execution analysis. Evaluating MessageTally spyOn: ["your expression here"] opens a window that contains the following information:

    1. a hierarchy list showing the methods executed with their associated execution time during the expression execution.
    2. leaf methods of the execution. A leaf method is a method that does not invoke other methods (e.g., primitive, accessors).
    3. statistic about the memory consumption and garbage collector involvement.

    Each of these points will be described later on.

    A TimeProfile.
    Figure \(\PageIndex{2}\): TimeProfiler uses MessageTally and navigates in executed methods.

    Figure \(\PageIndex{1}\) shows the result of the expression MessageTally spyOn: [20 timesRepeat: [Transcript show: 1000 factorial printString]]. The message spyOn: executes the provided block in a new process. The analysis focuses on one process, only, the one that executes the block to profile. The message spyAllOn: profiles all the processes that are active during the execution. This is useful to analyze the distribution of the computation over several processes.

    A tool a bit less crude than MessageTally is TimeProfileBrowser. It shows the implementation of the executed method in addition (Figure \(\PageIndex{2}\)). TimeProfileBrowser understand the message spyOn:. It means that in the below source code, MessageTally can be replaced with TimeProfileBrowser to obtain the better user interface.

    Integration in the programming environment

    As shown previously, the profiler may be directly invoked by sending spyOn: and spyAllOn: to the MessageTally class. It may be accessed through a number of additional ways.

    Via the World menu. The World menu (obtained by clicking outside any Pharo window) offers some profiling facilities under the System submenu (Figure \(\PageIndex{3}\)). Start profiling all Processes creates a block from a text selection and invokes spyAllOn:. The entry Start profiling UI profiles the user interface process. This is quite handy when debugging a user interface!

    Profiling options in the World menu.
    Figure \(\PageIndex{3}\): Access by the menu.

    Via the Test Runner. As the size of an application grows, unit tests are usually becoming a good candidate for code profiling. Running tests often is rather tedious when the time to run them is getting too long. The Test Runner in Pharo offers a button Run Profiled (Figure \(\PageIndex{4}\)).

    Pressing this button runs the selected unit tests and generates a message tally report.

    The RunProfiled button in the TestRunner.
    Figure \(\PageIndex{4}\): Button to generate a message Tally in the TestRunner.

    This page titled 16.3: Code Profiling in Pharo is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.