-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
119 lines (93 loc) · 3.34 KB
/
test.py
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
""" test cases """
import os
import unittest
from timeit import timeit
import f90nml
from fastnml import read_namelist, save_namelist
def read_from_file_f90nml(filename, n_threads, parser):
"f90nml: Read all at once from file"
return parser.read(filename)
def read_chunks_f90nml(filename, n_threads, parser):
"f90nml: Read in chunks"
return read_namelist(
filename,
n_threads=n_threads,
parser=parser,
simple=False,
) # use f90nml
def read_chunks_simple(filename, n_threads, parser):
"fastnml: Read in chunks"
return read_namelist(filename, n_threads=n_threads, parser=parser)
def run_read_tests(filenames, tests, parser, repeats):
# print(f'Each test will be repeated {repeats} times. '
# f'Reported times are averages.')
for filename in filenames:
print("")
print("-----------------------------")
print(str(filename))
print("-----------------------------")
print("")
for t in tests:
func, threads = t
for n_threads in range(0, threads + 1):
case = f"{func.__doc__} ({n_threads} threads) : "
tottime = timeit(
lambda: func(filename, n_threads, parser), number=repeats
)
print(f"{case.ljust(55)}{tottime/repeats} sec")
class TestFastnml(unittest.TestCase):
"""
Main unittest class.
"""
def test_1(self):
"""
Main unit test case for `fastnml`.
"""
print("")
print("---------------------")
print(" run the save tests:")
print("---------------------")
print("")
outfilename = "sample.nml"
d = {
"globvars": {
"a": {
"TF": True,
"REAL": 2.0,
"int": 146,
"array1": [1, 2],
"array2": [1, 2],
"array3": [1, 2],
"str": "string 'with quotes'",
"list": ["a", "b", None, "d", "e"],
}
},
"morevars": [{"name": 1}, {"name": 2}],
}
save_namelist(d, outfilename)
with open(outfilename, "r") as f:
print(f.read())
print("")
print("---------------------")
print(" run the read tests:")
print("---------------------")
print("")
filenames = [
"test.nml", # 112 namelists, all strings [8 sec]
"test4.nml", # 112 namelists, all strings, long keys w/ (2) [42 sec]
"test4b.nml", # 112 namelists, all strings, long keys no array [9 sec]
"test4c.nml", # 112 namelists, all strings, long keys w/ % [12 sec]
]
filenames = [os.path.join("tests", f) for f in filenames]
n_threads_to_test = 4 # for threading cases
tests = [
(read_from_file_f90nml, 0),
(read_chunks_f90nml, n_threads_to_test),
(read_chunks_simple, n_threads_to_test),
]
parser = f90nml.Parser()
parser.global_start_index = 1
repeats = 1
run_read_tests(filenames, tests, parser, repeats)
if __name__ == "__main__":
unittest.main()