Skip to content

Commit

Permalink
Merge pull request #12 from mrf345/testing
Browse files Browse the repository at this point in the history
Add file size plot and `7z` to plots
  • Loading branch information
mrf345 authored Sep 4, 2024
2 parents fcc9ef9 + f560145 commit 5ba765d
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 130 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ go.work.sum
.env
*.sla
safelock-cli
vendor
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,19 @@ You can find interactive examples of using it as a package to [encrypt](https://

### Performance

With the default settings it should be about **19.1** times faster than `gpgtar`
With the default settings the encryption should be about **19.5** times faster than `gpgtar` and **10.1** times faster than `7zip`

> [!NOTE]
> You can reproduce the results by running [bench_and_plot.py](benchmark/bench_and_plot.py) (based on [matplotlib](https://github.com/matplotlib/matplotlib) and [hyperfine](https://github.com/sharkdp/hyperfine))
> You can reproduce the results by running [bench_and_plot.py](benchmark/bench_and_plot.py) (based on [Matplotlib](https://github.com/matplotlib/matplotlib) and [Hyperfine](https://github.com/sharkdp/hyperfine))
<p align="center">
<a href="https://raw.githubusercontent.com/mrf345/safelock-cli/master/benchmark/encryption-time.webp" target="_blank">
<img src="benchmark/encryption-time.webp" align="center" alt="encryption time" />
<img src="benchmark/encryption-time.webp" alt="encryption time" />
</a>
<a href="https://raw.githubusercontent.com/mrf345/safelock-cli/master/benchmark/decryption-time.webp" target="_blank">
<img src="benchmark/decryption-time.webp" align="center" alt="encryption time" />
<img src="benchmark/decryption-time.webp" alt="decryption time" />
</a>
<a href="https://raw.githubusercontent.com/mrf345/safelock-cli/master/benchmark/file-size.webp" target="_blank">
<img src="benchmark/file-size.webp" alt="file size" />
</a>
</p>

And you could gain a slight file size reduction

```shell
> du -hs test/
1.2G test/

> ls -lh --block-size=MB test.sla test.gpg
-rw-r--r-- 1 mrf3 mrf3 1.2G Sep 3 17:55 test.gpg
-rw-r--r-- 1 mrf3 mrf3 959M Sep 3 17:29 test.sla
```
102 changes: 71 additions & 31 deletions benchmark/bench_and_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,53 @@
safelock_cmd = "safelock-cli"
pwd = "123456789"
rest = "60s"
input_path = "test"
input_path = "~/Videos"
output_name = "test"
output_dir = "safelock_dump"
runs = 3
figure_width = 14
figure_height = 2.5
figure_height = 3
bar_width = 0.6
measure = "Seconds"
root = os.getcwd()

def get_label(i, clean=False, key="command"):
matchers = [
('gpg', 'gpgtar',),
('7z', '7zip (fastest)',),
('256', 'safelock --sha256',),
('512', 'safelock --sha512',),
('safelock', 'safelock',),
]
label = next((v for m, v in matchers if m in i[key]))

if clean:
return label
if key == "label":
return f"{label}\n{i['size']:.2f} MB"

return f"{label}\n{i['median']:.3f}s"

def get_name(i):
matchers = [
('gpg', f'{output_name}.gpg',),
('7z', f'{output_name}.7z',),
('256', f'{output_name}_sha256.sla',),
('512', f'{output_name}_sha512.sla',),
('safelock', f'{output_name}.sla',),
]

return next((v for m, v in matchers if m in i))

def encrypt():
err = os.system(
f"hyperfine --runs {runs} --prepare "
f"'sleep {rest}' "
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {output_name}.sla --quiet' "
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {output_name}_sha256.sla --quiet --sha256' "
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {output_name}_sha512.sla --quiet --sha512' "
f"'gpgtar -e -o test.gpg -c --yes --batch --gpg-args \"--passphrase {pwd}\" Videos/' "
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('safelock')} --quiet' "
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('256')} --quiet --sha256' "
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('512')} --quiet --sha512' "
f"'7z a -p{pwd} -mx1 {get_name('7z')} {input_path}' "
f"'gpgtar -e -o {get_name('gpg')} -c --yes --batch --gpg-args \"--passphrase {pwd}\" Videos/' "
f"--export-json {root}/encryption.json"
)

Expand All @@ -35,38 +64,26 @@ def encrypt():
def decrypt():
err = os.system(
f"hyperfine --runs {runs} --prepare "
f"'rm -rf {output_dir} {output_name}_1_ && mkdir {output_dir} && sleep {rest}' "
f"'echo \"{pwd}\" | {safelock_cmd} decrypt {output_name}.sla {output_dir} --quiet' "
f"'echo \"{pwd}\" | {safelock_cmd} decrypt {output_name}_sha256.sla {output_dir} --quiet --sha256' "
f"'echo \"{pwd}\" | {safelock_cmd} decrypt {output_name}_sha512.sla {output_dir} --quiet --sha512' "
f"'gpgtar -d --yes --batch --gpg-args \"--passphrase {pwd}\" test.gpg' "
f"'rm -rf {output_dir} {output_name}_*_ && mkdir {output_dir} && sleep {rest}' "
f"'echo \"{pwd}\" | {safelock_cmd} decrypt {get_name('safelock')} {output_dir} --quiet' "
f"'echo \"{pwd}\" | {safelock_cmd} decrypt {get_name('256')} {output_dir} --quiet --sha256' "
f"'echo \"{pwd}\" | {safelock_cmd} decrypt {get_name('512')} {output_dir} --quiet --sha512' "
f"'7z e -y -p{pwd} -mx1 {get_name('7z')} -o{output_dir}' "
f"'gpgtar -d --yes --batch --gpg-args \"--passphrase {pwd}\" {get_name('gpg')}' "
f"--export-json {root}/decryption.json"
)

if err:
exit(err)

def get_label(i, clean=False):
label = i['command']

if 'gpg' in label:
label = 'gpgtar'
elif 'sha256' in label:
label = 'safelock --sha256'
elif 'sha512' in label:
label = 'safelock --sha512'
else:
label = 'safelock'

if clean:
return label

return f"{label}\n{i['median']:.3f}s"

# os.chdir(os.path.expanduser("~"))
os.chdir(os.path.expanduser("~"))
# encrypt()
# decrypt()
os.chdir(root)
plt.margins(3.5)


# Encryption Time Plot

with open("encryption.json") as f:
data = sorted(json.load(f)['results'], key=lambda i: i['median'])
Expand All @@ -75,8 +92,6 @@ def get_label(i, clean=False):
colors_map = {get_label(i, 1): random.choice(list(plot_colors.values())) for i in data}
colors = [colors_map[get_label(i, 1)] for i in data]

plt.margins(3.5)

fig, ax = plt.subplots()
ax.set_title('Encryption Time')
ax.set_xlabel(measure)
Expand All @@ -85,6 +100,9 @@ def get_label(i, clean=False):
fig.tight_layout()
fig.savefig("encryption-time.webp", transparent=True, format="webp")


# Decryption Time Plot

with open("decryption.json") as f:
data = sorted(json.load(f)['results'], key=lambda i: i['median'])
labels = [get_label(i) for i in data]
Expand All @@ -98,3 +116,25 @@ def get_label(i, clean=False):
fig.set_size_inches(w=figure_width, h=figure_height)
fig.tight_layout()
fig.savefig("decryption-time.webp", transparent=True, format="webp")


# File Sizes Plot

os.chdir(os.path.expanduser("~"))
data = sorted([{
'size': os.path.getsize(get_name(get_label(i))) / 1024 / 1024,
'label': get_label(i),
'color': colors_map[get_label(i, 1)],
} for i in data], key=lambda i: i['size'])
os.chdir(root)
labels = [get_label(i, key='label') for i in data]
sizes = [i['size'] for i in data]
colors = [i['color'] for i in data]

fig, ax = plt.subplots()
ax.set_title('File Size')
ax.set_xlabel("Megabytes")
ax.barh(labels, sizes, bar_width, color=colors)
fig.set_size_inches(w=figure_width, h=figure_height)
fig.tight_layout()
fig.savefig("file-size.webp", transparent=True, format="webp")
Binary file modified benchmark/decryption-time.webp
Binary file not shown.
100 changes: 60 additions & 40 deletions benchmark/decryption.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"results": [
{
"command": "echo \"123456789\" | safelock-cli decrypt test.sla safelock_dump --quiet",
"mean": 2.591056303126667,
"stddev": 0.3072584303134709,
"median": 2.4638408214600003,
"user": 2.6658728066666666,
"system": 1.96781978,
"min": 2.36783672846,
"max": 2.94149135946,
"mean": 2.5039628573266666,
"stddev": 0.19691248469553527,
"median": 2.55461392966,
"user": 2.67419198,
"system": 1.8112809399999998,
"min": 2.28667279766,
"max": 2.6706018446599997,
"times": [
2.94149135946,
2.36783672846,
2.4638408214600003
2.55461392966,
2.6706018446599997,
2.28667279766
],
"exit_codes": [
0,
Expand All @@ -22,17 +22,17 @@
},
{
"command": "echo \"123456789\" | safelock-cli decrypt test_sha256.sla safelock_dump --quiet --sha256",
"mean": 2.1992803144599997,
"stddev": 0.4012681619295813,
"median": 2.05163752946,
"user": 2.160165473333333,
"system": 1.6608864466666666,
"min": 1.8927501014600001,
"max": 2.65345331246,
"mean": 2.2569831443266666,
"stddev": 0.30656745930113133,
"median": 2.09402087566,
"user": 2.3718176466666665,
"system": 1.7837292733333332,
"min": 2.0663134356599997,
"max": 2.61061512166,
"times": [
2.65345331246,
1.8927501014600001,
2.05163752946
2.61061512166,
2.0663134356599997,
2.09402087566
],
"exit_codes": [
0,
Expand All @@ -42,17 +42,37 @@
},
{
"command": "echo \"123456789\" | safelock-cli decrypt test_sha512.sla safelock_dump --quiet --sha512",
"mean": 2.1854930261266667,
"stddev": 0.34595596536873846,
"median": 2.12447426646,
"user": 2.5949568066666666,
"system": 1.4746104466666665,
"min": 1.8741061304600002,
"max": 2.55789868146,
"mean": 2.232521114326666,
"stddev": 0.2060331833574435,
"median": 2.2566013786599997,
"user": 2.687645313333334,
"system": 1.57915894,
"min": 2.01550591466,
"max": 2.4254560496599997,
"times": [
2.55789868146,
2.12447426646,
1.8741061304600002
2.2566013786599997,
2.01550591466,
2.4254560496599997
],
"exit_codes": [
0,
0,
0
]
},
{
"command": "7z e -y -p123456789 -mx1 test.7z -osafelock_dump",
"mean": 19.035598546326668,
"stddev": 1.0811270538832332,
"median": 18.444795690659998,
"user": 20.779736646666663,
"system": 1.4327129399999998,
"min": 18.37860840766,
"max": 20.28339154066,
"times": [
20.28339154066,
18.37860840766,
18.444795690659998
],
"exit_codes": [
0,
Expand All @@ -62,17 +82,17 @@
},
{
"command": "gpgtar -d --yes --batch --gpg-args \"--passphrase 123456789\" test.gpg",
"mean": 7.854353938126668,
"stddev": 1.707887720081897,
"median": 7.8766436114600005,
"user": 0.22827714,
"system": 1.6720551133333332,
"min": 6.1354304734600005,
"max": 9.55098772946,
"mean": 6.697602776993334,
"stddev": 0.675004732935611,
"median": 6.57798586366,
"user": 0.18233997999999998,
"system": 1.4801756066666665,
"min": 6.090402811660001,
"max": 7.42441965566,
"times": [
9.55098772946,
7.8766436114600005,
6.1354304734600005
6.090402811660001,
7.42441965566,
6.57798586366
],
"exit_codes": [
0,
Expand Down
Binary file modified benchmark/encryption-time.webp
Binary file not shown.
Loading

0 comments on commit 5ba765d

Please sign in to comment.