Skip to content

Commit 29894ca

Browse files
committed
blog: update blogs
1 parent 314cdb7 commit 29894ca

24 files changed

+153
-13
lines changed

content/english/posts/2024/12-10-adding-searchbox-to-blog-site.md content/english/posts/2024/adding-searchbox-to-hugo-blog-site.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ INFINI Pizza is an upcoming search engine developed by INFINI Labs, written in R
2020

2121
Currently, The website of INFINI Pizza have integrated INFINI Pizza for WebAssembly. The specific search results are shown in the image below:
2222

23-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-1.png)
23+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-1.png)
2424

2525
## How to use INFINI Pizza for WebAssembly?
2626
Visit the website ([http://pizza.rs](http://pizza.rs/)), and by pressing the shortcut key 's', you can bring up the search box and experience the search capabilities provided by INFINI Pizza. Notably, during the search process, all of your actions are executed locally in the browser. Unlike traditional search implementations, where each query requires an interaction with a backend search server, INFINI Pizza for WebAssembly operates entirely offline. Even if you're disconnected from the internet, you can still enjoy a seamless search experience.
@@ -66,22 +66,22 @@ Since the example project has already uploaded the compiled code and samples, we
6666

6767
Open your browser and visit: [http://localhost:8083](http://localhost:8083/), as shown below:
6868

69-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-2.gif)
69+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-2.gif)
7070

7171
Observe the network requests in the browser, and you'll see that it loads the sample index.json data:
7272

73-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-3.png)
73+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-3.png)
7474

7575
In practice, if it's our own static website or blog, simply ensuring that this file (index.json) with the appropriate format exists in the root directory of the site is enough to quickly integrate the search functionality you see into your own website. OK, with the functionality verified, let's start integrating it into our site.
7676

7777
The official website of Pizza/INFINI Labs is statically generated using Hugo, and the index.json file does not need to be manually created. First, we need to enable Hugo to generate content in JSON format, which is a built-in capability of Hugo. We'll need to modify the configuration of the Hugo project:
7878

79-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-4.png)
79+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-4.png)
8080

8181

8282
Add a JSON output option to the outputs parameter, and then define the JSON output format template in the theme's template files:
8383

84-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-5.png)
84+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-5.png)
8585

8686

