Skip to content

Commit 5ba765d

Browse files
authored
Merge pull request #12 from mrf345/testing
Add file size plot and `7z` to plots
2 parents fcc9ef9 + f560145 commit 5ba765d

8 files changed

+203
-130
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ go.work.sum
2525
.env
2626
*.sla
2727
safelock-cli
28+
vendor

README.md

+7-15
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,19 @@ You can find interactive examples of using it as a package to [encrypt](https://
5050

5151
### Performance
5252

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

5555
> [!NOTE]
56-
> 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))
56+
> 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))
5757
5858
<p align="center">
5959
<a href="https://raw.githubusercontent.com/mrf345/safelock-cli/master/benchmark/encryption-time.webp" target="_blank">
60-
<img src="benchmark/encryption-time.webp" align="center" alt="encryption time" />
60+
<img src="benchmark/encryption-time.webp" alt="encryption time" />
6161
</a>
6262
<a href="https://raw.githubusercontent.com/mrf345/safelock-cli/master/benchmark/decryption-time.webp" target="_blank">
63-
<img src="benchmark/decryption-time.webp" align="center" alt="encryption time" />
63+
<img src="benchmark/decryption-time.webp" alt="decryption time" />
64+
</a>
65+
<a href="https://raw.githubusercontent.com/mrf345/safelock-cli/master/benchmark/file-size.webp" target="_blank">
66+
<img src="benchmark/file-size.webp" alt="file size" />
6467
</a>
6568
</p>
66-
67-
And you could gain a slight file size reduction
68-
69-
```shell
70-
> du -hs test/
71-
1.2G test/
72-
73-
> ls -lh --block-size=MB test.sla test.gpg
74-
-rw-r--r-- 1 mrf3 mrf3 1.2G Sep 3 17:55 test.gpg
75-
-rw-r--r-- 1 mrf3 mrf3 959M Sep 3 17:29 test.sla
76-
```

benchmark/bench_and_plot.py

+71-31
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,53 @@
88
safelock_cmd = "safelock-cli"
99
pwd = "123456789"
1010
rest = "60s"
11-
input_path = "test"
11+
input_path = "~/Videos"
1212
output_name = "test"
1313
output_dir = "safelock_dump"
1414
runs = 3
1515
figure_width = 14
16-
figure_height = 2.5
16+
figure_height = 3
1717
bar_width = 0.6
1818
measure = "Seconds"
1919
root = os.getcwd()
2020

21+
def get_label(i, clean=False, key="command"):
22+
matchers = [
23+
('gpg', 'gpgtar',),
24+
('7z', '7zip (fastest)',),
25+
('256', 'safelock --sha256',),
26+
('512', 'safelock --sha512',),
27+
('safelock', 'safelock',),
28+
]
29+
label = next((v for m, v in matchers if m in i[key]))
30+
31+
if clean:
32+
return label
33+
if key == "label":
34+
return f"{label}\n{i['size']:.2f} MB"
35+
36+
return f"{label}\n{i['median']:.3f}s"
37+
38+
def get_name(i):
39+
matchers = [
40+
('gpg', f'{output_name}.gpg',),
41+
('7z', f'{output_name}.7z',),
42+
('256', f'{output_name}_sha256.sla',),
43+
('512', f'{output_name}_sha512.sla',),
44+
('safelock', f'{output_name}.sla',),
45+
]
46+
47+
return next((v for m, v in matchers if m in i))
48+
2149
def encrypt():
2250
err = os.system(
2351
f"hyperfine --runs {runs} --prepare "
2452
f"'sleep {rest}' "
25-
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {output_name}.sla --quiet' "
26-
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {output_name}_sha256.sla --quiet --sha256' "
27-
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {output_name}_sha512.sla --quiet --sha512' "
28-
f"'gpgtar -e -o test.gpg -c --yes --batch --gpg-args \"--passphrase {pwd}\" Videos/' "
53+
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('safelock')} --quiet' "
54+
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('256')} --quiet --sha256' "
55+
f"'echo \"{pwd}\" | {safelock_cmd} encrypt {input_path} {get_name('512')} --quiet --sha512' "
56+
f"'7z a -p{pwd} -mx1 {get_name('7z')} {input_path}' "
57+
f"'gpgtar -e -o {get_name('gpg')} -c --yes --batch --gpg-args \"--passphrase {pwd}\" Videos/' "
2958
f"--export-json {root}/encryption.json"
3059
)
3160

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

