Repository: dmvass/sqlalchemy-easy-profile

Issue:
Does not work on python 3.8
Exception raises when trying to use time.clock on profiler.py.

timer.clock was deprecated since Python 3.3 and removed in 3.8.

From Python DOCs:

Deprecated since version 3.3, will be removed in version 3.8: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.



Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58e2c0a..d446327 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 
 ## [Unreleased]
 
+## [1.1.1] - 2020-07-26
+- Fixed deprecated time.clock [issue-16]
+
 ## [1.1.0] - 2020-06-29
 - Removed support of python 2.7, 3.5
 - Updated documentation
diff --git a/easy_profile/profiler.py b/easy_profile/profiler.py
index bfefff7..bc91152 100644
--- a/easy_profile/profiler.py
+++ b/easy_profile/profiler.py
@@ -3,20 +3,13 @@ import functools
 import inspect
 from queue import Queue
 import re
-import sys
-import time
+from time import perf_counter
 
 from sqlalchemy import event
 from sqlalchemy.engine.base import Engine
 
 from .reporters import StreamReporter
 
-# Optimize timer function for the platform
-if sys.platform == "win32":  # pragma: no cover
-    _timer = time.clock
-else:
-    _timer = time.time
-
 
 SQL_OPERATORS = ["select", "insert", "update", "delete"]
 OPERATOR_REGEX = re.compile("(%s) *." % "|".join(SQL_OPERATORS), re.IGNORECASE)
@@ -177,10 +170,10 @@ class SessionProfiler:
 
     def _before_cursor_execute(self, conn, cursor, statement, parameters,
                                context, executemany):
-        context._query_start_time = _timer()
+        context._query_start_time = perf_counter()
 
     def _after_cursor_execute(self, conn, cursor, statement, parameters,
                               context, executemany):
         self.queries.put(DebugQuery(
-            statement, parameters, context._query_start_time, _timer()
+            statement, parameters, context._query_start_time, perf_counter()
         ))


Test patch:
diff --git a/tests/test_profiler.py b/tests/test_profiler.py
index 1a64f49..eee3a40 100644
--- a/tests/test_profiler.py
+++ b/tests/test_profiler.py
@@ -142,9 +142,9 @@ class TestSessionProfiler(unittest.TestCase):
         self.assertListEqual(debug_queries, stats["call_stack"])
         self.assertDictEqual(stats["duplicates"], duplicates)
 
-    @mock.patch("easy_profile.profiler._timer")
+    @mock.patch("easy_profile.profiler.perf_counter")
     def test__before_cursor_execute(self, mocked):
-        expected_time = time.time()
+        expected_time = time.perf_counter()
         mocked.return_value = expected_time
         profiler = SessionProfiler()
         context = mock.Mock()
@@ -158,7 +158,7 @@ class TestSessionProfiler(unittest.TestCase):
         )
         self.assertEqual(context._query_start_time, expected_time)
 
-    @mock.patch("easy_profile.profiler._timer")
+    @mock.patch("easy_profile.profiler.perf_counter")
     def test__after_cursor_execute(self, mocked):
         expected_query = debug_queries[0]
         mocked.return_value = expected_query.end_time