Skip to content

Commit

Permalink
Add native support for regex benchmark (#266)
Browse files Browse the repository at this point in the history
* Add native support for regex benchmark

* Remove comments in Dockerfile.native for regex
  • Loading branch information
jlb6740 authored Jul 21, 2023
1 parent 9b0039a commit e89fce0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions benchmarks/regex/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/rust-benchmark-native/
17 changes: 17 additions & 0 deletions benchmarks/regex/Dockerfile.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM rust:1.70

# Copy in the `src` directory.
ENV SRC=/usr/src/regex/
WORKDIR $SRC
ADD rust-benchmark rust-benchmark
COPY sightglass.native.patch ./
COPY build-native.sh .
COPY libengine.so /usr/lib/

# Compile each of the benchmarks into the `/benchmark` directory.
RUN ./build-native.sh

# We copy the shared libraries to the `/benchmark` directory, where the client
# expects it.
WORKDIR /benchmark
RUN cp $SRC/*so .
13 changes: 13 additions & 0 deletions benchmarks/regex/build-native.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Build regex benchmark as native shared libraries (Linux-only).
#
# Usage: ./build-native.sh

(set -x;)
(rm -rf rust-benchmark-native);
(cp -r rust-benchmark rust-benchmark-native/);
(cp sightglass.native.patch rust-benchmark-native/);
(cd rust-benchmark-native; patch -Np1 -i ./sightglass.native.patch; cd -);
(cd rust-benchmark-native; cargo build --release; cp target/release/libbenchmark.so ../benchmark.so; cd -);
(set +x;)
59 changes: 59 additions & 0 deletions benchmarks/regex/sightglass.native.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
diff --git a/Cargo.toml b/Cargo.toml
index ee59a1c..dbeccb7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,3 +8,6 @@ regex = "1.7.1"
sightglass-api = "0.1"

[workspace]
+
+[lib]
+crate-type = ["cdylib"]
\ No newline at end of file
diff --git a/build.rs b/build.rs
new file mode 100644
index 0000000..6d7d743
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,3 @@
+fn main() {
+ println!("cargo:rustc-cdylib-link-arg=-L../../../engines/native/");
+}
diff --git a/src/main.rs b/src/lib.rs
similarity index 84%
rename from src/main.rs
rename to src/lib.rs
index a1ef2f8..0bf007d 100644
--- a/src/main.rs
+++ b/src/lib.rs
@@ -13,16 +13,27 @@ const URI_PATTERN: &str = r"[\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?"
const IP_PATTERN: &str =
r"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])";

-fn main() {
+#[link(name = "engine")]
+extern "C" {
+ fn bench_start() -> ();
+ fn bench_end() -> ();
+}
+
+#[no_mangle]
+pub extern "C" fn native_entry() {
let path = "default.input";
eprintln!("[regex] matching {}", path);
let data = std::fs::read_to_string(path).expect("unable to find `*.input` text file");

- bench::start();
+ unsafe {
+ bench_start();
+ }
let emails = count_matches(&data, EMAIL_PATTERN);
let uris = count_matches(&data, URI_PATTERN);
let ips = count_matches(&data, IP_PATTERN);
- bench::end();
+ unsafe {
+ bench_end();
+ }

eprintln!("[regex] found {} emails", emails);
eprintln!("[regex] found {} URIs", uris);

0 comments on commit e89fce0

Please sign in to comment.