Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql_query: returns execution_time_ms list containing execution time per query #697

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/0-mysql_query-returns-exec-time-ms.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- mysql_query - returns the ``execution_time_ms`` list containing execution time per query in milliseconds.
28 changes: 26 additions & 2 deletions plugins/modules/mysql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
- Andrew Klychkov (@Andersson007)
extends_documentation_fragment:
- community.mysql.mysql

'''

EXAMPLES = r'''
Expand Down Expand Up @@ -117,8 +116,18 @@
returned: changed
type: list
sample: [5, 1]
execution_time_ms:
description:
- A list containing execution time per query in milliseconds.
- The measurements are done right before and after passing
the query to the driver for execution.
returned: success
type: list
sample: [7104, 85]
version_added: '3.12.0'
'''

import time
import warnings

from ansible.module_utils.basic import AnsibleModule
Expand All @@ -139,6 +148,18 @@
# Module execution.
#


def execute_and_return_time(cursor, query, args):
# Measure query execution time in milliseconds
start_time = time.perf_counter()

cursor.execute(query, args)

# Calculate the execution time rounding it to 4 decimal places
exec_time_ms = round((time.perf_counter() - start_time) * 1000, 4)
laurent-indermuehle marked this conversation as resolved.
Show resolved Hide resolved
return cursor, exec_time_ms


def main():
argument_spec = mysql_common_argument_spec()
argument_spec.update(
Expand Down Expand Up @@ -213,6 +234,7 @@ def main():
query_result = []
executed_queries = []
rowcount = []
execution_time_ms = []

already_exists = False
for q in query:
Expand All @@ -223,7 +245,8 @@ def main():
category=mysql_driver.Warning)

try:
cursor.execute(q, arguments)
cursor, exec_time_ms = execute_and_return_time(cursor, q, arguments)
execution_time_ms.append(exec_time_ms)
except mysql_driver.Warning:
# When something is run with IF NOT EXISTS
# and there's "already exists" MySQL warning,
Expand Down Expand Up @@ -280,6 +303,7 @@ def main():
'executed_queries': executed_queries,
'query_result': query_result,
'rowcount': rowcount,
'execution_time_ms': execution_time_ms,
}

# Exit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
that:
- result is changed
- result.executed_queries == ['CREATE TABLE {{ test_table1 }} (id int)']
- result.execution_time_ms[0] > 0

- name: Insert test data
mysql_query:
Expand All @@ -52,6 +53,8 @@
- result is changed
- result.rowcount == [2, 1]
- result.executed_queries == ['INSERT INTO {{ test_table1 }} VALUES (1), (2)', 'INSERT INTO {{ test_table1 }} VALUES (3)']
- result.execution_time_ms[0] > 0
- result.execution_time_ms[1] > 0

- name: Check data in {{ test_table1 }}
mysql_query:
Expand Down
Loading