| Conditions | 16 |
| Total Lines | 68 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like glances.start() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 93 | def start(config, args): |
||
| 94 | """Start Glances.""" |
||
| 95 | |||
| 96 | # Load mode |
||
| 97 | global mode |
||
| 98 | |||
| 99 | if args.trace_malloc or args.memory_leak: |
||
| 100 | tracemalloc.start() |
||
| 101 | |||
| 102 | start_duration = Counter() |
||
| 103 | |||
| 104 | if core.is_standalone(): |
||
| 105 | from glances.standalone import GlancesStandalone as GlancesMode |
||
| 106 | elif core.is_client(): |
||
| 107 | if core.is_client_browser(): |
||
| 108 | from glances.client_browser import GlancesClientBrowser as GlancesMode |
||
| 109 | else: |
||
| 110 | from glances.client import GlancesClient as GlancesMode |
||
| 111 | elif core.is_server(): |
||
| 112 | from glances.server import GlancesServer as GlancesMode |
||
| 113 | elif core.is_webserver(): |
||
| 114 | from glances.webserver import GlancesWebServer as GlancesMode |
||
| 115 | |||
| 116 | # Init the mode |
||
| 117 | logger.info("Start {} mode".format(GlancesMode.__name__)) |
||
| 118 | mode = GlancesMode(config=config, args=args) |
||
| 119 | |||
| 120 | # Start the main loop |
||
| 121 | logger.debug("Glances started in {} seconds".format(start_duration.get())) |
||
| 122 | if args.stop_after: |
||
| 123 | logger.info('Glances will be stopped in ~{} seconds'.format(args.stop_after * args.time * args.memory_leak * 2)) |
||
| 124 | |||
| 125 | if args.memory_leak: |
||
| 126 | print( |
||
| 127 | 'Memory leak detection, please wait ~{} seconds...'.format( |
||
| 128 | args.stop_after * args.time * args.memory_leak * 2 |
||
| 129 | ) |
||
| 130 | ) |
||
| 131 | # First run without dump to fill the memory |
||
| 132 | mode.serve_n(args.stop_after) |
||
| 133 | # Then start the memory-leak loop |
||
| 134 | snapshot_begin = tracemalloc.take_snapshot() |
||
| 135 | |||
| 136 | if args.stdout_issue or args.stdout_apidoc: |
||
| 137 | # Serve once for issue/test mode |
||
| 138 | mode.serve_issue() |
||
| 139 | else: |
||
| 140 | # Serve forever |
||
| 141 | mode.serve_forever() |
||
| 142 | |||
| 143 | if args.memory_leak: |
||
| 144 | snapshot_end = tracemalloc.take_snapshot() |
||
| 145 | snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') |
||
| 146 | memory_leak = sum([s.size_diff for s in snapshot_diff]) |
||
| 147 | print("Memory comsumption: {0:.1f}KB (see log for details)".format(memory_leak / 1000)) |
||
| 148 | logger.info("Memory consumption (top 5):") |
||
| 149 | for stat in snapshot_diff[:5]: |
||
| 150 | logger.info(stat) |
||
| 151 | elif args.trace_malloc: |
||
| 152 | # See more options here: https://docs.python.org/3/library/tracemalloc.html |
||
| 153 | snapshot = tracemalloc.take_snapshot() |
||
| 154 | top_stats = snapshot.statistics("filename") |
||
| 155 | print("[ Trace malloc - Top 10 ]") |
||
| 156 | for stat in top_stats[:10]: |
||
| 157 | print(stat) |
||
| 158 | |||
| 159 | # Shutdown |
||
| 160 | mode.end() |
||
| 161 | |||
| 190 |