Skip to content

Commit ef14ade

Browse files
committed
Add Benchmark scripts
1 parent 5fe398b commit ef14ade

3 files changed

Lines changed: 156 additions & 56 deletions

File tree

crypto/aes/aesgcm-file-encrypt.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,18 @@
3434
#ifndef WOLFCRYPT_MAGIC
3535
#define WOLFCRYPT_MAGIC "WOLFSSL"
3636
#endif
37+
/*
38+
MIN_BUFFER_SIZE of less than
39+
*/
3740
#ifndef MIN_BUFFER_SIZE
3841
#define MIN_BUFFER_SIZE 1024
3942
#endif
43+
44+
/*
45+
For optimum performance, MAX_BUFFER_SIZE should be >= page_size and less than
46+
system memory in order to prevent memory fragmentation,better work with MMU
47+
memory alignments, leverage cache speedup, and minimize OS overhead.
48+
*/
4049
#ifndef MAX_BUFFER_SIZE
4150
/* Use upto 1 GByte of RAM */
4251
#define MAX_BUFFER_SIZE (1 << 30)
@@ -92,6 +101,10 @@ static size_t get_optimal_buffer_sz(int fd, size_t memory_size)
92101
optimal_size = block_size;
93102
file_size = get_file_sz(fd);
94103