4676
if err:
4777
exit(err)
4878

49-
def get_label(i, clean=False):
50-
label = i['command']
51-
52-
if 'gpg' in label:
53-
label = 'gpgtar'
54-
elif 'sha256' in label:
55-
label = 'safelock --sha256'
56-
elif 'sha512' in label:
57-
label = 'safelock --sha512'
58-
else:
59-
label = 'safelock'
60-
61-
if clean:
62-
return label
63-
64-
return f"{label}\n{i['median']:.3f}s"
65-
66-
# os.chdir(os.path.expanduser("~"))
79+
os.chdir(os.path.expanduser("~"))
6780
# encrypt()
6881
# decrypt()
6982
os.chdir(root)
83+
plt.margins(3.5)
84+
85+
86+
# Encryption Time Plot
7087

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

78-
plt.margins(3.5)
79-
8095
fig, ax = plt.subplots()
8196
ax.set_title('Encryption Time')
8297
ax.set_xlabel(measure)
@@ -85,6 +100,9 @@ def get_label(i, clean=False):
85100
fig.tight_layout()
86101
fig.savefig("encryption-time.webp", transparent=True, format="webp")
87102

103+
104+
# Decryption Time Plot
105+
88106
with open("decryption.json") as f:
89107
data = sorted(json.load(f)['results'], key=lambda i: i['median'])
90108
labels = [get_label(i) for i in data]
@@ -98,3 +116,25 @@ def get_label(i, clean=False):
98116
fig.set_size_inches(w=figure_width, h=figure_height)
99117
fig.tight_layout()
100118
fig.savefig("decryption-time.webp", transparent=True, format="webp")
119+
120+
121+
# File Sizes Plot
122+
123+
os.chdir(os.path.expanduser("~"))
124+
data = sorted([{
125+
'size': os.path.getsize(get_name(get_label(i))) / 1024 / 1024,
126+
'label': get_label(i),
127+
'color': colors_map[get_label(i, 1)],
128+
} for i in data], key=lambda i: i['size'])
129+
os.chdir(root)
130+
labels = [get_label(i, key='label') for i in data]
131+
sizes = [i['size'] for i in data]
132+
colors = [i['color'] for i in data]
133+
134+
fig, ax = plt.subplots()
135+
ax.set_title('File Size')
136+
ax.set_xlabel("Megabytes")
137+
ax.barh(labels, sizes, bar_width, color=colors)
138+
fig.set_size_inches(w=figure_width, h=figure_height)
139+
fig.tight_layout()
140+
fig.savefig("file-size.webp", transparent=True, format="webp")

benchmark/decryption-time.webp

1.1 KB
Binary file not shown.

benchmark/decryption.json

