test_benchmark.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright 2012 Pinterest.com
  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. import six
  15. import time
  16. import pytest
  17. # noinspection PyUnresolvedReferences
  18. try:
  19. # noinspection PyUnresolvedReferences
  20. import pylibmc
  21. HAS_PYLIBMC = True
  22. except Exception:
  23. HAS_PYLIBMC = False
  24. try:
  25. import memcache
  26. HAS_MEMCACHE = True
  27. except Exception:
  28. HAS_MEMCACHE = False
  29. try:
  30. import pymemcache.client
  31. HAS_PYMEMCACHE = True
  32. except Exception:
  33. HAS_PYMEMCACHE = False
  34. @pytest.fixture(params=[
  35. "pylibmc",
  36. "memcache",
  37. "pymemcache",
  38. ])
  39. def client(request, host, port):
  40. if request.param == "pylibmc":
  41. if not HAS_PYLIBMC:
  42. pytest.skip("requires pylibmc")
  43. client = pylibmc.Client(['{0}:{1}'.format(host, port)])
  44. client.behaviors = {"tcp_nodelay": True}
  45. elif request.param == "memcache":
  46. if not HAS_MEMCACHE:
  47. pytest.skip("requires python-memcached")
  48. client = memcache.Client(['{0}:{1}'.format(host, port)])
  49. elif request.param == "pymemcache":
  50. if not HAS_PYMEMCACHE:
  51. pytest.skip("requires pymemcache")
  52. client = pymemcache.client.Client((host, port))
  53. else:
  54. pytest.skip("unknown library {0}".format(request.param))
  55. client.flush_all()
  56. return client
  57. def benchmark(count, func, *args, **kwargs):
  58. start = time.time()
  59. for _ in range(count):
  60. result = func(*args, **kwargs)
  61. duration = time.time() - start
  62. print(str(duration))
  63. return result
  64. @pytest.mark.benchmark()
  65. def test_bench_get(request, client, pairs, count):
  66. key, value = six.next(six.iteritems(pairs))
  67. client.set(key, value)
  68. benchmark(count, client.get, key)
  69. @pytest.mark.benchmark()
  70. def test_bench_set(request, client, pairs, count):
  71. key, value = six.next(six.iteritems(pairs))
  72. benchmark(count, client.set, key, value)
  73. @pytest.mark.benchmark()
  74. def test_bench_get_multi(request, client, pairs, count):
  75. client.set_multi(pairs)
  76. benchmark(count, client.get_multi, list(pairs))
  77. @pytest.mark.benchmark()
  78. def test_bench_set_multi(request, client, pairs, count):
  79. benchmark(count, client.set_multi, pairs)
  80. @pytest.mark.benchmark()
  81. def test_bench_delete(request, client, pairs, count):
  82. benchmark(count, client.delete, six.next(six.iterkeys(pairs)))
  83. @pytest.mark.benchmark()
  84. def test_bench_delete_multi(request, client, pairs, count):
  85. # deleting missing key takes the same work client-side as real keys
  86. benchmark(count, client.delete_multi, list(pairs))