8787
The text content is as follows for easy copying and pasting. Save the file with the name index.json:
@@ -95,7 +95,7 @@ The text content is as follows for easy copying and pasting. Save the file with
9595
```
9696
OK, next, we need to add the tags we’ve used above to the metadata of each article or blog post on the site:
9797

98-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-6.png)
98+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-6.png)
9999

100100

101101
OK, start the Hugo site:
@@ -127,7 +127,7 @@ OK, start the Hugo site:
127127
128128
Open the Hugo site address and try accessing http://localhost:1313/index.json. You should be able to access the JSON file:
129129
130-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-7.png)
130+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-7.png)
131131
132132
## Integrate the search widget to Hugo
133133
@@ -151,28 +151,28 @@ Do you remember the resource files we downloaded from Pizza-searchbox earlier? W
151151
```
152152
153153
Open the index.html file, and you will see the following content:
154-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-8.png)
154+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-8.png)
155155
156156
In order to integrate with the searchbox, we just need to add the three highlighted sections of code into our own static website.
157157
158158
Copy the files from the assets directory to our Hugo site, in the following location:
159-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-9.png)
159+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-9.png)
160160
161161
Then, modify the Hugo theme templates by adding a segment of code to the html-head.html file in the header template of all pages to load our CSS stylesheet:
162-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-10.png)
162+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-10.png)
163163
164164
Next, continue modifying the Hugo theme template files by adding a segment of code to the footer template of all pages to load the JS script files:
165-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-11.png)
165+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-11.png)
166166
167167
Next, insert a `docsearch` tag in the appropriate place within the page template to place the search box, as shown in the image:
168-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-12.png)
168+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-12.png)
169169
170170
> the tag `docsearch` may be renamed to `searchbox` in the future
171171
172172
With that, the task is complete!
173173
174174
Open your browser to see the final result:
175-
![INFINI Pizza Search](/images/posts/2024/pizza-search-box-13.png)
175+
![INFINI Pizza Search](/images/posts/2024/pizza-search-box/pizza-search-box-13.png)
176176
177177
Finally, to summarize, with the help of the 3 lines of code and copy 3 files from INFINI Pizza searchbox, you can add a lightweight offline search functionality to your static site in just 5 minutes. Give it a try!
178178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
title: "Profiling Rust Code on macOS: My Daily Workflow"
3+
meta_title: "Rust Profiling on macOS: Micro-Benchmarks, Flamegraphs, and DTrace"
4+
description: "Learn how to optimize Rust applications with profiling tools like criterion, cargo flamegraph, and DTrace on macOS."
5+
date: 2024-12-11T08:00:00Z
6+
image: "/images/posts/2024/profiling-rust-on-macos.jpg"
7+
categories: ["Rust", "Performance"]
8+
author: "Medcl"
9+
tags: ["Rust", "Profiling", "macOS", "criterion", "flamegraph"]
10+
draft: false
11+
---
12+
13+
Profiling Rust code has become part of my daily routine. As I primarily develop on macOS, I've noticed there aren't many tools that allow for easy and quick profiling of Rust applications. So, I’d like to share my daily profiling workflow, in case it helps others. If you have other approaches or tools that work well for you, feel free to share—I’d love to hear them!
14+
15+
16+
## Setting Up Micro-Benchmarks
17+
18+
I use micro-benchmark tests to track the performance of critical functions in my Rust application. For this, I rely on `criterion`, which is both powerful and easy to use. Here’s what my project setup looks like:
19+
20+
21+
![Rust Profiling on macOS](/images/posts/2024/benchmarking-and-profiling-rust-applications-on-macos-a-practical-guide/benchmark-rust-1.png)
22+
23+
As you noticed, i organized my benchmark tests per module, and so i can easily include or exclude specify tests in the `benches.rs`, some times, they just take too much time, if i only want to profile specify tests, i can just comment out unrelated one. dirty but works.
24+
25+
If you have many similar tests, you may group them by use a customized name, like this:
26+
27+
![Rust Profiling on macOS](/images/posts/2024/benchmarking-and-profiling-rust-applications-on-macos-a-practical-guide/benchmark-rust-2.png)
28+
29+
Micro-benchmarking is a fundamental step that helps track performance changes when refactoring code or adding new features.
30+
31+
32+
## Profiling The Micro-Benchmark
33+
34+
What if some tests are slow, a quick way to profiling is to use:
35+
```shell
36+
sudo CARGO_PROFILE_BENCH_DEBUG=true cargo flamegraph --bench benches -o find-baseline.svg -- --bench
37+
```
38+
39+
You can run the benchmarks with a single command, but be sure to comment out any unrelated tests in `benches.rs`. Just remember to revert those changes or avoid checking them in later. that's tedious, yes, i know :(
40+
41+
## Tracking Performance with Bencher
42+
43+
Now that you have several micro-benchmark tests, how can you continuously monitor performance? I’m glad I discovered a free service provided by bencher.dev. It helps track performance over time, making it easier to identify any regressions or improvements.
44+
45+
46+
47+
![Rust Profiling on macOS](/images/posts/2024/benchmarking-and-profiling-rust-applications-on-macos-a-practical-guide/benchmark-rust-3.png)
48+
49+
50+
51+
52+
Here is my bencher command in my Makefile:
53+
```shell
54+
bencher-engine:
55+
if [ -z "${BENCHER_API_TOKEN:-}" ]; then \
56+
echo "Error: BENCHER_API_TOKEN environment variable is not set."; \
57+
exit 1; \
58+
fi
59+
60+
bencher run \
61+
--project pizza-engine-bd8p44nc \
62+
--branch main \
63+
--testbed localhost \
64+
--adapter rust_criterion \
65+
"cd lib/engine && cargo bench"
66+
```
67+
68+
Each time you run `make bencher-engine`, it executes all your benchmarks and sends the results to their database. This service is free to use for open-source projects, making it a great resource for ongoing performance tracking.
69+
70+
For example here is my [dashboard](https://bencher.dev/console/projects/pizza-engine-bd8p44nc/plots):
71+
72+
![Rust Profiling on macOS](/images/posts/2024/benchmarking-and-profiling-rust-applications-on-macos-a-practical-guide/benchmark-rust-4.png)
73+
74+
You can add a CI action to your GitHub repository to automatically track performance changes with each commit. If your code is hosted on GitHub, this setup will record performance variations for every commit, helping you maintain a history of your application's performance over time.
75+
76+
![Rust Profiling on macOS](/images/posts/2024/benchmarking-and-profiling-rust-applications-on-macos-a-practical-guide/benchmark-rust-5.png)
77+
78+
## Profiling on MacOs
79+
80+
Profiling on macOS can be slightly less convenient than on Linux, where there are many robust tools available. Here’s what I do to make the most of the profiling process, I use `Dtrace` along with two scripts:
81+
82+
```shell
83+
➜ cat ~/start_profile.sh
84+
#!/bin/bash
85+
86+
# Check if PID argument is provided
87+
if [ -z "$1" ]; then
88+
echo "Usage: $0 <pid>"
89+
exit 1
90+
fi
91+
92+
pid=$1
93+
94+
# Run dtrace with the provided PID
95+
sudo rm -rf target/out.user_stacks || true
96+
97+
sudo dtrace -x ustackframes=100 -n "profile-97 /pid == $pid/ { @[ustack()] = count(); } tick-60s { exit(0); }" -o target/out.user_stacks
98+
99+
➜ cat ~/end_profile.sh
100+
#!/bin/bash
101+
102+
# Clean up previous output files
103+
rm -f target/stacks.folded target/flamegraph.svg
104+
105+
# Process the new profiling data
106+
cat target/out.user_stacks | inferno-collapse-dtrace > target/stacks.folded
107+
cat target/stacks.folded | inferno-flamegraph > target/flamegraph.svg
108+
109+
# Notify the user
110+
echo "Flamegraph generated at target/flamegraph.svg"
111+
```
112+
113+
And then start your target Rust application, make sure you set `debug=true` in `Cargo.toml`
114+
115+
And then execute the start script, wait for a while and Ctrl+C to capture profiling data:
116+
117+
```shell
118+
➜ indexer git:(batch_indexing) ✗ ~/start_profile.sh 1494
119+
dtrace: system integrity protection is on, some features will not be available
120+
121+
dtrace: description 'profile-97 ' matched 2 probes
122+
^C%
123+
```
124+
125+
And then you can generate the flamegraph:
126+
```shell
127+
➜ indexer git:(batch_indexing) ✗ ~/end_profile.sh
128+
Flamegraph generated at target/flamegraph.svg
129+
```
130+
Open it with your web browser and figure out what's the bottlenect, and rock with it.
131+
132+
![Rust Profiling on macOS](/images/posts/2024/benchmarking-and-profiling-rust-applications-on-macos-a-practical-guide/benchmark-rust-6.png)
133+
134+
135+
That’s it! I hope this information helps you in your Rust development journey. If you have any questions or need further assistance, feel free to reach out!
136+
137+
References:
138+
- https://github.com/bheisler/criterion.rs
139+
- https://bencher.dev/
140+
- https://bencher.dev/console/projects/pizza-engine-bd8p44nc/plots

0 commit comments

Comments
 (0)