summaryrefslogtreecommitdiff
path: root/src/mongo/util/tick_source.h
diff options
context:
space:
mode:
authorWilliam Schultz <william.schultz@mongodb.com>2018-10-02 21:54:58 -0400
committerWilliam Schultz <william.schultz@mongodb.com>2018-10-02 21:57:52 -0400
commit982ba21e0ffbdaaf766dc8fa060728ba9b5f4914 (patch)
treeefc760c482e30bd70bfbaf5854779a89acf00ef3 /src/mongo/util/tick_source.h
parent23d7c89e501d221a41f350b0b10a52a4f05bb2e2 (diff)
downloadmongo-982ba21e0ffbdaaf766dc8fa060728ba9b5f4914.tar.gz
SERVER-36697 Utilize TickSource for transactions timing metrics
This patch converts the existing transactions diagnostics timing related metrics to use a TickSource to record transaction durations. The TickSource is a high precision, mock-able time source for measuring the passage of time. This patch also converts the existing unit tests to use a mock TickSource, which allows the tests to fully virtualize time, making them much faster and less flaky.
Diffstat (limited to 'src/mongo/util/tick_source.h')
-rw-r--r--src/mongo/util/tick_source.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/mongo/util/tick_source.h b/src/mongo/util/tick_source.h
index 134f4d05737..1aef0e478dc 100644
--- a/src/mongo/util/tick_source.h
+++ b/src/mongo/util/tick_source.h
@@ -50,5 +50,18 @@ public:
* Returns the conversion ratio from ticks to seconds.
*/
virtual Tick getTicksPerSecond() = 0;
+
+ /**
+ * Convert the given tick count into a duration, specified by the type parameter.
+ *
+ * e.g. tickSource->ticksTo<Milliseconds>(ticks);
+ */
+ template <typename D>
+ D ticksTo(Tick ticks) {
+ // The number of ticks per 1 duration unit.
+ double ticksPerD =
+ static_cast<double>(getTicksPerSecond()) * D::period::num / D::period::den;
+ return D(static_cast<int64_t>(ticks / ticksPerD));
+ }
};
} // namespace mongo