summaryrefslogtreecommitdiff
path: root/pymemcache/test/test_benchmark.py
blob: 55653bde2e0bffc55f68a6a0c5b6286621fc1e14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright 2012 Pinterest.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time
import pytest

try:
    import pylibmc  # type: ignore

    HAS_PYLIBMC = True
except Exception:
    HAS_PYLIBMC = False

try:
    import memcache  # type: ignore

    HAS_MEMCACHE = True
except Exception:
    HAS_MEMCACHE = False


try:
    import pymemcache.client

    HAS_PYMEMCACHE = True
except Exception:
    HAS_PYMEMCACHE = False


@pytest.fixture(
    params=[
        "pylibmc",
        "memcache",
        "pymemcache",
    ]
)
def client(request, host, port):
    if request.param == "pylibmc":
        if not HAS_PYLIBMC:
            pytest.skip("requires pylibmc")
        client = pylibmc.Client([f"{host}:{port}"])
        client.behaviors = {"tcp_nodelay": True}

    elif request.param == "memcache":
        if not HAS_MEMCACHE:
            pytest.skip("requires python-memcached")
        client = memcache.Client([f"{host}:{port}"])

    elif request.param == "pymemcache":
        if not HAS_PYMEMCACHE:
            pytest.skip("requires pymemcache")
        client = pymemcache.client.Client((host, port))

    else:
        pytest.skip(f"unknown library {request.param}")

    client.flush_all()
    return client


def benchmark(count, func, *args, **kwargs):
    start = time.time()

    for _ in range(count):
        result = func(*args, **kwargs)

    duration = time.time() - start
    print(str(duration))

    return result


@pytest.mark.benchmark()
def test_bench_get(request, client, pairs, count):
    key, value = next(pairs)
    client.set(key, value)
    benchmark(count, client.get, key)


@pytest.mark.benchmark()
def test_bench_set(request, client, pairs, count):
    key, value = next(pairs.items())
    benchmark(count, client.set, key, value)


@pytest.mark.benchmark()
def test_bench_get_multi(request, client, pairs, count):
    client.set_multi(pairs)
    benchmark(count, client.get_multi, list(pairs))


@pytest.mark.benchmark()
def test_bench_set_multi(request, client, pairs, count):
    benchmark(count, client.set_multi, pairs)


@pytest.mark.benchmark()
def test_bench_delete(request, client, pairs, count):
    benchmark(count, client.delete, next(pairs))


@pytest.mark.benchmark()
def test_bench_delete_multi(request, client, pairs, count):
    # deleting missing key takes the same work client-side as real keys
    benchmark(count, client.delete_multi, list(pairs))