File: stress-test3.py

package info (click to toggle)
pytables 3.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,284 kB
  • sloc: ansic: 82,216; python: 65,570; cpp: 753; sh: 394; makefile: 106
file content (243 lines) | stat: -rw-r--r-- 7,052 bytes parent folder | download
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
#!/usr/bin/env python3

"""This script allows to create arbitrarily large files with the desired
combination of groups, tables per group and rows per table.

Issue "python stress-test3.py" without parameters for a help on usage.

"""

import gc
import sys
from time import perf_counter as clock
from time import process_time as cpuclock

import tables as tb


class Test(tb.IsDescription):
    ngroup = tb.Int32Col(pos=1)
    ntable = tb.Int32Col(pos=2)
    nrow = tb.Int32Col(pos=3)
    float_ = tb.Float32Col(pos=3)
    # string = tb.StringCol(500, pos=4)


def create_file(
    filename, ngroups, ntables, nrows, complevel, complib, recsize
):

    # First, create the groups

    # Open a file in "w"rite mode
    fileh = tb.open_file(filename, mode="w", title="PyTables Stress Test")

    for k in range(ngroups):
        # Create the group
        group = fileh.create_group("/", "group%04d" % k, "Group %d" % k)

    fileh.close()

    # Now, create the tables
    rowswritten = 0
    rowsize = 0
    for k in range(ngroups):
        fileh = tb.open_file(filename, mode="a", root_uep="group%04d" % k)
        # Get the group
        group = fileh.root
        for j in range(ntables):
            # Create a table
            table = fileh.create_table(
                group,
                "table%04d" % j,
                Test,
                "Table%04d" % j,
                tb.Filters(complevel, complib),
                nrows,
            )
            rowsize = table.rowsize
            # Get the row object associated with the new table
            row = table.row
            # Fill the table
            for i in range(nrows):
                row["ngroup"] = k
                row["ntable"] = j
                row["nrow"] = i
                row.append()

            rowswritten += nrows
            table.flush()

        # Close the file
        fileh.close()

    return (rowswritten, rowsize)


def read_file(filename, ngroups, recsize, verbose):
    # Open the HDF5 file in read-only mode

    rowsread = 0
    rowsize = 0
    buffersize = 0
    for ngroup in range(ngroups):
        fileh = tb.open_file(filename, mode="r")
        # Get the group
        group = fileh.get_node(fileh.root, "group%04d" % ngroup)
        ntable = 0
        if verbose:
            print("Group ==>", group)
        for table in fileh.list_nodes(group, "Leaf"):
            rowsize = table.rowsize
            buffersize = table.rowsize * table.nrowsinbuf
            if verbose > 1:
                print("Table ==>", table)
                print("Max rows in buf:", table.nrowsinbuf)
                print("Rows in", table._v_pathname, ":", table.nrows)
                print("Buffersize:", table.rowsize * table.nrowsinbuf)
                print("MaxTuples:", table.nrowsinbuf)

            nrow = 0
            for row in table:
                try:
                    assert row["ngroup"] == ngroup
                    assert row["ntable"] == ntable
                    assert row["nrow"] == nrow
                except Exception:
                    print(
                        "Error in group: %d, table: %d, row: %d"
                        % (ngroup, ntable, nrow)
                    )
                    print("Record ==>", row)
                nrow += 1

            assert nrow == table.nrows
            rowsread += table.nrows
            ntable += 1

        # Close the file (eventually destroy the extended type)
        fileh.close()

    return (rowsread, rowsize, buffersize)


def dump_garbage():
    """show us what the garbage is about."""
    # Force collection
    print("\nGARBAGE:")
    gc.collect()

    print("\nGARBAGE OBJECTS:")
    for x in gc.garbage:
        s = str(x)
        # if len(s) > 80: s = s[:77] + "..."
        print(type(x), "\n   ", s)


if __name__ == "__main__":
    import getopt

    usage = """usage: %s [-d debug] [-v level] [-p] [-r] [-w] [-l complib] [-c complevel] [-g ngroups] [-t ntables] [-i nrows] file
    -d debugging level
    -v verbosity level
    -r only read test
    -w only write test
    -l sets the compression library to be used ("zlib", "bzip2", "blosc", "blosc:codec")
    -c sets a compression level (do not set it or 0 for no compression)
    -g number of groups hanging from "/"
    -t number of tables per group
    -i number of rows per table
"""

    try:
        opts, pargs = getopt.getopt(sys.argv[1:], "d:v:parwl:c:g:t:i:")
    except Exception:
        sys.stderr.write(usage)
        sys.exit(0)

    # if we pass too much parameters, abort
    if len(pargs) != 1:
        sys.stderr.write(usage)
        sys.exit(0)

    # default options
    ngroups = 5
    ntables = 5
    nrows = 100
    verbose = 0
    debug = 0
    recsize = "medium"
    testread = 1
    testwrite = 1
    complevel = 0
    complib = "zlib"

    # Get the options
    for option in opts:
        if option[0] == "-d":
            debug = int(option[1])
        if option[0] == "-v":
            verbose = int(option[1])
        elif option[0] == "-r":
            testwrite = 0
        elif option[0] == "-w":
            testread = 0
        elif option[0] == "-l":
            complib = option[1]
        elif option[0] == "-c":
            complevel = int(option[1])
        elif option[0] == "-g":
            ngroups = int(option[1])
        elif option[0] == "-t":
            ntables = int(option[1])
        elif option[0] == "-i":
            nrows = int(option[1])

    if debug:
        gc.enable()
        gc.set_debug(gc.DEBUG_LEAK)

    # Catch the hdf5 file passed as the last argument
    file = pargs[0]

    print("Compression level:", complevel)
    if complevel > 0:
        print("Compression library:", complib)
    rowsw = 0
    if testwrite:
        t1 = clock()
        cpu1 = cpuclock()
        (rowsw, rowsz) = create_file(
            file, ngroups, ntables, nrows, complevel, complib, recsize
        )
        t2 = clock()
        cpu2 = cpuclock()
        tapprows = t2 - t1
        cpuapprows = cpu2 - cpu1
        print(f"Rows written: {rowsw}  Row size: {rowsz}")
        print(
            f"Time writing rows: {tapprows:.3f} s (real) "
            f"{cpuapprows:.3f} s (cpu)  {cpuapprows / tapprows:.0%}"
        )
        print(f"Write Krows/sec:  {rowsw / (tapprows * 1000):.3f}")
        print(f"Write MB/s : {rowsw * rowsz / (tapprows * 2**20):.3f}")

    if testread:
        t1 = clock()
        cpu1 = cpuclock()
        (rowsr, rowsz, bufsz) = read_file(file, ngroups, recsize, verbose)
        t2 = clock()
        cpu2 = cpuclock()
        treadrows = t2 - t1
        cpureadrows = cpu2 - cpu1
        print(f"Rows read: {rowsw}  Row size: {rowsz}, Buf size: {bufsz}")
        print(
            f"Time reading rows: {treadrows:.3f} s (real) "
            f"{cpureadrows:.3f} s (cpu)  {cpureadrows / treadrows:.0%}"
        )
        print(f"Read Krows/sec:  {rowsr / (treadrows * 1000):.3f}")
        print(f"Read MB/s : {rowsr * rowsz / (treadrows * 2**20):.3f}")

    # Show the dirt
    if debug > 1:
        dump_garbage()