test_benchmark.py 2.9 KB

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