Skip to content

Commit

Permalink
perf: reduce-testing-time (#3427)
Browse files Browse the repository at this point in the history
* feat: packing locals in launch_mapdl for debugging

* perf: reducing testing time of licensing

* chore: adding changelog file 3427.added.md

* perf: improving testing on pool.

* perf: checking just sending the message or not.

* fix: find_version test

* feat: using cache for path

* refactor: moving call later.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* chore: empty commit to trigger CICD

* fix: rename job to pull-request

* feat: cache directory at starting

* feat: adding 'fake_exit' to mapdl.exit

* feat: adding early exit in flush_stored

* fix: test

* revert: "feat: adding early exit in flush_stored"

This reverts commit 05bd04d.

* fix: wrong indentation and adding early exit.

* fix: adding early exit to _flush_stored in mapdlgrpc.

* test: reducing testing time in test_only_one_instance

* fix: test key

* fix: test instance name

* fix: test instance name

---------

Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
Co-authored-by: German Martinez Ayuso <gayuso@ottu2200701.ansys.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 26, 2024
1 parent 8298793 commit 1a1cfd4
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 223 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ jobs:
uses: ansys/actions/branch-name-style@v7


commit-name:
pull-request-name:
if: github.event_name == 'pull_request'
name: Check the name of the commit
name: Check the name of the pull-request
runs-on: ubuntu-latest
steps:
- name: Check commit name
- name: Check pull-request name
uses: ansys/actions/commit-style@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/3427.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
perf: reduce-testing-time
76 changes: 18 additions & 58 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,21 +1663,7 @@ def launch_mapdl(
return

if _debug_no_launch:
return pack_parameters(
port,
ip,
add_env_vars,
replace_env_vars,
cleanup_on_exit,
loglevel,
set_no_abort,
remove_temp_dir_on_exit,
log_apdl,
use_vtk,
start_parm,
start_instance,
version,
) # type: ignore
return pack_parameters(locals()) # type: ignore

mapdl = MapdlGrpc(
ip=ip,
Expand Down Expand Up @@ -1821,21 +1807,7 @@ def launch_mapdl(
elif mode == "grpc":
if _debug_no_launch:
# Early exit, just for testing
return pack_parameters(
port,
ip,
add_env_vars,
replace_env_vars,
cleanup_on_exit,
loglevel,
set_no_abort,
remove_temp_dir_on_exit,
log_apdl,
use_vtk,
start_parm,
start_instance,
version,
) # type: ignore
return pack_parameters(locals()) # type: ignore

port, actual_run_location, process = launch_grpc(
port=port,
Expand Down Expand Up @@ -2283,34 +2255,22 @@ def get_value(
return exec_file, jobname, nproc, ram, additional_switches


def pack_parameters(
port,
ip,
add_env_vars,
replace_env_vars,
cleanup_on_exit,
loglevel,
set_no_abort,
remove_temp_dir_on_exit,
log_apdl,
use_vtk,
start_parm,
start_instance,
version,
):
def pack_parameters(locals_var):
# pack all the arguments in a dict for debugging purposes
# We prefer to explicitly output the desired output
dict_ = {}
dict_["port"] = port
dict_["ip"] = ip
dict_["add_env_vars"] = add_env_vars
dict_["replace_env_vars"] = replace_env_vars
dict_["cleanup_on_exit"] = cleanup_on_exit
dict_["loglevel"] = loglevel
dict_["set_no_abort"] = set_no_abort
dict_["remove_temp_dir_on_exit"] = remove_temp_dir_on_exit
dict_["log_apdl"] = log_apdl
dict_["use_vtk"] = use_vtk
dict_["start_parm"] = start_parm
dict_["start_instance"] = start_instance
dict_["version"] = version
dict_["port"] = locals_var["port"]
dict_["ip"] = locals_var["ip"]
dict_["add_env_vars"] = locals_var["add_env_vars"]
dict_["replace_env_vars"] = locals_var["replace_env_vars"]
dict_["cleanup_on_exit"] = locals_var["cleanup_on_exit"]
dict_["loglevel"] = locals_var["loglevel"]
dict_["set_no_abort"] = locals_var["set_no_abort"]
dict_["remove_temp_dir_on_exit"] = locals_var["remove_temp_dir_on_exit"]
dict_["log_apdl"] = locals_var["log_apdl"]
dict_["use_vtk"] = locals_var["use_vtk"]
dict_["start_parm"] = locals_var["start_parm"]
dict_["start_instance"] = locals_var["start_instance"]
dict_["version"] = locals_var["version"]
dict_["additional_switches"] = locals_var["additional_switches"]
return dict_
47 changes: 29 additions & 18 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def __init__(
self._start_parm: Dict[str, Any] = start_parm
self._jobname: str = start_parm.get("jobname", "file")
self._path: Union[str, pathlib.Path] = start_parm.get("run_location", None)
self._path_cache = None # Cache
self._print_com: bool = print_com # print the command /COM input.
self.check_parameter_names = start_parm.get("check_parameter_names", True)

Expand Down Expand Up @@ -510,10 +511,14 @@ def directory(self) -> str:
# new line to fix path issue, see #416
self._path = repr(self._path)[1:-1]
else: # pragma: no cover
raise IOError(
f"The directory returned by /INQUIRE is not valid ('{self._path}')."
)
if self._path_cache:
return self._path_cache
else:
raise IOError(
f"The directory returned by /INQUIRE is not valid ('{self._path}')."
)

self._path_cache = self._path # update
return self._path

@directory.setter
Expand Down Expand Up @@ -1895,7 +1900,13 @@ def _flush_stored(self):
Overridden by gRPC.
"""
if not self._stored_commands:
self._log.debug("There is no commands to be flushed.")
self._store_commands = False
return

self._log.debug("Flushing stored commands")

rnd_str = random_string()
tmp_out = os.path.join(tempfile.gettempdir(), f"tmp_{rnd_str}.out")
self._stored_commands.insert(0, f"/OUTPUT, {tmp_out}")
Expand All @@ -1904,25 +1915,25 @@ def _flush_stored(self):
if self._apdl_log:
self._apdl_log.write(commands + "\n")

self._store_commands = False
self._stored_commands = []
self._store_commands = False
self._stored_commands = []

# write to a temporary input file
self._log.debug(
"Writing the following commands to a temporary " "apdl input file:\n%s",
commands,
)
# write to a temporary input file
self._log.debug(
"Writing the following commands to a temporary " "apdl input file:\n%s",
commands,
)

tmp_inp = os.path.join(tempfile.gettempdir(), f"tmp_{random_string()}.inp")
with open(tmp_inp, "w") as f:
f.writelines(commands)
tmp_inp = os.path.join(tempfile.gettempdir(), f"tmp_{random_string()}.inp")
with open(tmp_inp, "w") as f:
f.writelines(commands)

# interactive result
_ = self.input(tmp_inp, write_to_log=False)
# interactive result
_ = self.input(tmp_inp, write_to_log=False)

time.sleep(0.1) # allow MAPDL to close the file
if os.path.isfile(tmp_out):
self._response = "\n" + open(tmp_out).read()
time.sleep(0.1) # allow MAPDL to close the file
if os.path.isfile(tmp_out):
self._response = "\n" + open(tmp_out).read()

if self._response is None: # pragma: no cover
self._log.warning("Unable to read response from flushed commands")
Expand Down
28 changes: 18 additions & 10 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ def _run_at_connect(self):
with self.run_as_routine("POST26"):
self.numvar(200, mute=True)

self.inquire("", "DIRECTORY")
self.show(self._file_type_for_plots)
self.version # Caching version
self.file_type_for_plots # Setting /show,png and caching it.
Expand Down Expand Up @@ -1038,7 +1039,7 @@ def _threaded_heartbeat(self):
continue

@protect_from(ValueError, "I/O operation on closed file.")
def exit(self, save=False, force=False):
def exit(self, save=False, force=False, **kwargs):
"""Exit MAPDL.
Parameters
Expand Down Expand Up @@ -1069,8 +1070,6 @@ def exit(self, save=False, force=False):
elif self._exited:
# Already exited.
return
else:
mapdl_path = self.directory

if save:
self._log.debug("Saving MAPDL database")
Expand All @@ -1094,17 +1093,21 @@ def exit(self, save=False, force=False):
self._exiting = True
self._log.debug("Exiting MAPDL")

if self._local:
self._cache_pids() # Recache processes
if not kwargs.pop("fake_exit", False):
# This cannot should not be faked
if self._local:
mapdl_path = self.directory
self._cache_pids() # Recache processes

if os.name == "nt":
if os.name == "nt":
self._kill_server()
self._close_process()
self._remove_lock_file(mapdl_path)
else:
self._kill_server()
self._close_process()
self._remove_lock_file(mapdl_path)
else:
self._kill_server()

self._exited = True
self._exiting = False

if self._remote_instance: # pragma: no cover
# No cover: The CI is working with a single MAPDL instance
Expand Down Expand Up @@ -2031,6 +2034,11 @@ def _flush_stored(self):
"""Writes stored commands to an input file and runs the input
file. Used with non_interactive.
"""
if not self._stored_commands:
self._log.debug("There is no commands to be flushed.")
self._store_commands = False
return

self._log.debug("Flushing stored commands")

commands = "\n".join(self._stored_commands)
Expand Down
8 changes: 6 additions & 2 deletions src/ansys/mapdl/core/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def __init__(
self._n_instances = n_instances

# Getting debug arguments
_debug_no_launch = kwargs.pop("_debug_no_launch", None)
_debug_no_launch = kwargs.get("_debug_no_launch", None)

if run_location is None:
run_location = create_temp_dir()
Expand Down Expand Up @@ -338,7 +338,6 @@ def __init__(
"exec_file": exec_file,
"n_instances": n_instances,
}
return

# Converting ip or hostname to ip
self._ips = [socket.gethostbyname(each) for each in self._ips]
Expand All @@ -357,6 +356,11 @@ def __init__(
)
for i, (ip, port) in enumerate(zip(ips, ports))
]

# Early exit due to debugging
if _debug_no_launch:
return

if wait:
[thread.join() for thread in threads]

Expand Down
Loading

0 comments on commit 1a1cfd4

Please sign in to comment.