summaryrefslogtreecommitdiff
path: root/src/python/test/pyeclib_test.py
blob: bc69a23c00e678b510f4d752ec8e6c03d983b961 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Copyright (c) 2013, Kevin Greenan (kmgreen2@gmail.com)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.  THIS SOFTWARE IS
# PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import pyeclib_c
import time
import random
import sys
import os
import codecs

from string import ascii_letters


class Timer:

    def __init__(self):
        self.start_time = 0
        self.end_time = 0

    def start(self):
        self.start_time = time.time()

    def stop(self):
        self.end_time = time.time()

    def curr_delta(self):
        return self.end_time - self.start_time

    def stop_and_return(self):
        self.end_time = time.time()
        return self.curr_delta()


def setup(file_sizes):
    if not os.path.isdir("test_files"):
        os.mkdir("./test_files")

    for size_str in file_sizes:

        size_desc = size_str.split("-")

        size = int(size_desc[0])

        if (size_desc[1] == 'M'):
            size *= 1000000
        elif (size_desc[1] == 'K'):
            size *= 1000

        testdata = ''.join(random.choice(ascii_letters) for i in range(size))
        testdata_bytes = testdata.encode('utf-8')

        filename = "test_file.%s" % size_str
        with open(("test_files/%s" % filename), "wb") as fp:
            fp.write(testdata_bytes)


def cleanup(file_sizes):
    for size_str in file_sizes:
        filename = "test_files/test_file.%s" % size_str
        os.unlink(filename)
    os.rmdir("./test_files")


def time_encode(num_data, num_parity, w, ec_type, file_size, iterations):
    timer = Timer()
    tsum = 0
    handle = pyeclib_c.init(num_data, num_parity, w, ec_type)

    filename = "test_file.%s" % file_size
    with open("test_files/%s" % filename, "rb") as fp:
        whole_file_bytes = fp.read()

    timer.start()
    for l in range(iterations):
        fragments = pyeclib_c.encode(handle, whole_file_bytes)
    tsum = timer.stop_and_return()

    return tsum / iterations


def time_decode(num_data, num_parity, w, ec_type, file_size, iterations, hd):
    timer = Timer()
    tsum = 0
    handle = pyeclib_c.init(num_data, num_parity, w, ec_type)

    filename = "test_file.%s" % file_size
    with open("test_files/%s" % filename, "rb") as fp:
        whole_file_bytes = fp.read()

    fragments = pyeclib_c.encode(handle, whole_file_bytes)

    orig_fragments = fragments[:]

    for i in range(iterations):
        missing_idxs = []
        num_missing = hd - 1
        for j in range(num_missing):
            idx = random.randint(0, (num_data + num_parity) - 1)
            while idx in missing_idxs:
                idx = random.randint(0, (num_data + num_parity) - 1)
            missing_idxs.append(idx)
            fragments[idx] = b'\0' * len(fragments[0])

        timer.start()
        decoded_fragments = pyeclib_c.decode(
            handle, fragments[
                :num_data], fragments[
                num_data:], missing_idxs, len(
                fragments[0]))
        tsum += timer.stop_and_return()

        fragments = decoded_fragments
    
        for j in range(num_data + num_parity):
            if orig_fragments[j] != decoded_fragments[j]:
                with open("orig_fragments", "wb") as fd_orig:
                    fd_orig.write(orig_fragments[j])

                with open("decoded_fragments", "wb") as fd_decoded:
                    fd_decoded.write(decoded_fragments[j])

                print(("Fragment %d was not reconstructed!!!" % j))
                sys.exit(2)

        decoded_fragments = None

    return tsum / iterations


def test_reconstruct(num_data, num_parity, w, ec_type, file_size, iterations):
    timer = Timer()
    tsum = 0
    handle = pyeclib_c.init(num_data, num_parity, w, ec_type)

    filename = "test_file.%s" % file_size
    with open("test_files/%s" % filename, "rb") as fp:
        whole_file_bytes = fp.read()

    orig_fragments = pyeclib_c.encode(handle, whole_file_bytes)

    for i in range(iterations):
        fragments = orig_fragments[:]
        num_missing = 1
        missing_idxs = []
        for j in range(num_missing):
            idx = random.randint(0, (num_data + num_parity) - 1)
            while idx in missing_idxs:
                idx = random.randint(0, (num_data + num_parity) - 1)
            missing_idxs.append(idx)
            fragments[idx] = b'\0' * len(fragments[0])

        timer.start()
        reconstructed_fragment = pyeclib_c.reconstruct(
            handle, fragments[
                :num_data], fragments[
                num_data:], missing_idxs, missing_idxs[0], len(
                fragments[0]))

        tsum += timer.stop_and_return()

        if orig_fragments[missing_idxs[0]] != reconstructed_fragment:
            with open("orig_fragments", "wb") as fd_orig:
                fd_orig.write(orig_fragments[missing_idxs[0]])
            with open("decoded_fragments", "wb") as fd_decoded:
                fd_decoded.write(reconstructed_fragment)
            print(("Fragment %d was not reconstructed!!!" % missing_idxs[0]))
            sys.exit(2)

    return tsum / iterations


