Skip to content

Commit 89ee145

Browse files
committed
fix ecc_speedtest.py decoding and add decoding only mode + bump v3.0.7
Signed-off-by: Stephen L. <lrq3000@gmail.com>
1 parent 23b8f6f commit 89ee145

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

pyFileFixity/_infos.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"author": "Stephen Larroque",
33
"email": "LRQ3000@gmail.com",
4-
"version": "3.0.6"
4+
"version": "3.0.7"
55
}

pyFileFixity/ecc_speedtest.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
sys.path.append(os.path.join(thispathname))
3232

3333
# ECC and hashing facade libraries
34-
from lib._compat import _range
34+
from lib._compat import _range, _bytes
3535
from lib.aux_funcs import sizeof_fmt
3636
from lib.eccman import ECCMan, compute_ecc_params
3737
from lib.hasher import Hasher
@@ -70,7 +70,7 @@ def main(argv=None):
7070
msg_nb = 1000000
7171
tamper_rate = 0.4 # tamper rate is relative to the number of ecc bytes, not the whole message (not like the resilience_rate)
7272
tamper_mode = 'noise' # noise or erasure
73-
no_decoding = True
73+
no_decoding = False # True for only encoding, False for both encoding then decoding, 3 for only decoding (which includes an encoding step)
7474
subchunking = False
7575
subchunk_size = 50
7676

@@ -86,33 +86,37 @@ def main(argv=None):
8686
print("ECC Speed Test, started on %s" % datetime.datetime.now().isoformat())
8787
print("====================================")
8888
print("ECC algorithm: %i." % ecc_algo)
89+
print("%s." % ("Only encoding test" if no_decoding is True else ("Encoding and decoding test" if no_decoding is False else "Decoding test only (including an encoding step)")))
8990

9091
# -- Encoding test
9192
# IMPORTANT: we do NOT check the correctness of encoding, only the speed! It's up to you to verify that you are computing the ecc correctly.
92-
total_time = 0
93-
total_size = msg_nb*max_block_size
94-
bardisp = tqdm.tqdm(total=total_size, leave=True, desc='ENC', unit='B', unit_scale=True, ncols=79, mininterval=0.5) # display progress bar based on the number of bytes encoded
95-
k = ecc_params["message_size"]
96-
# Generate a random string and encode it
97-
for msg in gen_random_string(msg_nb, k):
98-
start = time.process_time() # time.clock() was dropped in Py3.8, use time.perf_counter() instead to time performance including sleep, or time.process_time() without sleep periods.
99-
if subchunking:
100-
for i in xrange(0, len(msg), subchunk_size):
101-
ecc_manager_subchunk.encode(msg[i:i+subchunk_size])
102-
else:
103-
ecc_manager.encode(msg)
104-
total_time += time.process_time() - start
105-
bardisp.update(max_block_size)
106-
bardisp.close()
107-
print("Encoding: total time elapsed: %f sec for %s of data. Real Speed (only encoding, no other computation): %s." % (total_time, format_sizeof(total_size, 'B'), format_sizeof(total_size/total_time, 'B/sec') ))
93+
if not no_decoding == 3:
94+
total_time = 0
95+
total_size = msg_nb*max_block_size
96+
bardisp = tqdm.tqdm(total=total_size, leave=True, desc='ENC', unit='B', unit_scale=True, ncols=79, mininterval=0.5) # display progress bar based on the number of bytes encoded
97+
k = ecc_params["message_size"]
98+
# Generate a random string and encode it
99+
for msg in gen_random_string(msg_nb, k):
100+
start = time.process_time() # time.clock() was dropped in Py3.8, use time.perf_counter() instead to time performance including sleep, or time.process_time() without sleep periods.
101+
if subchunking:
102+
for i in xrange(0, len(msg), subchunk_size):
103+
ecc_manager_subchunk.encode(msg[i:i+subchunk_size])
104+
else:
105+
ecc_manager.encode(msg)
106+
total_time += time.process_time() - start
107+
bardisp.update(max_block_size)
108+
bardisp.close()
109+
print("Encoding: total time elapsed: %f sec for %s of data. Real Speed (only encoding, no other computation): %s." % (total_time, format_sizeof(total_size, 'B'), format_sizeof(total_size/total_time, 'B/sec') ))
108110

109111
# -- Decoding test
110-
if not no_decoding:
112+
if not no_decoding or no_decoding == 3:
111113
total_time = 0
112114
total_size = msg_nb*max_block_size
113-
bardisp = tqdm.tqdm(total=total_size, leave=True, desc='ENC', unit='B', unit_scale=True) # display progress bar based on the number of bytes encoded
115+
bardisp = tqdm.tqdm(total=total_size*2, leave=True, desc='DEC', unit='B', unit_scale=True) # display progress bar based on the number of bytes encoded
114116
# Generate a random string and encode it
115117
for msg in gen_random_string(msg_nb, ecc_params["message_size"]):
118+
# Make it into a bytearray first
119+
msg = bytearray(msg, 'utf-8')
116120
# Computing the ecc first
117121
ecc = ecc_manager.encode(msg)
118122

@@ -128,8 +132,8 @@ def main(argv=None):
128132
elif tamper_mode == 'e' or tamper_mode == 'erasure': # Erase the character (set a null byte)
129133
msg_tampered[pos] = 0
130134
# Convert back to a string
131-
msg_tampered = str(msg_tampered)
132-
ecc = str(ecc)
135+
msg_tampered = _bytes(msg_tampered)
136+
ecc = _bytes(ecc)
133137

134138
# Decode the tampered message with ecc
135139
start = time.process_time()
@@ -141,7 +145,7 @@ def main(argv=None):
141145
print("Warning, there was an error while decoding. Please check your parameters (tamper_rate not too high) or the decoding procedure.")
142146
pass
143147
total_time += time.process_time() - start
144-
bardisp.update(max_block_size)
148+
bardisp.update(max_block_size*2) # update x2 because we encode AND decode
145149
bardisp.close()
146150
print("Decoding: total time elapsed: %f sec for %s of data. Real Speed (only decoding, no other computation): %s." % (total_time, format_sizeof(total_size, 'B'), format_sizeof(total_size/total_time, 'B/sec') ))
147151

0 commit comments

Comments
 (0)