Optimize running speed
This commit is contained in:
parent
c46123c1d4
commit
cd007774a0
63
Galactic.py
63
Galactic.py
|
@ -162,6 +162,16 @@ def import_module(file: str):
|
|||
spec.loader.exec_module(module_from_spec)
|
||||
return module_from_spec
|
||||
|
||||
def calculate_execution_time(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
start_time = time.time()
|
||||
result = func(*args, **kwargs)
|
||||
end_time = time.time()
|
||||
execution_time_ms = (end_time - start_time) * 1000
|
||||
print('Function \'{}\' with args {} and kwargs {} took {:.3f} ms to execute.'.format(func.__name__, args, kwargs, execution_time_ms), file=sys.stderr)
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
|
||||
class BrowserMobileEmulation(dict):
|
||||
"""
|
||||
|
@ -509,6 +519,26 @@ class EmptyMethod:
|
|||
return self.no_method
|
||||
|
||||
|
||||
class BrowserService(Service):
|
||||
def __del__(self):
|
||||
pass
|
||||
|
||||
def is_connectable(self):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(0.05)
|
||||
try:
|
||||
return s.connect_ex(('127.0.0.1', self.port)) == 0
|
||||
except socket.timeout:
|
||||
return False
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
class BrowserOptions(Options):
|
||||
def __del__(self):
|
||||
pass
|
||||
|
||||
|
||||
class Browser(InspectRequestsMixin, DriverCommonMixin, Chrome):
|
||||
"""
|
||||
Browser web driver.
|
||||
|
@ -544,8 +574,8 @@ class Browser(InspectRequestsMixin, DriverCommonMixin, Chrome):
|
|||
# Initialization settings.
|
||||
if (isinstance(option_arguments, list)) is bool(0): option_arguments = []
|
||||
cdplist = []
|
||||
service = Service()
|
||||
options = Options()
|
||||
service = BrowserService()
|
||||
options = BrowserOptions()
|
||||
self.cdplist = cdplist
|
||||
# Delete prompt information of chrome being controlled.
|
||||
exclude_switches = ['enable-automation', 'enable-logging', 'disable-translate']
|
||||
|
@ -807,6 +837,7 @@ class Browser(InspectRequestsMixin, DriverCommonMixin, Chrome):
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
@calculate_execution_time
|
||||
def quit_backend(self):
|
||||
"""
|
||||
Exit the browser backend.
|
||||
|
@ -1731,6 +1762,7 @@ class BrowserManager:
|
|||
finally:
|
||||
s.close()
|
||||
|
||||
@calculate_execution_time
|
||||
def plugin_run(self, user_id: str, plugin_id: str, requirements=None):
|
||||
try:
|
||||
self.user_operate_starting(user_id)
|
||||
|
@ -1748,6 +1780,7 @@ class BrowserManager:
|
|||
finally:
|
||||
self.user_operate_complete(user_id)
|
||||
|
||||
@calculate_execution_time
|
||||
def plugin_shutdown(self, user_id: str):
|
||||
try:
|
||||
self.user_operate_starting(user_id)
|
||||
|
@ -1768,6 +1801,7 @@ class BrowserManager:
|
|||
self._update_user_running()
|
||||
return [user_id for user_id, running in self.user_running.items() if running and running.is_running]
|
||||
|
||||
@calculate_execution_time
|
||||
def user_all_details(self, user_id: str = None):
|
||||
plugins = {plugin_id: {'name': plugin_class.name, 'requirements': plugin_class.requirements} for plugin_id, plugin_class in self.plugins.items()}
|
||||
details = {user_id: self.user_running[user_id].status() for user_id in self.user_ids() if user_id in self.user_running.keys()}
|
||||
|
@ -1777,6 +1811,7 @@ class BrowserManager:
|
|||
else:
|
||||
return details
|
||||
|
||||
@calculate_execution_time
|
||||
def user_set_name(self, user_id: str, user_name: str):
|
||||
try:
|
||||
self.user_operate_starting(user_id)
|
||||
|
@ -1792,6 +1827,7 @@ class BrowserManager:
|
|||
self.user_operate_complete(user_id)
|
||||
|
||||
@func_set_timeout(0.55)
|
||||
@calculate_execution_time
|
||||
def user_focus_window(self, user_id: str):
|
||||
if (user_id in self.user_ids()) == 0:
|
||||
raise FileExistsError('User ID not exists.')
|
||||
|
@ -1808,6 +1844,7 @@ class BrowserManager:
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
@calculate_execution_time
|
||||
def user_add(self, user_id: str):
|
||||
try:
|
||||
self.user_operate_starting(user_id)
|
||||
|
@ -1831,6 +1868,7 @@ class BrowserManager:
|
|||
finally:
|
||||
self.user_operate_complete(user_id)
|
||||
|
||||
@calculate_execution_time
|
||||
def user_del(self, user_id: str):
|
||||
try:
|
||||
self.user_operate_starting(user_id)
|
||||
|
@ -1848,6 +1886,7 @@ class BrowserManager:
|
|||
finally:
|
||||
self.user_operate_complete(user_id)
|
||||
|
||||
@calculate_execution_time
|
||||
def user_run(self, user_id: str):
|
||||
try:
|
||||
self.user_operate_starting(user_id)
|
||||
|
@ -1875,6 +1914,7 @@ class BrowserManager:
|
|||
finally:
|
||||
self.user_operate_complete(user_id)
|
||||
|
||||
@calculate_execution_time
|
||||
def user_die(self, user_id: str):
|
||||
try:
|
||||
self.user_operate_starting(user_id)
|
||||
|
@ -1941,7 +1981,7 @@ class MainWindow(QMainWindow):
|
|||
self.tray_icon = QSystemTrayIcon(QIcon(os.path.join(os.path.dirname(__file__), 'favicon.ico')), self)
|
||||
self.tray_icon_update_timer = QTimer(self)
|
||||
self.tray_icon_update_timer.timeout.connect(self.on_tray_icon_update)
|
||||
self.tray_icon_update_timer.start(1750)
|
||||
self.tray_icon_update_timer.start(2250)
|
||||
self.tray_icon.activated.connect(self.on_tray_icon_activated)
|
||||
self.tray_menu = QMenu()
|
||||
action_list = [
|
||||
|
@ -1985,7 +2025,6 @@ class MainWindow(QMainWindow):
|
|||
self.webview.load(QUrl('http://%s:%s/' % ('127.0.0.1' if self.web_listen_host == '0.0.0.0' else self.web_listen_host, self.web_listen_port)))
|
||||
|
||||
def exit(self):
|
||||
self.webview.load(QUrl('about:blank'))
|
||||
self.webview.deleteLater()
|
||||
self.runner.handle_interrupt()
|
||||
try:
|
||||
|
@ -2312,14 +2351,14 @@ class MainRunner:
|
|||
self.application_scale_rate = None
|
||||
self.window = None
|
||||
self.plugin_list = [
|
||||
BrowserPluginFileTest,
|
||||
BrowserPluginTextTest,
|
||||
BrowserPluginMultFileTest,
|
||||
BrowserPluginMultTextTest,
|
||||
BrowserPluginLoggingTest,
|
||||
BrowserPluginMessageTest,
|
||||
BrowserPluginFileCommandDebug,
|
||||
BrowserPluginTextCommandDebug
|
||||
# BrowserPluginFileTest,
|
||||
# BrowserPluginTextTest,
|
||||
# BrowserPluginMultFileTest,
|
||||
# BrowserPluginMultTextTest,
|
||||
# BrowserPluginLoggingTest,
|
||||
# BrowserPluginMessageTest,
|
||||
# BrowserPluginFileCommandDebug,
|
||||
# BrowserPluginTextCommandDebug
|
||||
]
|
||||
|
||||
def _copy_files_and_directories(self, src, dst):
|
||||
|
|
Loading…
Reference in New Issue