def test_get_fragment_partition(
        num_data, num_parity, w, ec_type, file_size, iterations):
    handle = pyeclib_c.init(num_data, num_parity, w, ec_type)

    filename = "test_file.%s" % file_size
    with open("test_files/%s" % filename, "rb") as fp:
        whole_file_bytes = fp.read()

    fragments = pyeclib_c.encode(handle, whole_file_bytes)
    # print(fragments)

    for i in range(iterations):
        missing_fragments = random.sample(fragments, 3)
        avail_fragments = fragments[:]
        missing_fragment_idxs = []
        for missing_frag in missing_fragments:
            missing_fragment_idxs.append(fragments.index(missing_frag))
            avail_fragments.remove(missing_frag)

        (data_frags, parity_frags, missing_idxs) =\
            pyeclib_c.get_fragment_partition(handle, avail_fragments)

        missing_fragment_idxs.sort()
        missing_idxs.sort()

        if missing_fragment_idxs != missing_idxs:
            print(("Missing idx mismatch in test_get_fragment_partition: "
                   "%s != %s\n" % (missing_fragment_idxs, missing_idxs)))
            sys.exit()

        decoded_fragments = pyeclib_c.decode(
            handle, data_frags, parity_frags, missing_idxs, len(
                data_frags[0]))


def test_fragments_to_string(num_data, num_parity, w, ec_type, file_size):
    handle = pyeclib_c.init(num_data, num_parity, w, ec_type)

    filename = "test_file.%s" % file_size
    with open(("test_files/%s" % filename), "rb") as fp:
        whole_file_bytes = fp.read()

    fragments = pyeclib_c.encode(handle, whole_file_bytes)

    concat_str = pyeclib_c.fragments_to_string(handle, fragments[:num_data])

    if concat_str != whole_file_bytes:
        print(("String does not equal the original string "
               "(len(orig) = %d, len(new) = %d\n" %
               (len(whole_file_bytes), len(concat_str))))


def test_get_required_fragments(num_data, num_parity, w, ec_type):
    handle = pyeclib_c.init(num_data, num_parity, w, ec_type)

    #
    # MDS codes need any k fragments
    #
    # if ec_type in ["rs_vand_isa_l", "rs_vand", "rs_cauchy_orig"]:
    if ec_type in ["rs_vand_isa_l"]:
        expected_fragments = [i for i in range(num_data + num_parity)]
        missing_fragments = []

        #
        # Remove between 1 and num_parity
        #
        for i in range(random.randint(0, num_parity - 1)):
            missing_fragment = random.sample(expected_fragments, 1)[0]
            missing_fragments.append(missing_fragment)
            expected_fragments.remove(missing_fragment)

        expected_fragments = expected_fragments[:num_data]
        required_fragments = pyeclib_c.get_required_fragments(
            handle,
            missing_fragments)

        if expected_fragments != required_fragments:
            print(("Unexpected required fragments list "
                   "(exp != req): %s != %s" %
                   (expected_fragments, required_fragments)))
            sys.exit(2)


def get_throughput(avg_time, size_str):
    size_desc = size_str.split("-")

    size = float(size_desc[0])

    if (size_desc[1] == 'M'):
        throughput = size / avg_time
    elif (size_desc[1] == 'K'):
        throughput = (size / 1000.0) / avg_time

    return (format(throughput, '.10g'))

#num_datas = [12, 12, 12]
#num_parities = [2, 3, 4]
num_datas = [12, 12, 12]
num_parities = [3, 4, 4]
iterations = 100

# rs_types = [("rs_vand", 16), ("rs_vand_isa_l", 16), ("rs_cauchy_orig", 4)]
rs_types = [("rs_vand_isa_l", 8)]
#xor_types = [("flat_xor_4", 12, 6, 4), (
#    "flat_xor_4", 10, 5, 4), ("flat_xor_3", 10, 5, 3)]
xor_types = []

sizes = ["101-K", "202-K", "303-K"]

setup(sizes)

for (ec_type, k, m, hd) in xor_types:
    print(("\nRunning tests for %s k=%d, m=%d\n" % (ec_type, k, m)))

    type_str = "%s" % (ec_type)

    for size_str in sizes:
        avg_time = time_encode(k, m, 32, type_str, size_str, iterations)
        print("Encode (%s): %s" %
             (size_str, get_throughput(avg_time, size_str)))

    for size_str in sizes:
        avg_time = time_decode(k, m, 32, type_str, size_str, iterations, 3)
        print("Decode (%s): %s" %
              (size_str, get_throughput(avg_time, size_str)))

    for size_str in sizes:
        avg_time = test_reconstruct(k, m, 32, type_str, size_str, iterations)
        print("Reconstruct (%s): %s" %
              (size_str, get_throughput(avg_time, size_str)))

for (ec_type, w) in rs_types:
    print(("\nRunning tests for %s w=%d\n" % (ec_type, w)))

    for i in range(len(num_datas)):
        for size_str in sizes:
            print("%d encode\n" % i)
            test_get_fragment_partition(
                num_datas[i], num_parities[i], w, ec_type, size_str, iterations)

    print("1 encode\n")
    for i in range(len(num_datas)):
        for size_str in sizes:
            test_fragments_to_string(
                num_datas[i], num_parities[i], w, ec_type, size_str)

    print("2 encode\n")
    for i in range(len(num_datas)):
        test_get_required_fragments(num_datas[i], num_parities[i], w, ec_type)

    print("3 encode\n")
    for i in range(len(num_datas)):
        for size_str in sizes:
            avg_time = time_encode(
                num_datas[i], num_parities[i], w, ec_type, size_str, iterations)
            print(("Encode (%s): %s" %
                  (size_str, get_throughput(avg_time, size_str))))

    for i in range(len(num_datas)):
        for size_str in sizes:
            avg_time = time_decode(
                num_datas[i], num_parities[i], w, ec_type, size_str, iterations,
                num_parities[i] + 1)
            print(("Decode (%s): %s" %
                  (size_str, get_throughput(avg_time, size_str))))

    for i in range(len(num_datas)):
        for size_str in sizes:
            avg_time = test_reconstruct(
                num_datas[i], num_parities[i], w, ec_type, size_str, iterations)
            print(("Reconstruct (%s): %s" %
                  (size_str, get_throughput(avg_time, size_str))))


cleanup(sizes)