_leak_test.py
Go to the documentation of this file.
1 # Copyright 2020 The gRPC Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """A smoke test for memory leaks on short-lived channels without close.
15 
16 This test doesn't guarantee all resources are cleaned if `Channel.close` is not
17 explicitly invoked. The recommended way of using Channel object is using `with`
18 clause, and let context manager automatically close the channel.
19 """
20 
21 from concurrent.futures import ThreadPoolExecutor
22 import logging
23 import os
24 import resource
25 import sys
26 import unittest
27 
28 import grpc
29 
30 _TEST_METHOD = '/test/Test'
31 _REQUEST = b'\x23\x33'
32 _LARGE_NUM_OF_ITERATIONS = 5000
33 
34 # If MAX_RSS inflated more than this size, the test is failed.
35 _FAIL_THRESHOLD = 25 * 1024 * 1024 # 25 MiB
36 
37 
39  return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
40 
41 
43  if x > 1024 * 1024 * 1024:
44  return "%.2f GiB" % (x / 1024.0 / 1024 / 1024)
45  elif x > 1024 * 1024:
46  return "%.2f MiB" % (x / 1024.0 / 1024)
47  elif x > 1024:
48  return "%.2f KiB" % (x / 1024.0)
49  else:
50  return "%d B" % x
51 
52 
54 
55  def service(self, handler_call_details):
56  if handler_call_details.method == _TEST_METHOD:
57  return grpc.unary_unary_rpc_method_handler(lambda x, _: x)
58 
59 
61  server = grpc.server(ThreadPoolExecutor(max_workers=1),
62  options=(('grpc.so_reuseport', 0),))
63  server.add_generic_rpc_handlers((_GenericHandler(),))
64  port = server.add_insecure_port('localhost:0')
65  server.start()
66  return 'localhost:%d' % port, server
67 
68 
69 def _perform_an_rpc(address):
70  channel = grpc.insecure_channel(address)
71  multicallable = channel.unary_unary(_TEST_METHOD)
72  response = multicallable(_REQUEST)
73  assert _REQUEST == response
74 
75 
76 class TestLeak(unittest.TestCase):
77 
79  address, server = _start_a_test_server()
80 
81  # Records memory before experiment.
82  before = _get_max_rss()
83 
84  # Amplifies the leak.
85  for n in range(_LARGE_NUM_OF_ITERATIONS):
86  _perform_an_rpc(address)
87 
88  # Fails the test if memory leak detected.
89  diff = _get_max_rss() - before
90  if diff > _FAIL_THRESHOLD:
91  self.fail("Max RSS inflated {} > {}".format(
92  _pretty_print_bytes(diff),
93  _pretty_print_bytes(_FAIL_THRESHOLD)))
94 
95 
96 if __name__ == "__main__":
97  logging.basicConfig(level=logging.DEBUG)
98  unittest.main(verbosity=2)
tests_py3_only.unit._leak_test.TestLeak.test_leak_with_single_shot_rpcs
def test_leak_with_single_shot_rpcs(self)
Definition: _leak_test.py:78
grpc.unary_unary_rpc_method_handler
def unary_unary_rpc_method_handler(behavior, request_deserializer=None, response_serializer=None)
Definition: src/python/grpcio/grpc/__init__.py:1510
grpc.insecure_channel
def insecure_channel(target, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1962
http2_test_server.format
format
Definition: http2_test_server.py:118
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
tests_py3_only.unit._leak_test._start_a_test_server
def _start_a_test_server()
Definition: _leak_test.py:60
grpc.GenericRpcHandler
Definition: src/python/grpcio/grpc/__init__.py:1333
tests_py3_only.unit._leak_test.TestLeak
Definition: _leak_test.py:76
grpc.server
def server(thread_pool, handlers=None, interceptors=None, options=None, maximum_concurrent_rpcs=None, compression=None, xds=False)
Definition: src/python/grpcio/grpc/__init__.py:2034
tests_py3_only.unit._leak_test._GenericHandler.service
def service(self, handler_call_details)
Definition: _leak_test.py:55
tests_py3_only.unit._leak_test._perform_an_rpc
def _perform_an_rpc(address)
Definition: _leak_test.py:69
tests_py3_only.unit._leak_test._GenericHandler
Definition: _leak_test.py:53
tests_py3_only.unit._leak_test._pretty_print_bytes
def _pretty_print_bytes(x)
Definition: _leak_test.py:42
tests_py3_only.unit._leak_test._get_max_rss
def _get_max_rss()
Definition: _leak_test.py:38


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:38