-
Notifications
You must be signed in to change notification settings - Fork 684
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile the Rust code in --release mode in the dev environment for more accurate benchmarking.
- Loading branch information
Showing
2 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}") |