<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adrien Cacciaguerra</title>
    <description>The latest articles on DEV Community by Adrien Cacciaguerra (@adriencaccia).</description>
    <link>https://dev.to/adriencaccia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F744079%2Fc4255665-3c55-4ef7-9c61-ec29d03d1403.jpeg</url>
      <title>DEV Community: Adrien Cacciaguerra</title>
      <link>https://dev.to/adriencaccia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adriencaccia"/>
    <language>en</language>
    <item>
      <title>Pinpoint performance regressions with CI-Integrated differential profiling</title>
      <dc:creator>Adrien Cacciaguerra</dc:creator>
      <pubDate>Mon, 23 Oct 2023 13:27:17 +0000</pubDate>
      <link>https://dev.to/codspeed/pinpoint-performance-regressions-with-ci-integrated-differential-profiling-546k</link>
      <guid>https://dev.to/codspeed/pinpoint-performance-regressions-with-ci-integrated-differential-profiling-546k</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Checkout the &lt;a href="https://codspeed.io/blog/pinpoint-performance-regressions-with-ci-integrated-differential-profiling"&gt;original post on CodSpeed&lt;/a&gt; to use interactive flame graphs components 📊&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Flame Graphs are a visualization tool that helps you understand how your software is performing. They display the call stack of a program and the time spent in each function. They are a powerful tool to quickly identify performance bottlenecks.&lt;/p&gt;

&lt;p&gt;Differential Flame Graphs combine two flame graphs to highlight the differences between them. They allow you to easily spot performance regressions and improvements and gain invaluable insights into your software's performance.&lt;/p&gt;

&lt;p&gt;Below you can find an example of a differential flame graph following a change in a codebase. At a glance, we can understand where the code has become slower. Here it is the &lt;code&gt;parse_issue_fixed&lt;/code&gt; function as it has the brightest red color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6Q4LDvUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7gekm2awrtb6ou3rh4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Q4LDvUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7gekm2awrtb6ou3rh4e.png" width="800" height="190"&gt;&lt;/a&gt;&lt;br&gt;Flame Graphs component taken from the CodSpeed app.
  &lt;/p&gt;

&lt;p&gt;Let's explore how to read flame graphs and how CodSpeed automates flame graph generation in your CI pipeline.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CodSpeed supports Rust, Node.js, Python and generates flame graphs out of the box. More languages are coming soon!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Spotting a performance regression
&lt;/h2&gt;

&lt;p&gt;Let's dive into the previous example.&lt;/p&gt;

&lt;p&gt;We have a function &lt;code&gt;parse_issue_fixed&lt;/code&gt; that parses a GitHub pull request body and extracts the issue number that it fixes. Given &lt;code&gt;body = "fixes #123"&lt;/code&gt;, the function returns &lt;code&gt;123&lt;/code&gt;. Here &lt;code&gt;body&lt;/code&gt; can be a multiline string, and the issue number can be anywhere in the string.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;On GitHub, adding the string &lt;code&gt;fixes #123&lt;/code&gt; in a pull request body will automatically close the issue #123 when the pull request is merged.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We notice that the code is quite long and not easily understandable. We refactor it to use a regular expression instead. This gives us the following diff:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+import re
&lt;/span&gt;
+FIXES_REGEX = re.compile(r"fixes #(\d+)")
&lt;span class="gi"&gt;+
+
&lt;/span&gt; def parse_issue_fixed(body: str) -&amp;gt; int | None:
&lt;span class="gd"&gt;-    prefix = "fixes #"
-    index = body.find(prefix)
-    if index == -1:
-        return None
-
-    start = index + len(prefix)
-    end = start
-    while end &amp;lt; len(body) and body[end].isdigit():
-        end += 1
-    return int(body[start:end])
&lt;/span&gt;&lt;span class="gi"&gt;+    match = FIXES_REGEX.search(body)
+    return int(match.group(1)) if match else None
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, we have a much shorter and more readable function. But did we introduce a performance regression?&lt;/p&gt;

&lt;p&gt;We already have a benchmark in place for the function &lt;code&gt;parse_pr&lt;/code&gt;, a higher-level function that parses a GitHub pull request body and calls multiple functions to retrieve information, including &lt;code&gt;parse_issue_fixed&lt;/code&gt;. The input is a multiline string of approximately 50kB. We chose a large input to better understand the performance characteristics of the function and make sure the function performs well when parsing large pull request bodies.&lt;/p&gt;

&lt;p&gt;Let's check out the flame graphs for the benchmark to analyze the performance impact of our change. The &lt;strong&gt;Base&lt;/strong&gt; flame graph is the one before the change, the &lt;strong&gt;Head&lt;/strong&gt; flame graph is the one after the change, and finally, the &lt;strong&gt;Diff&lt;/strong&gt; flame graph is the difference between the two.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6witFCla--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxx3gv9ldcevr5tvge6n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6witFCla--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxx3gv9ldcevr5tvge6n.png" width="800" height="315"&gt;&lt;/a&gt;&lt;br&gt;Flame Graphs component taken from the CodSpeed app.
  &lt;/p&gt;

