@@ -13,38 +13,41 @@ Performance Considerations
13
13
Overview
14
14
--------
15
15
16
- In this guide, you can learn how to optimize performance of the {+driver-short+}.
17
- To connect to MongoDB, you must create a ``Client`` instance. Your ``Client``
18
- instance automatically handles most aspects of connection, such as discovering server topology, monitoring
19
- your connection, and maintaining an internal connection pool.
20
- This guide describes best practices to configure and use your ``Client`` instance.
16
+ In this guide, you can learn how to optimize the performance of the
17
+ {+driver-short+}. To connect to MongoDB, you must create a ``Client`` instance. Your ``Client``
18
+ instance automatically handles most aspects of connection, such as
19
+ discovering server topology, monitoring your connection, and maintaining
20
+ an internal connection pool. This guide describes best practices for
21
+ configuring and using your ``Client`` instance.
21
22
22
23
.. _rust-performance-client-lifecycle:
23
24
24
25
Client Lifecycle
25
26
----------------
26
27
27
28
We recommend that you reuse your client across sessions and operations.
28
- You can use the same ``Client`` instance to perform multiple tasks, as the ``Client`` type is safe for concurrent use by multiple threads.
29
- Creating a new ``Client`` instance for each request results in slower performance.
29
+ You can use the same ``Client`` instance to perform multiple tasks, as
30
+ the ``Client`` type is safe for concurrent use by multiple threads.
31
+ Creating a new ``Client`` instance for each request results in slower
32
+ performance.
30
33
31
- The following code creates a method that accepts a pointer to an existing ``Client`` instance,
32
- which allows you to execute many requests by using same client:
34
+ The following code creates a method that accepts a pointer to an
35
+ existing ``Client`` instance, which allows you to perform many requests
36
+ by using the same client:
33
37
34
38
.. literalinclude:: /includes/fundamentals/code-snippets/performance.rs
35
39
:language: rust
36
40
:dedent:
37
- :start-after: start-perf-client-faster
38
- :end-before: end-perf-client-faster
39
41
40
42
.. _rust-performance-parallelism:
41
43
42
44
Parallelism
43
45
-----------
44
46
45
- If you can run parallel data operations, you can optimize performance by running asynchronous, concurrent tasks.
46
- The following code uses the ``task`` module from the ``tokio`` crate to create separate, concurrent
47
- tasks for multiple data operations:
47
+ If you can run parallel data operations, you can optimize performance by
48
+ running asynchronous, concurrent tasks. The following code uses the
49
+ ``spawn()`` method from the ``tokio::task`` module to create separate,
50
+ concurrent tasks to perform insert operations:
48
51
49
52
.. literalinclude:: /includes/fundamentals/code-snippets/performance-parallel.rs
50
53
:language: rust
@@ -55,28 +58,31 @@ tasks for multiple data operations:
55
58
Runtime
56
59
-------
57
60
58
- A ``Client`` instance is bound to the instance of the ``tokio`` or ``async-std`` runtime in which you created it.
59
- If you use a ``Client`` instance to perform operations on a different runtime, you might experience unexpected behavior or failures.
61
+ A ``Client`` instance is bound to the instance of the ``tokio`` or
62
+ ``async-std`` runtime in which you created it. If you use a ``Client``
63
+ instance to perform operations on a different runtime, you might
64
+ experience unexpected behavior or failures.
60
65
61
- If you are using the ``test`` helper macro from the ``tokio`` or ``async_std`` crate to test your application,
62
- you might accidentally run operations on a different runtime than you intended.
63
- This is because these helper macros create a new runtime for each test.
64
- You can use one of the following strategies to avoid this issue:
66
+ If use the ``test`` helper macro from the ``tokio`` or
67
+ ``async_std`` crate to test your application, you might accidentally run
68
+ operations on a different runtime than intended. This is because these
69
+ helper macros create a new runtime for each test. However, you can use
70
+ one of the following strategies to avoid this issue:
65
71
66
72
- Attach the runtime to the ``Client`` instance without using the ``test`` helper macros.
67
73
- Create a new ``Client`` instance for every ``async`` test.
68
74
69
75
This example follows the first strategy and creates a global runtime used only for testing.
70
- In the following code, the ``test_list_dbs()`` method uses a client that manually connects to this runtime
71
- to list databases in the deployment.
76
+ In the following code, the ``test_list_dbs()`` method uses a client that
77
+ manually connects to this runtime to list databases in the deployment:
72
78
73
79
.. literalinclude:: /includes/fundamentals/code-snippets/performance-bundle-runtime.rs
74
80
:language: rust
75
81
:dedent:
76
82
77
- Implementing the second strategy,
78
- the following code creates a new ``Client`` instance for each test run with ``tokio::test``,
79
- ensuring that there is no unintended interaction between runtimes:
83
+ Implementing the second strategy, the following code creates a new
84
+ ``Client`` instance for each test run with ``tokio::test``,
85
+ ensuring that there are no unintended interactions between runtimes:
80
86
81
87
.. literalinclude:: /includes/fundamentals/code-snippets/performance-new-client.rs
82
88
:language: rust
@@ -85,13 +91,15 @@ ensuring that there is no unintended interaction between runtimes:
85
91
Additional Information
86
92
----------------------
87
93
88
- For more information about the concepts in this guide, see the following pages:
94
+ For more information about connecting to MongoDB, see the
95
+ :ref:`Connection Guide <rust-connect-to-mongodb-client>`.
89
96
90
- - :ref:`Connection Guide <rust-connect-to-mongodb-client>`
97
+ .. TODO link to runtimes page
91
98
92
99
API Documentation
93
100
~~~~~~~~~~~~~~~~~
94
101
95
102
- `Client() <{+api+}/struct.Client.html>`__
96
-
97
- .. TODO link to Async page when done
103
+ - `spawn() <https://docs.rs/tokio/latest/tokio/task/fn.spawn.html>`__ in
104
+ the ``tokio::task`` module
105
+ - `tokio::runtime <https://docs.rs/tokio/latest/tokio/runtime/index.html>`__ module
0 commit comments