{"id":"cpp-log-analyzer","title":"Project A: a log analyzer in Python and C++","sections":[{"id":"overview","title":"Overview","html":"<p>It is the middle of the night, and the inference service you run has an alert: users say chat responses feel slow. The dashboards are down, but every server writes one line per request to an access log, and you have the last few million lines. The questions anyone asks first are simple. How many requests failed, and with which status codes? Which endpoint is slow? What are the p50, p95 and p99 latencies? Someone will reach for Python, and that is a fine first move. This project builds that tool twice, once in Python and once in C++, and then compares them honestly.</p>\n<p>Latency percentiles are the language of serving systems. A service-level objective for an LLM API is usually written as a percentile, such as \"p99 time to first token under two seconds\", because the average hides the users who wait longest. The <a href=\"/learn/llm-inference#latency-and-throughput-metrics\">LLM inference chapter</a> defines time to first token and the other serving metrics, and Project E later in this course measures percentiles again on a simulated inference server. Here you compute them from raw data, and you learn exactly what \"p99\" means, which turns out to need a precise definition.</p>\n<p>This is also your first real C++ program: several source files, a library and a test executable built by CMake, checked under the sanitizers from <a href=\"/learn/cpp-undefined-behavior\">Chapter 5</a>, and timed against Python. Along the way you meet a few pieces of the standard library that make text processing fast, each introduced when it is needed. Chapter 9 covers the containers and algorithms properly. The goal is not just a fast program but a correct one: the two versions must print the same report, byte for byte, so that <code>diff</code> can prove they agree.</p>","headings":[]},{"id":"what-the-analyzer-reports","title":"What the analyzer reports","html":"<h3 id=\"what-the-analyzer-reports\">What the analyzer reports</h3>\n<p>The input is a text file with one request per line. The output is a short report with three parts:</p>\n<ol>\n<li>How many lines were read, how many were malformed and skipped, and how many valid requests remain.</li>\n<li>A table of status codes, in increasing order, with each code's count and share of the requests.</li>\n<li>A latency table: one row for all requests together, then one row per endpoint, with the endpoints sorted so the one with the worst p99 comes first. Each row has the count, the mean, p50, p95, p99, p99.9 and the maximum, in milliseconds.</li>\n</ol>\n<p>Why report percentiles at all, instead of the average? The system design course explains it in <a href=\"/learn/performance-and-latency#averages-hide-the-slow-requests\">Averages hide the slow requests</a>: a mean blends fast and slow requests into a number that describes neither, while p99 tells you what the slowest 1 in 100 users experienced. The widget below lets you see it. It holds 10,000 simulated latencies, and you choose how many of them are slow and how slow they are.</p>\n<div class=\"widget-slot\" data-widget=\"latency-percentiles\"></div>\n<p>Try these settings:</p>\n<ul>\n<li>Start at 0% slow. The mean and the median are almost the same, and p99 is only a little more than twice the median, which is the ordinary spread of fast requests.</li>\n<li>Set 2% slow at 20×. The median barely moves, but p99 jumps to the slow requests. The mean rises too, yet it still looks like an ordinary fast request and says nothing about the 1 user in 50 who waited most of a second.</li>\n<li>Set 0.5% slow at 50×. Now p99 stays fast, because fewer than 1 request in 100 is slow and the whole tail fits above the 99th percentile. This is why serving teams also watch p99.9 and the maximum, and why the analyzer reports both.</li>\n</ul>","headings":[{"id":"what-the-analyzer-reports","title":"What the analyzer reports","depth":3}]},{"id":"the-log-format-and-the-generator","title":"The log format and the generator","html":"<h3 id=\"the-log-format-and-the-generator\">The log format and the generator</h3>\n<p>Real access logs come in many formats. This project uses a simple one with five fields separated by single spaces:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span>&#x3C;timestamp in Unix milliseconds> &#x3C;method> &#x3C;endpoint> &#x3C;status> &#x3C;latency in microseconds></span></span>\n<span class=\"line\"><span>1763164800123 POST /v1/chat/completions 200 734211</span></span></code></pre>\n<p>Every number is an integer, and that choice is deliberate. Storing the latency as a whole number of microseconds, rather than as <code>734.211</code> milliseconds, means neither analyzer has to parse a floating-point number, every value is exact, and both can print milliseconds with integer arithmetic (734211 becomes \"734\" and \"211\"), so their output cannot differ in the last decimal place. Many real systems log integer durations for the same reasons.</p>\n<p>The generator, <a href=\"/labs/cpp/cpp-log-analyzer/gen_logs.py\"><code>gen_logs.py</code></a>, writes a log that looks like an OpenAI-compatible inference API. It uses only the standard library and a fixed seed, so two runs with the same arguments write identical files. Each endpoint has its own share of traffic and its own latency distribution, a log-normal one, which is a common rough model for request latencies because they cannot be negative and have a long right tail:</p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div class=\"table-wrap\"><table><thead><tr><th>Endpoint</th><th>Share of traffic</th><th>Median latency</th><th>Why it behaves this way</th></tr></thead><tbody><tr><td><code>POST /v1/chat/completions</code></td><td>45%</td><td>900 ms</td><td>generates many tokens; 0.2% wait in a queue and take ten times longer</td></tr><tr><td><code>POST /v1/completions</code></td><td>10%</td><td>450 ms</td><td>generates fewer tokens</td></tr><tr><td><code>POST /v1/embeddings</code></td><td>25%</td><td>35 ms</td><td>one forward pass, no generation</td></tr><tr><td><code>POST /v1/moderations</code></td><td>5%</td><td>60 ms</td><td>a small classifier</td></tr><tr><td><code>GET /v1/models</code></td><td>5%</td><td>3 ms</td><td>returns a list from memory</td></tr><tr><td><code>GET /health</code></td><td>10%</td><td>0.8 ms</td><td>a load balancer checking the server is alive</td></tr></tbody></table></div>\n<p>About 97% of requests succeed with status 200. The rest are 400 (a bad request) and 429 (rate limited), which are rejected in about a millisecond, 500 and 503 (server errors), and 504 (the gateway gave up after 60 seconds). About one line in 100,000 is cut short, the way a log looks when the process writing it crashed mid-line, so the analyzers have something to reject.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">python3</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> gen_logs.py</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> big.log</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">                    # 5,000,000 lines</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">python3</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> gen_logs.py</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small.log</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> --lines</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 200000</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">   # a small file for the sanitizer runs</span></span></code></pre>\n<p>A line is about 46 bytes on average, from 34 bytes for a health check to 51 for a chat completion, so the big file is about 230 MB. The generator is pure Python and takes a while; that is part of the lesson, because the same interpreter overhead will show up in the Python analyzer.</p>","headings":[{"id":"the-log-format-and-the-generator","title":"The log format and the generator","depth":3}]},{"id":"version-1-python","title":"Version 1: Python","html":"<h3 id=\"version-1-python\">Version 1: Python</h3>\n<p>Write the Python version first. It is short, it is obviously correct, and it becomes the reference the C++ version must match. The full program is <a href=\"/labs/cpp/cpp-log-analyzer/analyze.py\"><code>analyze.py</code></a>. Its heart is a parser that returns <code>None</code> for any line that breaks the rules:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> number</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(field):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"A field of 1 to 20 ASCII digits whose value fits in 64 bits, as an int, or None.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> field.isdigit() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">or</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> len</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(field) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">></span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 20</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    value </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(field)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> value </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> value </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> MAX_UINT64</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> else</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> parse_line</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(line):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"Return (status, endpoint, latency_us) for a valid line of bytes, or None.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line.endswith(</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">b</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\\n</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        line </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line[:</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">-</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">]</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    parts </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line.split(</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">b</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\" \"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> len</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(parts) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">!=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 5</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    timestamp, method, endpoint, status, latency </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parts</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> method </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">or</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> endpoint.startswith(</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">b</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"/\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    timestamp, status, latency </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> number(timestamp), number(status), number(latency)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> timestamp </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> or</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> status </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> or</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> latency </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 100</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> &#x3C;=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> status </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 599</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> or</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> latency </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">></span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> MAX_LATENCY_US</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> status, endpoint, latency</span></span></code></pre>\n<p>The rules look fussy, and they are fussy on purpose. \"Valid\" must mean exactly the same thing in both languages, or the two reports will disagree on malformed lines and you will not know which one is wrong. So the rules are written to be easy to implement identically: exactly five fields split on single spaces, numbers made only of the ASCII digits 0 to 9 (<code>bytes.isdigit()</code> accepts nothing else), at most 20 digits and a value that fits in 64 bits, a status from 100 to 599, and a latency of at most one hour. The one-hour limit also guarantees that every latency fits in a 32-bit unsigned integer, which the C++ version uses to halve its memory.</p>\n<p>The file is opened in binary mode, <code>open(path, \"rb\")</code>. That skips decoding every line from UTF-8, and it stops Python from translating Windows line endings, which C++ does not do either. The main loop counts statuses in a dictionary and appends each latency to two lists, one for all requests and one for the request's endpoint:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">with</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> open</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(sys.argv[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">], </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"rb\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">as</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> f:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> f:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        lines </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        record </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parse_line(line)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> record </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            malformed </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            continue</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        status, endpoint, latency </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> record</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        by_status[status] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> by_status.get(status, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">0</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        all_latencies.append(latency)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        by_endpoint.setdefault(endpoint, []).append(latency)</span></span></code></pre>\n<p>Think about what this costs per line, because it predicts the comparison later. Every line becomes a new <code>bytes</code> object, <code>split</code> makes five more, <code>int</code> makes up to three integer objects, and the call to <code>parse_line</code> returns a new tuple. Each step runs through the interpreter. Memory adds up too: a CPython <code>int</code> above 256 is a separate object, 28 bytes for values below 2^30, and each list slot is an 8-byte pointer to it. Both lists point to the same object, so each request costs roughly 28 + 8 + 8 = 44 bytes, or a little over 200 MB for five million requests.</p>\n<h4 id=\"pinning-down-p99\">Pinning down \"p99\"</h4>\n<p>\"The 99th percentile\" sounds precise, but there are many definitions in use. Statisticians Rob Hyndman and Yanan Fan catalogued nine of them in 1996, and statistics packages disagree on which to use by default. NumPy's <code>np.percentile</code>, for example, defaults to linear interpolation: when the rank falls between two values, it returns a weighted blend of the two, which may be a latency that no request actually had.</p>\n<p>This project uses the simplest definition, the <strong>nearest-rank</strong> method: p is the smallest value such that at least p% of the values are less than or equal to it. In a sorted list of n values, that is the value at 1-based rank ⌈p × n / 100⌉. For the 10 values 1 to 10, p50 is the 5th value, 5, and p95, p99 and p99.9 are all the 10th, 10. NumPy can compute the same thing with <code>np.percentile(a, 99, method=\"inverted_cdf\")</code>, which is Hyndman and Fan's definition 1.</p>\n<p>Both analyzers compute the rank with integers only, writing the percentile in thousandths (p99 is 990, p99.9 is 999) so that p99.9 needs no fractions:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> rank_index</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(n, permille):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> max</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">((permille </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> n </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 999</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">//</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1000</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">-</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">   # ceil(permille * n / 1000), as a 0-based index</span></span></code></pre>\n<p>Adding 999 before the integer division rounds up, which turns floor division into a ceiling. Avoiding floating point here is not pedantry: in floating point, <code>0.55 * 100</code> is 55.00000000000001, whose ceiling is 56, so p55 of 100 values would come out one rank too high. With integers, both languages get the same rank for every n.</p>\n<p>The mean is the integer sum divided by the count, rounded down to a whole microsecond, and the report prints milliseconds as <code>us // 1000</code>, a dot and <code>us % 1000</code> padded to three digits. Every number in the report is therefore computed and printed with integer arithmetic, which is what makes a byte-for-byte comparison possible.</p>","headings":[{"id":"version-1-python","title":"Version 1: Python","depth":3},{"id":"pinning-down-p99","title":"Pinning down \"p99\"","depth":4}]},{"id":"version-2-c-file-by-file","title":"Version 2: C++, file by file","html":"<h3 id=\"version-2-c-file-by-file\">Version 2: C++, file by file</h3>\n<p>The C++ version is split the way a real project would be:</p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div class=\"table-wrap\"><table><thead><tr><th>File</th><th>What it holds</th></tr></thead><tbody><tr><td><a href=\"/labs/cpp/cpp-log-analyzer/src/log_parser.h\"><code>src/log_parser.h</code></a>, <a href=\"/labs/cpp/cpp-log-analyzer/src/log_parser.cpp\"><code>src/log_parser.cpp</code></a></td><td>the <code>LogRecord</code> struct and <code>parse_line</code>, with the same rules as the Python parser</td></tr><tr><td><a href=\"/labs/cpp/cpp-log-analyzer/src/stats.h\"><code>src/stats.h</code></a>, <a href=\"/labs/cpp/cpp-log-analyzer/src/stats.cpp\"><code>src/stats.cpp</code></a></td><td><code>rank_index</code>, <code>summarize</code> and the integer-only number formatting</td></tr><tr><td><a href=\"/labs/cpp/cpp-log-analyzer/src/main.cpp\"><code>src/main.cpp</code></a></td><td>the command line, the read loop, the counting and the report</td></tr><tr><td><a href=\"/labs/cpp/cpp-log-analyzer/tests/test_core.cpp\"><code>tests/test_core.cpp</code></a></td><td>checks for the parser, the percentiles and the formatting</td></tr><tr><td><a href=\"/labs/cpp/cpp-log-analyzer/CMakeLists.txt\"><code>CMakeLists.txt</code></a></td><td>builds a library, the program and the tests</td></tr></tbody></table></div>\n<p>The parser and the statistics do not know about files or the command line, so they go into a library that two programs link: the analyzer and the tests. This is the same <code>add_library</code> and <code>target_link_libraries</code> pattern as Chapter 1's tinymath, with one addition, a test registered with CTest:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">add_library</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(loga_core src/log_parser.cpp src/stats.cpp)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">target_include_directories</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(loga_core </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">PUBLIC</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> src)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">add_executable</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(log_analyzer src/main.cpp)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">target_link_libraries</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(log_analyzer </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">PRIVATE</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> loga_core)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">add_executable</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(test_core tests/test_core.cpp)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">target_link_libraries</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(test_core </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">PRIVATE</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> loga_core)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">enable_testing</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">()</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">add_test</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">NAME</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> test_core </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">COMMAND</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> test_core)</span></span></code></pre>\n<p>After a build, <code>ctest --test-dir build</code> runs every registered test and reports which passed. Everything lives in a namespace, <code>loga</code>, so that names like <code>summarize</code> cannot collide with anything else. The code uses plain structs and functions only; Chapter 6 introduces classes.</p>\n<h4 id=\"reading-lines-ifstream-and-getline\">Reading lines: ifstream and getline</h4>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">ifstream</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">path</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">ios</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">binary</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">);</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">string</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line;</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">   // reused for every line</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">while</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">getline</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(in, line)) {</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  ++</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">lines;</span></span>\n<span class=\"line\"><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  // parse and count ...</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre>\n<p><code>std::ifstream</code>, from <code>&#x3C;fstream></code>, is a file opened for reading, and <code>std::getline</code> reads characters into <code>line</code> up to the next <code>'\\n'</code>, which it consumes and does not store. Both are buffered: the stream reads the file in large blocks and hands out lines from its buffer, so there is not one system call per line. Declaring <code>line</code> once, outside the loop, matters more than it looks. <code>getline</code> reuses the string's existing buffer, so after the first few lines no more memory is allocated. A <code>std::string</code> declared inside the loop would be created and destroyed five million times.</p>\n<p>The <code>while (std::getline(...))</code> form stops at the end of the file, and it still returns the last line if the file does not end with a newline, just as Python's <code>for line in f</code> does. <code>std::ios::binary</code> means \"no newline translation\", which makes no difference on Linux and macOS but keeps the behavior identical on Windows.</p>\n<h4 id=\"splitting-without-copying-string_view\">Splitting without copying: string_view</h4>\n<p>Python's <code>split</code> makes a new object for every field. C++ can avoid copying the fields at all with <code>std::string_view</code>, from <code>&#x3C;string_view></code>: a pointer to some characters and a length, which refers to text stored somewhere else. <code>parse_line</code> takes the line as a <code>string_view</code> and splits it into five more views of the same characters:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">bool</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> parse_line</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">string_view</span><span style=\"color:#E36209;--shiki-dark:#FFAB70\"> line</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">LogRecord</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x26;</span><span style=\"color:#E36209;--shiki-dark:#FFAB70\"> out</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) {</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">  std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">string_view</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> fields[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">5</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">  std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">size_t</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> count </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 0</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">  std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">size_t</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> start </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 0</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  while</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">true</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) {</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">    std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">size_t</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> space </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">find</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">' '</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, start);</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (count </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 5</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> false</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  // a sixth field: too many</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (space </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">string_view</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::npos) {</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">      fields[count</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">++</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">substr</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(start);</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">      break</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    }</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    fields[count</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">++</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> line.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">substr</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(start, space </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">-</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> start);</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    start </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> space </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">  }</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (count </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">!=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 5</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> false</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  // ... check the method and endpoint, parse the three numbers, apply the ranges</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre>\n<p><code>find</code> returns the position of the next space, or <code>npos</code> when there is none, and <code>substr</code> on a view returns another view, not a copy. Two spaces in a row produce an empty field, and a trailing space produces an empty sixth field, both rejected, exactly like <code>split(b\" \")</code> in Python.</p>\n<p>There is a catch, and it is Chapter 3's dangling pointer in a new form. A view does not own its characters. <code>LogRecord::endpoint</code> points into <code>line</code>, and the next <code>getline</code> overwrites <code>line</code>. So the per-endpoint map must own a copy of each endpoint name:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::unordered_map</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">string</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::vector</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">uint32_t>></span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> by_endpoint;</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">string</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> key;</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  // reused, like line</span></span>\n<span class=\"line\"><span style=\"color:#6A737D;--shiki-dark:#6A737D\">// in the loop:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">key.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">assign</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(record.endpoint);</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">by_endpoint[key].</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">push_back</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(record.latency_us);</span></span></code></pre>\n<p>If the map's key type were <code>std::string_view</code>, every key would point into <code>line</code> and silently change as the loop read more lines. It would compile without a warning, and AddressSanitizer would not necessarily catch it, because <code>line</code>'s buffer is still allocated: the keys would simply read the wrong characters. Reusing <code>key</code> with <code>assign</code> copies the characters into an existing buffer, so the copy costs no allocation, and <code>by_endpoint[key]</code> copies the string into the map only the first time an endpoint is seen.</p>\n<h4 id=\"parsing-numbers-from_chars\">Parsing numbers: from_chars</h4>\n<p>There are several ways to turn <code>\"734211\"</code> into a number in C++, and the obvious ones are a poor fit here. <code>std::stoi</code> takes a <code>std::string</code>, not a view, so each call would need a copy; it skips leading spaces and stops quietly at the first non-digit, so <code>\"12abc\"</code> becomes 12; and it reports errors by throwing exceptions. A <code>std::stringstream</code> is flexible but slow, partly because it consults the locale for every number.</p>\n<p><code>std::from_chars</code>, from <code>&#x3C;charconv></code>, was added in C++17 for exactly this job. It reads digits from a range of characters, writes the value, and returns where it stopped and an error code. It does not allocate, does not throw, ignores the locale, and does not skip spaces. For an unsigned type it also refuses a minus sign. One helper wraps it with the same rule as Python's <code>number</code>:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">bool</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> parse_number</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">string_view</span><span style=\"color:#E36209;--shiki-dark:#FFAB70\"> field</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">uint64_t&#x26;</span><span style=\"color:#E36209;--shiki-dark:#FFAB70\"> value</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) {</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (field.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">empty</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">||</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> field.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">size</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">></span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 20</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> false</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  const</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> char*</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> end </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> field.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">data</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> field.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">size</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">();</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  auto</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [ptr, ec] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">from_chars</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(field.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">data</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(), end, value);</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ec </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::errc{} </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x26;&#x26;</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ptr </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> end;</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre>\n<p><code>ec == std::errc{}</code> means a number was read and it fit in 64 bits; <code>ptr == end</code> means every character of the field was a digit. A value too large for 64 bits gives <code>std::errc::result_out_of_range</code>, which Python matches by comparing with <code>2**64 - 1</code>.</p>\n<h4 id=\"counting-unordered_map\">Counting: unordered_map</h4>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::unordered_map</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">uint64_t></span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> by_status;</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">++</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">by_status[record.status];</span></span></code></pre>\n<p><code>std::unordered_map</code>, from <code>&#x3C;unordered_map></code>, is a hash table, the closest relative of a Python <code>dict</code>. Its <code>operator[]</code> returns a reference to the value for a key, and if the key is missing it first inserts one with a zero value, which makes it behave like Python's <code>defaultdict(int)</code>. That is convenient for counting and a trap elsewhere, because merely reading <code>m[k]</code> for a missing key adds it.</p>\n<p>An unordered map has no useful iteration order, so the report copies the status counts into a vector of pairs and sorts it before printing. A <code>std::map</code>, which keeps its keys sorted, would also work. Chapter 9 compares the containers properly.</p>\n<h4 id=\"percentiles-sort-or-nth_element\">Percentiles: sort or nth_element</h4>\n<p>To find a percentile, you need the value that would sit at a given position if the data were sorted. Python sorts each list, which costs O(n log n) comparisons. But a report needs only four positions (and the maximum), not a fully sorted list, and the standard library has an algorithm for exactly that. <code>std::nth_element(first, nth, last)</code> rearranges the range so that the element at <code>nth</code> is the one that would be there after sorting, with nothing larger before it and nothing smaller after it. It runs in linear time on average.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">auto</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> first </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> values.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">begin</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">();</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> i </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 0</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">; i </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> kNumPercentiles; </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">++</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">i) {</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  auto</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> nth </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> values.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">begin</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> static_cast&#x3C;</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">ptrdiff_t</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">></span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">rank_index</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(values.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">size</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(), kPermilles[i]));</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">  std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">nth_element</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(first, nth, values.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">end</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">());</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">  s.percentile[i] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> *</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">nth;</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">  first </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> nth;</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">s.max </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> *</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">max_element</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(first, values.</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">end</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">());</span></span></code></pre>\n<p>Because the ranks increase (p50, then p95, then p99, then p99.9), each search can start at the previous answer: after the first call, everything from <code>nth</code> onward is at least as large as the p50 value, so the p95 value is somewhere in that part. The maximum is in that part too. The analyzer also accepts <code>--sort</code>, which uses <code>std::sort</code> and reads the ranks directly, so you can measure the difference.</p>\n<p><code>rank_index</code> is where Chapter 5 earns its place. With <code>n</code> at five million and the permille at 999, the product <code>permille * n</code> is about 5 × 10^9, which is larger than the 2.1 billion an <code>int</code> can hold. Computed with <code>int</code>, it would be signed overflow, undefined behavior, and almost certainly a wrong rank. The code computes it with <code>std::uint64_t</code>, and one of the tests checks the five-million case.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">size_t</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> rank_index</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">size_t</span><span style=\"color:#E36209;--shiki-dark:#FFAB70\"> n</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">uint64_t</span><span style=\"color:#E36209;--shiki-dark:#FFAB70\"> permille</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) {</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">  std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">uint64_t</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (permille </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> n </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 999</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">/</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1000</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> (rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 0</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">  return</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> static_cast&#x3C;</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">size_t></span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">-</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">);</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre>\n<p>Latencies are stored as <code>std::uint32_t</code>, 4 bytes each, since the one-hour limit guarantees they fit. Two vectors of five million latencies, one for all requests and one split by endpoint, then need about 40 MB, plus spare capacity while the vectors grow, compared with the roughly 200 MB estimated for Python. The sum used for the mean, though, is a <code>std::uint64_t</code>: a 32-bit sum would wrap around after a few thousand one-second requests.</p>\n<h4 id=\"the-same-report-byte-for-byte\">The same report, byte for byte</h4>\n<p>The report is printed with <code>std::cout</code> and the manipulators from <code>&#x3C;iomanip></code>: <code>std::setw(12)</code> pads the next value to 12 characters, and <code>std::left</code> and <code>std::right</code> choose the side. These match Python's format specifications <code>:&#x3C;24</code> and <code>:>12</code> exactly. Milliseconds are formatted the same way in both languages:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">string</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> format_ms</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">uint64_t</span><span style=\"color:#E36209;--shiki-dark:#FFAB70\"> us</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) { </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">return</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">to_string</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(us </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">/</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1000</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> \".\"</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> +</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> three_digits</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(us); }</span></span></code></pre>\n<p>The endpoint rows are sorted by p99, slowest first, and by name when two p99s are equal, so the order is fully determined. Both programs write their timings to standard error rather than standard output, so the timings never end up in the report files you compare.</p>","headings":[{"id":"version-2-c-file-by-file","title":"Version 2: C++, file by file","depth":3},{"id":"reading-lines-ifstream-and-getline","title":"Reading lines: ifstream and getline","depth":4},{"id":"splitting-without-copying-string_view","title":"Splitting without copying: string_view","depth":4},{"id":"parsing-numbers-from_chars","title":"Parsing numbers: from_chars","depth":4},{"id":"counting-unordered_map","title":"Counting: unordered_map","depth":4},{"id":"percentiles-sort-or-nth_element","title":"Percentiles: sort or nth_element","depth":4},{"id":"the-same-report-byte-for-byte","title":"The same report, byte for byte","depth":4}]},{"id":"build-test-and-run-under-the-sanitizers","title":"Build, test, and run under the sanitizers","html":"<h3 id=\"build-test-and-run-under-the-sanitizers\">Build, test, and run under the sanitizers</h3>\n<p>Download the folder's files, keeping <code>src/</code> and <code>tests/</code> as subfolders, and build a Release version for timing:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">cmake</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> -S</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> .</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> -B</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> build</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> -DCMAKE_BUILD_TYPE=Release</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">cmake</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> --build</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> build</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">ctest</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> --test-dir</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> build</span></span></code></pre>\n<p>The tests use a small <code>CHECK</code> macro instead of <code>assert</code>, for a reason worth knowing. CMake's Release build passes <code>-DNDEBUG</code>, and <code>assert</code> expands to nothing when <code>NDEBUG</code> is defined, so tests written with <code>assert</code> silently test nothing in Release. <code>CHECK</code> prints the failing condition and its line and keeps going.</p>\n<p>Now prove the C++ version agrees with the reference on the small file first, where mistakes are cheap:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">python3</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> analyze.py</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small.log</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> ></span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small_py.txt</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">./build/log_analyzer</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small.log</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> ></span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small_cpp.txt</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">diff</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small_py.txt</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small_cpp.txt</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> &#x26;&#x26; </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">echo</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> identical</span></span></code></pre>\n<p>If <code>diff</code> prints anything, one of the two versions has a bug, and the differing lines tell you where to look. Then build a second copy with AddressSanitizer and UndefinedBehaviorSanitizer and run it on the small file:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">cmake</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> -S</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> .</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> -B</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> build-san</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> -DCMAKE_BUILD_TYPE=Debug</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> -DLAB_SANITIZE=ON</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">cmake</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> --build</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> build-san</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">./build-san/test_core</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">./build-san/log_analyzer</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> small.log</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> ></span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> /dev/null</span></span></code></pre>\n<p>A clean run prints only the timings line. Use the small file because a sanitized Debug build is several times slower than Release, and nothing about the bugs it looks for depends on the file's size. If you change the code, for example to try a <code>string_view</code> key, run this build again before you trust the result.</p>","headings":[{"id":"build-test-and-run-under-the-sanitizers","title":"Build, test, and run under the sanitizers","depth":3}]},{"id":"timing-it-fairly","title":"Timing it fairly","html":"<h3 id=\"timing-it-fairly\">Timing it fairly</h3>\n<p>A comparison is only worth something if it is fair, and timing programs that read a 230 MB file has several traps:</p>\n<ul>\n<li><strong>Build in Release.</strong> A Debug build is unoptimized and can be many times slower. Timing it measures the compiler's settings, not your code.</li>\n<li><strong>Warm the file cache.</strong> The first run after generating the file may read it from disk, and later runs read it from the operating system's page cache in memory. Run each program once without timing it, then time it several times and look at the spread. (To measure cold reads on purpose, Linux can drop the cache with <code>sync; echo 3 | sudo tee /proc/sys/vm/drop_caches</code>, and macOS has <code>sudo purge</code>.)</li>\n<li><strong>Time the whole program, and its phases.</strong> The shell's <code>time</code> command reports wall-clock time (<code>real</code>) and CPU time (<code>user</code> and <code>sys</code>) for the whole run. Inside the programs, Python uses <code>time.perf_counter()</code> and C++ uses <code>std::chrono::steady_clock</code>, a clock that never jumps backwards when the system time is adjusted, to time the two phases separately: reading and parsing, then summarizing and printing.</li>\n<li><strong>Check the output every time.</strong> A faster program that prints a different report is not faster, it is wrong. Keep the <code>diff</code>.</li>\n</ul>\n<p>The C++ timer is a few lines:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">const</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> auto</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> t0 </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">chrono</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">steady_clock</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">now</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">();</span></span>\n<span class=\"line\"><span style=\"color:#6A737D;--shiki-dark:#6A737D\">// ... read and parse ...</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">double</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> seconds </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">chrono</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">duration</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">&#x3C;</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">double</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">>(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">std</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">chrono</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">steady_clock</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">::</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">now</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">-</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> t0).</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">count</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">();</span></span></code></pre>\n<p><code>std::chrono::duration&#x3C;double></code> converts the difference to seconds as a <code>double</code>. For repeated runs with statistics, the <a href=\"https://github.com/sharkdp/hyperfine\" target=\"_blank\" rel=\"noopener noreferrer\">hyperfine</a> tool runs a command many times, with warm-up runs, and reports the mean and spread. Chapter 14 is about measuring performance properly.</p>","headings":[{"id":"timing-it-fairly","title":"Timing it fairly","depth":3}]},{"id":"the-comparison","title":"The comparison","html":"<h3 id=\"the-comparison\">The comparison</h3>\n<p>Now run both on the big file:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">time</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> python3 analyze.py big.log </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">></span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> report_py.txt</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">time</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ./build/log_analyzer big.log </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">></span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> report_cpp.txt</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">time</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ./build/log_analyzer --sort big.log </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">></span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> report_sort.txt</span></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">diff</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> report_py.txt</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> report_cpp.txt</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> &#x26;&#x26; </span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">diff</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> report_cpp.txt</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> report_sort.txt</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> &#x26;&#x26; </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">echo</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> identical</span></span></code></pre>\n<p>Before you look at the numbers, predict them. Which phase dominates in Python, parsing or summarizing? Python's sort is written in C and is fast, but the parsing loop runs a handful of interpreted operations and allocates several objects for every one of five million lines. In C++ the parsing loop allocates nothing after the first few lines, so expect most of its time to go to reading the file and finding the spaces, and very little to the statistics. Then predict the ratio between the two total times, and write it down.</p>\n<p>Memory is worth measuring too. On macOS, <code>/usr/bin/time -l</code> prints the maximum resident set size, the peak physical memory the process used, in bytes. On Linux, <code>/usr/bin/time -v</code> prints it in kilobytes. Compare it with the estimates above: roughly 200 MB of integer objects and list slots for Python, and about 40 MB of latencies plus vector growth for C++.</p>\n<p>Finally, look at the report itself. Which endpoint has the worst p99, and is it the one with the worst mean? Is p99 of all requests dominated by one endpoint? Which status codes have the strange latencies, and why? The generator's table above says what to expect.</p>","headings":[{"id":"the-comparison","title":"The comparison","depth":3}]},{"id":"stretch-goals","title":"Stretch goals","html":"<h3 id=\"stretch-goals\">Stretch goals</h3>\n<ul>\n<li><strong>Compare with NumPy.</strong> Load the latencies into a NumPy array and compute <code>np.percentile(a, [50, 95, 99, 99.9], method=\"inverted_cdf\")</code>. It should match the report. Then try the default method and see how far the interpolated values move.</li>\n<li><strong>Read the file in one go.</strong> Replace <code>getline</code> with reading the whole file into one buffer and splitting it yourself, or map the file into memory with <code>mmap</code>, which Chapter 21 covers. Measure how much of the C++ time was the line-by-line reading.</li>\n<li><strong>Use two cores.</strong> Split the file into two halves at a line boundary and parse each half in its own thread, then merge the counts and the latency vectors. Part IV teaches threads properly; this is a preview of why merging results is the hard part.</li>\n<li><strong>Bound the memory.</strong> Storing every latency does not scale to a billion lines. Replace the vectors with a fixed set of logarithmic latency buckets, like the widget's histogram, and report approximate percentiles from the bucket counts. HdrHistogram, in the further reading below, is a well-known implementation of this idea.</li>\n<li><strong>Try PyPy or pandas.</strong> Run <code>analyze.py</code> under PyPy, a Python implementation with a just-in-time compiler, or rewrite it with pandas' <code>read_csv</code>, and see how much of the gap each one closes.</li>\n</ul>","headings":[{"id":"stretch-goals","title":"Stretch goals","depth":3}]},{"id":"check-yourself","title":"Check yourself","html":"<h3 id=\"check-yourself\">Check yourself</h3>\n<details>\n<summary>Why does the log store latency as integer microseconds instead of fractional milliseconds?</summary>\n<p>Integers are exact, so both analyzers compute identical sums, ranks and percentiles, and both can print milliseconds with integer division and remainder, which makes the two reports identical byte for byte. It also means neither program parses floating-point text, which is slower and harder to make identical across languages.</p>\n</details>\n<details>\n<summary>For the latencies 10, 20, 30, 40 and 1000 ms, what are the nearest-rank p50 and p99, and what is the mean?</summary>\n<p>With n = 5, p50 is at rank ⌈0.5 × 5⌉ = 3, which is 30 ms, and p99 is at rank ⌈0.99 × 5⌉ = 5, which is 1000 ms. The mean is 1100 / 5 = 220 ms, a latency that no request had, which is why the report leads with percentiles.</p>\n</details>\n<details>\n<summary>The C++ version stores each endpoint as a std::string_view key in an unordered_map pointing into the line buffer. What goes wrong?</summary>\n<p>The view does not own its characters. <code>getline</code> overwrites the line buffer on every iteration, so each key would point at whatever the latest line contains, and the map's keys and hashes would no longer match. The map must own a copy of each endpoint, which is why the code assigns it into a <code>std::string</code> first. AddressSanitizer may not catch it, because the buffer is still allocated memory.</p>\n</details>\n<details>\n<summary>Why use std::from_chars rather than std::stoi to parse the status and latency fields?</summary>\n<p><code>from_chars</code> works directly on a range of characters, so it needs no copy of the field, never allocates or throws, ignores the locale, and reports exactly where it stopped, so the code can require that the whole field was digits. <code>stoi</code> needs a <code>std::string</code>, skips leading spaces, quietly accepts trailing junk such as <code>12abc</code>, and reports errors with exceptions.</p>\n</details>\n<details>\n<summary>Why is std::nth_element enough for a percentile, and what does it cost compared with sorting?</summary>\n<p>A percentile is the value at one rank of the sorted data, and <code>nth_element</code> puts exactly that value at that position, with smaller values before and larger values after, in linear time on average. A full sort costs O(n log n) and also orders everything else, which the report never uses. Because the four ranks increase, each later search can start at the previous answer.</p>\n</details>\n<details>\n<summary>The project's tests pass in a Debug build but would test nothing in a Release build if they used assert. Why?</summary>\n<p>CMake's Release configuration defines <code>NDEBUG</code>, and <code>assert</code> expands to nothing when <code>NDEBUG</code> is defined, so every check disappears and the tests always pass. The project's own <code>CHECK</code> macro does not depend on <code>NDEBUG</code>, so the checks run in every build type.</p>\n</details>\n<h3 id=\"further-reading\">Further reading</h3>\n<ul>\n<li><a href=\"https://www.jstor.org/stable/2684934\" target=\"_blank\" rel=\"noopener noreferrer\">Sample Quantiles in Statistical Packages</a>, by Rob Hyndman and Yanan Fan (The American Statistician, 1996), compares nine definitions of a sample quantile and recommends one; the nearest-rank method is their definition 1.</li>\n<li><a href=\"https://numpy.org/doc/stable/reference/generated/numpy.percentile.html\" target=\"_blank\" rel=\"noopener noreferrer\">numpy.percentile</a> documents NumPy's <code>method</code> argument, its linear default and the <code>inverted_cdf</code> option.</li>\n<li><a href=\"https://en.cppreference.com/w/cpp/utility/from_chars\" target=\"_blank\" rel=\"noopener noreferrer\">cppreference: std::from_chars</a> describes exactly what it accepts and the errors it reports, and <a href=\"https://en.cppreference.com/w/cpp/string/basic_string_view\" target=\"_blank\" rel=\"noopener noreferrer\">std::basic_string_view</a> warns about views that outlive their characters.</li>\n<li><a href=\"https://en.cppreference.com/w/cpp/algorithm/nth_element\" target=\"_blank\" rel=\"noopener noreferrer\">cppreference: std::nth_element</a> and <a href=\"https://en.cppreference.com/w/cpp/container/unordered_map\" target=\"_blank\" rel=\"noopener noreferrer\">std::unordered_map</a> give the guarantees and complexity of the two workhorses of this project.</li>\n<li><a href=\"https://www.barroso.org/publications/TheTailAtScale.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">The Tail at Scale</a>, by Jeffrey Dean and Luiz André Barroso, explains why tail latency dominates in large services.</li>\n<li><a href=\"https://hdrhistogram.github.io/HdrHistogram/\" target=\"_blank\" rel=\"noopener noreferrer\">HdrHistogram</a> is a widely used histogram for recording latencies with bounded memory and known precision, the idea behind the \"bound the memory\" stretch goal.</li>\n<li><a href=\"https://github.com/sharkdp/hyperfine\" target=\"_blank\" rel=\"noopener noreferrer\">hyperfine</a> is a command-line benchmarking tool that handles warm-up runs and repetitions for you.</li>\n</ul>","headings":[{"id":"check-yourself","title":"Check yourself","depth":3},{"id":"further-reading","title":"Further reading","depth":3}]}]}