+60-40
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
"results": [
33
{
44
"command": "echo \"123456789\" | safelock-cli decrypt test.sla safelock_dump --quiet",
5-
"mean": 2.591056303126667,
6-
"stddev": 0.3072584303134709,
7-
"median": 2.4638408214600003,
8-
"user": 2.6658728066666666,
9-
"system": 1.96781978,
10-
"min": 2.36783672846,
11-
"max": 2.94149135946,
5+
"mean": 2.5039628573266666,
6+
"stddev": 0.19691248469553527,
7+
"median": 2.55461392966,
8+
"user": 2.67419198,
9+
"system": 1.8112809399999998,
10+
"min": 2.28667279766,
11+
"max": 2.6706018446599997,
1212
"times": [
13-
2.94149135946,
14-
2.36783672846,
15-
2.4638408214600003
13+
2.55461392966,
14+
2.6706018446599997,
15+
2.28667279766
1616
],
1717
"exit_codes": [
1818
0,
@@ -22,17 +22,17 @@
2222
},
2323
{
2424
"command": "echo \"123456789\" | safelock-cli decrypt test_sha256.sla safelock_dump --quiet --sha256",
25-
"mean": 2.1992803144599997,
26-
"stddev": 0.4012681619295813,
27-
"median": 2.05163752946,
28-
"user": 2.160165473333333,
29-
"system": 1.6608864466666666,
30-
"min": 1.8927501014600001,
31-
"max": 2.65345331246,
25+
"mean": 2.2569831443266666,
26+
"stddev": 0.30656745930113133,
27+
"median": 2.09402087566,
28+
"user": 2.3718176466666665,
29+
"system": 1.7837292733333332,
30+
"min": 2.0663134356599997,
31+
"max": 2.61061512166,
3232
"times": [
33-
2.65345331246,
34-
1.8927501014600001,
35-
2.05163752946
33+
2.61061512166,
34+
2.0663134356599997,
35+
2.09402087566
3636
],
3737
"exit_codes": [
3838
0,
@@ -42,17 +42,37 @@
4242
},
4343
{
4444
"command": "echo \"123456789\" | safelock-cli decrypt test_sha512.sla safelock_dump --quiet --sha512",
45-
"mean": 2.1854930261266667,
46-
"stddev": 0.34595596536873846,
47-
"median": 2.12447426646,
48-
"user": 2.5949568066666666,
49-
"system": 1.4746104466666665,
50-
"min": 1.8741061304600002,
51-
"max": 2.55789868146,
45+
"mean": 2.232521114326666,
46+
"stddev": 0.2060331833574435,
47+
"median": 2.2566013786599997,
48+
"user": 2.687645313333334,
49+
"system": 1.57915894,
50+
"min": 2.01550591466,
51+
"max": 2.4254560496599997,
5252
"times": [
53-
2.55789868146,
54-
2.12447426646,
55-
1.8741061304600002
53+
2.2566013786599997,
54+
2.01550591466,
55+
2.4254560496599997
56+
],
57+
"exit_codes": [
58+
0,
59+
0,
60+
0
61+
]
62+
},
63+
{
64+
"command": "7z e -y -p123456789 -mx1 test.7z -osafelock_dump",
65+
"mean": 19.035598546326668,
66+
"stddev": 1.0811270538832332,
67+
"median": 18.444795690659998,
68+
"user": 20.779736646666663,
69+
"system": 1.4327129399999998,
70+
"min": 18.37860840766,
71+
"max": 20.28339154066,
72+
"times": [
73+
20.28339154066,
74+
18.37860840766,
75+
18.444795690659998
5676
],
5777
"exit_codes": [
5878
0,
@@ -62,17 +82,17 @@
6282
},
6383
{
6484
"command": "gpgtar -d --yes --batch --gpg-args \"--passphrase 123456789\" test.gpg",
65-
"mean": 7.854353938126668,
66-
"stddev": 1.707887720081897,
67-
"median": 7.8766436114600005,
68-
"user": 0.22827714,
69-
"system": 1.6720551133333332,
70-
"min": 6.1354304734600005,
71-
"max": 9.55098772946,
85+
"mean": 6.697602776993334,
86+
"stddev": 0.675004732935611,
87+
"median": 6.57798586366,
88+
"user": 0.18233997999999998,
89+
"system": 1.4801756066666665,
90+
"min": 6.090402811660001,
91+
"max": 7.42441965566,
7292
"times": [
73-
9.55098772946,
74-
7.8766436114600005,
75-
6.1354304734600005
93+
6.090402811660001,
94+
7.42441965566,
95+
6.57798586366
7696
],
7797
"exit_codes": [
7898
0,

benchmark/encryption-time.webp

1.14 KB
Binary file not shown.

0 commit comments

Comments
 (0)