-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_write.py
111 lines (92 loc) · 3.01 KB
/
test_write.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
import asyncio
from utils import timer
@timer.timer
def test_python_write_file():
with open("write.txt", "wb") as fp:
for i in range(10 ** 6):
s = f"{i}" * 100
fp.write(f"中{i}:{s}\n".encode("gbk"))
@timer.atimer
async def test_write_file():
import aiofile
async with aiofile.open("write1.txt", "wb") as fp:
write_tasks = []
for i in range(10 ** 6):
s = f"{i}" * 100
# @timer.atimer
# async def _write(fp, data):
# await fp.write(data)
#
# await _write(fp, f"中{i}:{s}\n")
await fp.write(f"中{i}:{s}\n".encode("gbk"))
# write_tasks.append(task)
# await asyncio.gather(*write_tasks)
@timer.atimer
async def test_aiofiles_write_file():
import aiofiles
async with aiofiles.open("write2.txt", "w") as fp:
write_tasks = []
for i in range(10 ** 6):
s = f"{i}" * 100
task = fp.write(f"中{i}:{s}\n")
write_tasks.append(task)
await asyncio.gather(*write_tasks)
@timer.timer
def valid():
with open("write.txt", "rb") as fp0, open("write1.txt", "rb") as fp1, open("write2.txt", "rb") as fp2:
fp0_read = fp0.read()
fp1_read = fp1.read()
try:
fp2_read = fp2.read()
except:
fp2_read = ""
print(fp0_read == fp1_read, fp0_read == fp2_read, len(fp0_read), len(fp1_read))
print(fp0_read[:1024])
print(fp1_read[:1024])
@timer.atimer
async def test_write_correct():
import aiofile
write_lines = []
async with aiofile.open("write1.txt", "wb") as fp:
for i in range(10):
s = f"{i}" * 100
line = f"中{i}:{s}\n".encode("gbk")
await fp.write(line)
write_lines.append(line)
with open("write1.txt", "rb") as fp:
assert fp.read() == b''.join(write_lines)
async def test_write_correct_repeat():
for i in range(10 ** 5):
await test_write_correct()
@timer.atimer
async def test_write_lines():
import aiofile
async with aiofile.open("write1.txt", "wb") as fp:
lines = []
for i in range(10 ** 6):
s = f"{i}" * 100
line = f"中{i}:{s}\n".encode("gbk")
lines.append(line)
# @timer.atimer
# async def _write(fp, data):
# await fp.write(data)
#
# await _write(fp, f"中{i}:{s}\n")
await fp.write_lines(lines)
@timer.timer
def test_python_write_lines():
with open("write.txt", "wb") as fp:
lines = []
for i in range(10 ** 6):
s = f"{i}" * 100
line = f"中{i}:{s}\n".encode("gbk")
lines.append(line)
fp.writelines(lines)
if __name__ == '__main__':
# asyncio.run(test_write_correct_repeat())
# asyncio.run(test_write_file())
# # asyncio.run(test_aiofiles_write_file())
# test_python_write_file()
asyncio.run(test_write_lines())
test_python_write_lines()
valid()