Skip to content

Commit

Permalink
WIP: Benchmark redwood vs gpg
Browse files Browse the repository at this point in the history
Compile the Rust code in --release mode in the dev environment for more
accurate benchmarking.
  • Loading branch information
legoktm committed Oct 20, 2023
1 parent f6d9bd0 commit 124f613
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
8 changes: 2 additions & 6 deletions redwood/build-wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@

os.chdir(args.redwood)

if args.release:
flavor = "release"
cargo_flags = ["--release"]
else:
flavor = "debug"
cargo_flags = []
flavor = "release"
cargo_flags = ["--release"]

env = {"CARGO_TARGET_DIR": str(args.target), **os.environ}

Expand Down
83 changes: 83 additions & 0 deletions securedrop/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/opt/venvs/securedrop-app-code/bin/python

import tempfile
import timeit
from pathlib import Path

import pretty_bad_protocol as gnupg

import redwood

PASSPHRASE = "correcthorsebatterystaple"
COUNTER = 0


def format_time(dt):
"""Copied out of timeit.main()"""
units = {"nsec": 1e-9, "usec": 1e-6, "msec": 1e-3, "sec": 1.0}
precision = 3
scales = [(scale, unit) for unit, scale in units.items()]
scales.sort(reverse=True)
for scale, unit in scales:
if dt >= scale:
break

return "%.*g %s" % (precision, dt / scale, unit)


r_public_key, r_secret_key, r_fingerprint = redwood.generate_source_key_pair(PASSPHRASE, "foobar")
for (input_bytes, human_bytes) in [
(5_000, "5KB"),
(50_000, "50KB"),
(500_000, "500KB"),
(5_000_000, "5MB"),
(50_000_000, "50MB"),
(500_000_000, "500MB"),
]:
loops = 10 if input_bytes >= 50_000_000 else 100
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
gpg = gnupg.GPG(
binary="gpg2",
homedir=str(tmpdir),
options=["--yes", "--trust-model direct"],
)
g_fingerprint = gpg.gen_key(
gpg.gen_key_input(
passphrase=PASSPHRASE,
name_email="foobar",
key_type="RSA",
key_length=4096,
name_real="Source Key",
creation_date="2013-05-14",
expire_date="0",
)
)
plaintext = tmpdir / "message.txt"
plaintext.write_text("A" * input_bytes)
ciphertext = tmpdir / "message.asc"

def redwood_encrypt():
global COUNTER
COUNTER += 1
ciphertext = tmpdir / f"message-{COUNTER}.asc"

redwood.encrypt_stream([r_public_key], plaintext.open(mode="rb"), ciphertext)

def gpg_encrypt():
global COUNTER
COUNTER += 1
ciphertext = tmpdir / f"message-{COUNTER}.asc"
gpg.encrypt(
plaintext.open(mode="rb"),
g_fingerprint,
output=str(ciphertext),
always_trust=True,
armor=False,
)

print(f"== {human_bytes} ({loops} loops) ==")
r_duration = timeit.repeat(redwood_encrypt, number=loops)
print(f"redwood: {[format_time(dt) for dt in r_duration]}")
g_duration = timeit.repeat(gpg_encrypt, number=loops)
print(f"gpg: {[format_time(dt) for dt in g_duration]}")

0 comments on commit 124f613

Please sign in to comment.