&lt;p&gt;Here we can see that the &lt;code&gt;parse_issue_fixed&lt;/code&gt; function is bright red, thus denoting that it was slower after the change and had the biggest performance impact on the benchmark. When hovering over the &lt;code&gt;parse_issue_fixed&lt;/code&gt; frame, we can see that the performance impact is a &lt;em&gt;-21.5% regression&lt;/em&gt; on the overall benchmark.&lt;/p&gt;

&lt;p&gt;So in our use case, using a regular expression will result in slower executions compared to using &lt;code&gt;str.find&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So yes, our change for the sake of readability introduced a performance regression. We can now make an informed decision on whether we want to keep the change or not.&lt;/p&gt;

&lt;p&gt;Now that we understand how a differential flame graph works and how useful it is, let's explore how to generate one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating flame graphs manually is unreliable and time-consuming
&lt;/h2&gt;

&lt;p&gt;The first thing to note is that manually generated flame graphs on a dev machine are &lt;strong&gt;not consistent&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Running the same script multiple times with the same input will result in different flame graphs. Indeed, other processes running on the machine will impact the execution time of the script and its different functions. This make it hard to spot performance regressions with confidence.&lt;/p&gt;

&lt;p&gt;The second thing to note is that generating flame graphs manually is &lt;strong&gt;tedious&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The first tool to generate flame graphs is the eponym FlameGraph by Brendan Gregg written in Perl. Since then, many other tools have been created in and for other languages. The general steps are the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run your script with a profiler and output the profile in a file&lt;/li&gt;
&lt;li&gt;Run the tool on the profile to generate a flame graph svg&lt;/li&gt;
&lt;li&gt;Open the generated svg in your browser and explore the data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is already a quite heavy process: you have to create a script to isolate the part of your program you want to generate a flame graph for, then run a bunch of commands and finally open a file in your browser.&lt;/p&gt;

&lt;p&gt;This is why we decided to automate the generation of flame graphs in your CI pipeline with CodSpeed, and find a way to make sure that the flame graphs are consistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automating flame graphs with CodSpeed
&lt;/h2&gt;

&lt;p&gt;Since we already had wrappers of benchmarking libraries in different languages, we decided to augment them with profiling capabilities.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to follow to get flame graphs in your CI pipeline
&lt;/h3&gt;

&lt;p&gt;Write benchmarks with one of our wrappers in Rust, Node.js, or Python.&lt;/p&gt;

&lt;p&gt;For example, in Python, use our &lt;code&gt;pytest&lt;/code&gt; extension &lt;code&gt;pytest-codspeed&lt;/code&gt; (which API is compatible with &lt;code&gt;pytest-benchmark&lt;/code&gt;) and add a test marked as a benchmark:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pytest&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;parse_pr&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;parse_pr&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;pytest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;benchmark&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_parse_pr&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;parse_pr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;very_long_body_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pr_number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;126&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Refactor some code"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the benchmarks in the &lt;code&gt;@codspeed/action&lt;/code&gt; GitHub Action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run benchmarks&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;CodSpeedHQ/action@v1&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pytest tests/ --codspeed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The action will run the benchmarks on a "virtualized" CPU with Valgrind. Instead of measuring the execution time, it measures the number of CPU cycles and memory accesses. Each benchmark is only ran once and it is enough to get &lt;strong&gt;consistent data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This data is then sent to CodSpeed's servers and flame graphs are generated. A comment is added to the GitHub Pull Request, with a link to the CodSpeed app, where you can browse the benchmarks and their flame graphs.&lt;/p&gt;

&lt;p&gt;And voilà! You now have flame graphs in your CI pipeline, with the benefits of consistency and automation. Moreover, now that are using CodSpeed, &lt;strong&gt;performance regressions will automatically be detected and reported&lt;/strong&gt; in your pull requests 🎉&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Head out to our &lt;a href="https://docs.codspeed.io"&gt;documentation&lt;/a&gt; to view integrationsfor the different languages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Going further
&lt;/h2&gt;

&lt;p&gt;Flame graphs are not only useful for spotting performance regressions but they can also be used to understand the performance impact of a change. Being an improvement or new calls to a function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HTtamq3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpfyfyjx999yzxcw04vs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HTtamq3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpfyfyjx999yzxcw04vs.png" width="800" height="187"&gt;&lt;/a&gt;&lt;br&gt;A more complex flame graph with regressions, improvements, and added code
  &lt;/p&gt;

&lt;p&gt;If you enjoyed this article, you can follow us on &lt;a href="https://twitter.com/codspeedhq"&gt;Twitter&lt;/a&gt; to get notified when we publish new articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://docs.codspeed.io/features/trace-generation"&gt;Trace Generation&lt;/a&gt; in the CodSpeed documentation&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/CodSpeedHQ/pytest-codspeed"&gt;pytest-codspeed&lt;/a&gt;, plugin for pytest&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/CodSpeedHQ/action"&gt;@codspeed/action&lt;/a&gt;, GitHub Action to run benchmarks and generate flame graphs&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.brendangregg.com/flamegraphs.html"&gt;Flame Graphs&lt;/a&gt; by Brendan Gregg&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.brendangregg.com/blog/2014-11-09/differential-flame-graphs.html"&gt;Differential Flame Graphs&lt;/a&gt; by Brendan Gregg&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue"&gt;Linking a pull request to an issue&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://valgrind.org/"&gt;Valgrind&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/ionelmc/pytest-benchmark"&gt;pytest-benchmark&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>performance</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
