1
|
|
|
import os |
2
|
|
|
import time |
3
|
|
|
from typing import Optional |
4
|
|
|
|
5
|
|
|
from cleo import Command, Input, Output |
6
|
|
|
|
7
|
|
|
from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle |
8
|
|
|
|
9
|
|
|
|
10
|
|
View Code Duplication |
class TraversePerformanceTestCommand(Command): |
|
|
|
|
11
|
|
|
""" |
12
|
|
|
Traversing recursively a directory performance test |
13
|
|
|
|
14
|
|
|
traverse-performance-test |
15
|
|
|
{--stat : Get status of each file} |
16
|
|
|
{dir : The start directory} |
17
|
|
|
""" |
18
|
|
|
|
19
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
20
|
|
|
def __init__(self): |
21
|
|
|
""" |
22
|
|
|
Object constructor. |
23
|
|
|
""" |
24
|
|
|
Command.__init__(self) |
25
|
|
|
|
26
|
|
|
self.__stat: bool = False |
27
|
|
|
""" |
28
|
|
|
If True stat must be called for each file. |
29
|
|
|
""" |
30
|
|
|
|
31
|
|
|
self._io: Optional[BackupPcCloneStyle] = None |
32
|
|
|
""" |
33
|
|
|
The output style. |
34
|
|
|
""" |
35
|
|
|
|
36
|
|
|
self.__dir_count: int = 0 |
37
|
|
|
""" |
38
|
|
|
The number of directories counted. |
39
|
|
|
""" |
40
|
|
|
|
41
|
|
|
self.__file_count: int = 0 |
42
|
|
|
""" |
43
|
|
|
The number of file counted. |
44
|
|
|
""" |
45
|
|
|
|
46
|
|
|
self.__start_time: float = 0 |
47
|
|
|
""" |
48
|
|
|
The timestamp of the start of the performance test. |
49
|
|
|
""" |
50
|
|
|
|
51
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
52
|
|
|
def __traverse(self, path: str) -> None: |
53
|
|
|
""" |
54
|
|
|
Traverse recursively a directory. |
55
|
|
|
|
56
|
|
|
@param str path: The path to the directory. |
57
|
|
|
""" |
58
|
|
|
dirs = [] |
59
|
|
|
for entry in os.scandir(path): |
60
|
|
|
if self.__stat and not entry.is_symlink(): |
61
|
|
|
entry.stat() |
62
|
|
|
|
63
|
|
|
if entry.is_file(): |
64
|
|
|
self.__file_count += 1 |
65
|
|
|
|
66
|
|
|
elif entry.is_dir(): |
67
|
|
|
dirs.append(entry.name) |
68
|
|
|
self.__dir_count += 1 |
69
|
|
|
|
70
|
|
|
for name in dirs: |
71
|
|
|
self.__traverse(os.path.join(path, name)) |
72
|
|
|
|
73
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
74
|
|
|
def __report(self, end_time: float) -> None: |
75
|
|
|
""" |
76
|
|
|
Prints the performance report. |
77
|
|
|
|
78
|
|
|
@param float end_time: The timestamp of the end of the performance test. |
79
|
|
|
""" |
80
|
|
|
self._io.writeln('') |
81
|
|
|
self._io.writeln('number of directories: {}'.format(self.__dir_count)) |
82
|
|
|
self._io.writeln('number of files : {}'.format(self.__file_count)) |
83
|
|
|
self._io.writeln('get status : {}'.format('yes' if self.__stat else 'no')) |
84
|
|
|
self._io.writeln('duration : {0:.1f}s'.format(end_time - self.__start_time)) |
85
|
|
|
|
86
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
87
|
|
|
def execute(self, input_object: Input, output_object: Output) -> None: |
88
|
|
|
""" |
89
|
|
|
Executes the command. |
90
|
|
|
|
91
|
|
|
@param Input input_object: The input. |
92
|
|
|
@param Output output_object: The output. |
93
|
|
|
""" |
94
|
|
|
self.input = input_object |
95
|
|
|
self.output = output_object |
96
|
|
|
|
97
|
|
|
self.handle() |
98
|
|
|
|
99
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
100
|
|
|
def handle(self) -> None: |
101
|
|
|
""" |
102
|
|
|
Executes the command. |
103
|
|
|
""" |
104
|
|
|
self._io = BackupPcCloneStyle(self.input, self.output) |
105
|
|
|
|
106
|
|
|
self.__stat = self.option('stat') |
107
|
|
|
self.__dir_count = 0 |
108
|
|
|
self.__file_count = 0 |
109
|
|
|
self.__start_time = time.time() |
110
|
|
|
|
111
|
|
|
dir_name = self.argument('dir') |
112
|
|
|
|
113
|
|
|
self._io.writeln('Traversing <fso>{}</fso>'.format(dir_name)) |
114
|
|
|
self.__traverse(dir_name) |
115
|
|
|
self.__report(time.time()) |
116
|
|
|
|
117
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
118
|
|
|
|