104+
if (optimal_size > MAX_BUFFER_SIZE) {
105+
optimal_size = MAX_BUFFER_SIZE;
106+
}
107+
95108
while (optimal_size * 2 <= memory_size &&
96109
optimal_size * 2 <= MAX_BUFFER_SIZE &&
97110
optimal_size <= file_size) {
@@ -298,8 +311,8 @@ int encrypt_file_AesGCM(const char *in_file, const char *out_file,
298311
\param out_file file name to hold plain text
299312
\param key_str key must be 32 Bytes
300313
*/
301-
int decrypt_file_AesGCM(const char* in_file, const char* out_file,
302-
const char* key_str)
314+
int decrypt_file_AesGCM(const char *in_file, const char *out_file,
315+
const char *key_str)
303316
{
304317
byte* in_buf;
305318
byte* out_buf;
@@ -433,8 +446,9 @@ int decrypt_file_AesGCM(const char* in_file, const char* out_file,
433446
}
434447

435448
#ifdef OPENSSL_EXTRA
436-
int encrypt_file(const char* in_file, const char* out_file,
437-
const char* key_str, const char *iv_str)
449+
what
450+
int encrypt_file(const char *in_file, const char *out_file,
451+
const char *key_str, const char *iv_str)
438452
{
439453
int in_fd;
440454
int in_len;
@@ -713,15 +727,15 @@ int decrypt_file(const char *in_file, const char *out_file, const char *key_str)
713727

714728
int sanityTest_default(int file_sz) {
715729

716-
const char* cmd_enc ="./aesgcm-file-encrypt -e 256 -m 1 \
730+
const char *cmd_enc ="./aesgcm-file-encrypt -e 256 -m 1 \
717731
-k 77CF00EC060192530B5D06B6B426799B \
718732
-v 77CF00EC060192530B5D06B6B426799B \
719733
-i text.bin -o text2cipher.bin";
720-
const char* cmd_dec ="./aesgcm-file-encrypt -d 256 -m 1 \
734+
const char *cmd_dec ="./aesgcm-file-encrypt -d 256 -m 1 \
721735
-k 77CF00EC060192530B5D06B6B426799B \
722736
-i text2cipher.bin -o text2cipher2text.bin";
723-
const char* cmd_diff = "diff -q text.bin text2cipher2text.bin";
724-
char buffer[MIN_BUFFER_SIZE];
737+
const char *cmd_diff = "diff -q text.bin text2cipher2text.bin";
738+
char buffer[1024];
725739

726740
sprintf(buffer,"dd if=/dev/urandom bs=1024 count=%d | head -c %d > \
727741
text.bin", (file_sz/1024)+1, file_sz);
@@ -747,15 +761,15 @@ text.bin", (file_sz/1024)+1, file_sz);
747761
pclose(pipe);
748762

749763
#ifdef OPENSSL_EXTRA
750-
const char* cmd_enc_evp ="./aesgcm-file-encrypt -e 256 -m 1 \
764+
const char *cmd_enc_evp ="./aesgcm-file-encrypt -e 256 -m 1 \
751765
-k 77CF00EC060192530B5D06B6B426799B \
752766
-v 77CF00EC060192530B5D06B6B426799B \
753767
-i text.bin -o text2cipher.evp.bin";
754-
const char* cmd_dec_evp ="./aesgcm-file-encrypt -d 256 -m 1 \
768+
const char *cmd_dec_evp ="./aesgcm-file-encrypt -d 256 -m 1 \
755769
-k 77CF00EC060192530B5D06B6B426799B \
756770
-i text2cipher.evp.bin -o text2cipher2text.evp.bin";
757-
const char* cmd_diff_evp = "diff -q text.bin text2cipher2text.evp.bin";
758-
const char* cmd_diff_gcm_evp = "diff -q text2cipher2text.bin \
771+
const char *cmd_diff_evp = "diff -q text.bin text2cipher2text.evp.bin";
772+
const char *cmd_diff_gcm_evp = "diff -q text2cipher2text.bin \
759773
text2cipher2text.evp.bin";
760774

761775
if (system(cmd_enc_evp) != 0 || system(cmd_dec_evp) != 0 ) {
@@ -821,10 +835,10 @@ test will create three files:text.bin, cipher, decrypted plain. \n");
821835

822836
int main(int argc, char** argv)
823837
{
824-
const char* inFile = NULL;
825-
const char* ivStr = NULL;
826-
const char* keyStr = NULL;
827-
const char* outFile = NULL;
838+
const char *inFile = NULL;
839+
const char *ivStr = NULL;
840+
const char *keyStr = NULL;
841+
const char *outFile = NULL;
828842
int file_sz = 0;
829843
int key_sz = 0;
830844
int method = 0;

crypto/aes/aesgcm-file-encrypt.sh

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,71 @@
33
# set -x
44

55
echo "aesgcm-file-encrypt tests"
6+
echo "./aesgcm-file-encrypt -b to run benchmark after building wolfSSL with"
7+
echo "./configure --enable-aesgcm-stream && sudo make install"
8+
9+
# Define the output CSV file
10+
output_file="aesgcm_times.csv"
11+
output_file_header="aesgcm_times_header.csv"
12+
partition="/dev/nvme0n1p8"
613

714
keyStr=$(cat /dev/urandom | base64 | head -c 32)
815
echo "key = $keyStr"
916

1017
ivStr=$(cat /dev/urandom | base64 | head -c 16)
1118
echo "IV = $ivStr"
1219

13-
1420
if [[ "$1" == "-b" ]] || [[ "$1" == "--bench" ]]; then
21+
cp "$output_file" "$output_file.back"
22+
echo "File-size,Buffer-size,AES-256-GCM-enc,AES-256-GCM-dec" > "$output_file"
1523

16-
# Define an array of file sizes to test
17-
file_sizes=("10M" "50M" "100M" "500M" "1G")
24+
buffer_sizes=("32" "64" "128" "256" "512" "1024" "2048" "4096" "8192" "12288" "20480" "1073741824")
1825

19-
# Define the output CSV file and write the header row
20-
output_file="aesgcm_times.csv"
26+
for b_size in "${buffer_sizes[@]}"
27+
do
28+
rm aesgcm-file-encrypt text*
29+
make clean
30+
make CPPFLAGS="-DMAX_BUFFER_SIZE=$b_size -DMIN_BUFFER_SIZE=32" aesgcm-file-encrypt
2131

22-
echo "System Information:" > "$output_file"
23-
echo "--------------------" >> "$output_file"
24-
echo "Hostname: $(hostname)" >> "$output_file"
25-
echo "Kernel Version: $(uname -r)" >> "$output_file"
26-
echo "Processor Type: $(uname -m)" >> "$output_file"
27-
echo "Operating System: $(lsb_release -d | awk '{print $2,$3,$4,$5}')" >> "$output_file"
28-
echo "CPU Info: $(lscpu | grep 'Model name' | cut -d':' -f2 | sed 's/^ //')" >> "$output_file"
29-
echo "Memory Info: $(free -h | grep 'Mem' | awk '{print $2}')" >> "$output_file"
32+
# Define an array of file sizes to test
3033

31-
echo "File Size, Encryption, Decryption, Encryption EVP, Decryption EVP" > "$output_file"
34+
file_sizes=("10M" "50M" "100M" "500M" "1G")
3235

36+
echo "System Information:" > "$output_file_header"
37+
echo "--------------------" >> "$output_file_header"
38+
echo "Hostname: $(hostname)" >> "$output_file_header"
39+
echo "Kernel Version: $(uname -r)" >> "$output_file_header"
40+
echo "Processor Type: $(uname -m)" >> "$output_file_header"
41+
echo "Operating System: $(lsb_release -d | awk '{print $2,$3,$4,$5}')" >> "$output_file_header"
42+
echo "CPU Info: $(lscpu | grep 'Model name' | cut -d':' -f2 | sed 's/^ //')" >> "$output_file_header"
43+
echo "Memory Info: $(free -h | grep 'Mem' | awk '{print $2}')" >> "$output_file_header"
44+
echo "The page size is: $(sudo blockdev --getbsz $partition) bytes." >> "$output_file_header"
45+
echo "The sector size of the disk is: $(sudo blockdev --getss $partition) bytes." >> "$output_file_header"
3346

3447
# Loop over each file size and measure performance
3548
for size in "${file_sizes[@]}"
3649
do
37-
# Generate a test file of the given size
38-
text_file=text_"$size".bin
39-
dd if=/dev/urandom of=$text_file bs=$size count=1 conv=fsync
40-
41-
t_aesgcm_e="$( TIMEFORMAT="%R";time (./aesgcm-file-encrypt -e 256 -m 1 -k $keyStr -v $ivStr -i $text_file -o text2cipher.bin > /dev/null 2>&1) 2>&1 )"
42-
t_aesgcm_d="$( TIMEFORMAT="%R";time (./aesgcm-file-encrypt -d 256 -m 1 -k $keyStr -v $ivStr -i text2cipher.bin -o text2cipher2text.bin > /dev/null 2>&1) 2>&1 )"
43-
44-
diff -s $text_file text2cipher2text.bin > /dev/null
45-
if [ $? -eq 0 ]; then
46-
echo "Passed $t_aesgcm_e $t_aesgcm_d"
47-
else
48-
echo "Failed"
49-
fi
50-
51-
t_evpgcm_e="$( TIMEFORMAT="%R";time (./aesgcm-file-encrypt -e 256 -m 2 -k $keyStr -v $ivStr -i $text_file -o text2cipher.evp.bin > /dev/null 2>&1) 2>&1 )"
52-
t_evpgcm_d="$( TIMEFORMAT="%R";time (./aesgcm-file-encrypt -d 256 -m 2 -k $keyStr -v $ivStr -i text2cipher.evp.bin -o text2cipher2text.evp.bin > /dev/null 2>&1) 2>&1 )"
53-
54-
diff -s $text_file text2cipher2text.evp.bin > /dev/null
55-
if [ $? -eq 0 ]; then
56-
echo "Passed $t_evpgcm_e $t_evpgcm_d"
57-
else
58-
echo "Failed"
59-
fi
60-
61-
echo "$size, $t_aesgcm_e,$t_aesgcm_d,$t_evpgcm_e,$t_evpgcm_d" >> "$output_file"
62-
rm text*
50+
# Generate a test file of the given size
51+
text_file=text_"$size".bin
52+
dd if=/dev/urandom of=$text_file bs=$size count=1 conv=fsync
53+
54+
t_aesgcm_e="$( TIMEFORMAT="%R";time (./aesgcm-file-encrypt -e 256 -m 1 -k $keyStr -v $ivStr -i $text_file -o text2cipher.bin > /dev/null 2>&1) 2>&1 )"
55+
t_aesgcm_d="$( TIMEFORMAT="%R";time (./aesgcm-file-encrypt -d 256 -m 1 -k $keyStr -v $ivStr -i text2cipher.bin -o text2cipher2text.bin > /dev/null 2>&1) 2>&1 )"
56+
57+
diff -s $text_file text2cipher2text.bin > /dev/null
58+
if [ $? -eq 0 ]; then
59+
echo "Passed $t_aesgcm_e $t_aesgcm_d"
60+
else
61+
echo "Failed"
62+
fi
63+
echo "$size,$b_size,$t_aesgcm_e,$t_aesgcm_d" >> "$output_file"
64+
rm text*
6365
done
66+
done
6467
else
68+
rm aesgcm-file-encrypt text*
69+
make clean
70+
make CPPFLAGS="-DMAX_BUFFER_SIZE=4096" aesgcm-file-encrypt
6571
# test a smaller file size (default option)
6672
dd if=/dev/urandom bs=1024 count=1 | head -c 1022 > text.bin
6773
res=$(./aesgcm-file-encrypt -e 256 -m 1 -k $keyStr -v $ivStr -i text.bin -o text2cipher.bin )
@@ -78,4 +84,21 @@ else
7884
rm text*
7985
fi
8086

87+
if true; then
88+
input_file="$output_file"
89+
output_file_sorted="data.csv"
90+
91+
awk -F, 'function to_bytes(x) {
92+
split(x, a, /[^0-9.]+/);
93+
return a[1] * (index("KMGT", substr(x, length(a[1]) + 1)) * 1024 ^ (index("KMGT", substr(x, length(a[1]) + 1)) - 1));
94+
}
95+
NR == 1 { gsub(",", " "); print $0; next }
96+
{ line = $1; for (i = 2; i <= NF; i++) line = line "," $i; print to_bytes($1) "," line }' "$input_file" |
97+
sort -t, -k2,2n -k3,3n | awk -F, 'NR == 1 { print $0 } NR > 1 { $1=""; $0=substr($0, 2); print }' > "$output_file_sorted"
98+
99+
sed -i 's/1073741824/1G/g' "$output_file_sorted"
100+
./plot_data.gp
101+
fi
102+
103+
81104
exit 0

crypto/aes/plot_data.gp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/gnuplot
2+
3+
# Function to get the number of lines in the file
4+
filelines(file) = system(sprintf("awk 'END {print NR}' %s", file))
5+
6+
# Get the number of lines in the data file
7+
datafile = "data.csv"
8+
nlines = int(filelines(datafile)) - 1
9+
# you can adjust the number of rows you want to ply
10+
chunk_size = 12
11+
12+
# Set the terminal type
13+
set term png
14+
15+
# Set the title, style, and key settings
16+
set title "wolfSSL"
17+
set style fill solid 1.00 border lt -1
18+
set style histogram clustered gap 4 title textcolor lt -1
19+
set datafile missing '-'
20+
set style data histograms
21+
22+
# Set x-axis settings
23+
set xtics border in scale 0,0 nomirror rotate by -45 autojustify
24+
set xtics norangelimit
25+
set xtics ()
26+
set format y '%.0s%c'
27+
28+
set lmargin at screen 0.15
29+
set rmargin at screen 0.90
30+
31+
#set bmargin at screen 0.10
32+
set tmargin at screen 0.90
33+
34+
# Set legend position
35+
#set key outside below
36+
set key autotitle columnhead noenhanced
37+
38+
# Set grid settings
39+
set grid mxtics mytics
40+
set grid x y
41+
42+
# Set titles for x and y axes
43+
set xlabel "FileSize-BufferSize (Bytes)"
44+
set ylabel "Total time (Seconds)"
45+
46+
# Uncomment and set xrange and yrange if necessary
47+
# set xrange [lower:upper]
48+
# set yrange [lower:upper]
49+
50+
# Loop through the data file, plotting 12 rows at a time
51+
52+
do for [i=0:int((nlines-1)/chunk_size)] {
53+
# Set the output file name
54+
set output sprintf("output_%03d.png", i + 1)
55+
56+
# Calculate start and end rows for the current chunk
57+
start = 0 + i * chunk_size
58+
end = start + chunk_size - 1
59+
60+
# Plot the data from the CSV file for the current chunk
61+
plot for [col=3:*] for [filename in datafile] filename using col:xticlabels(strcol(1)."-".strcol(2)) every ::start::end with linespoints
62+
}
63+

0 commit comments

Comments
 (0)