File: bench.py

package info (click to toggle)
django-cacheops 7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: python: 3,189; sh: 7; makefile: 4
file content (198 lines) | stat: -rw-r--r-- 5,693 bytes parent folder | download | duplicates (2)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from cacheops import invalidate_obj, invalidate_model
from cacheops.conf import settings
from cacheops.redis import redis_client
from cacheops.tree import dnfs

from .models import Category, Post, Extra


posts = list(Post.objects.cache().all())
posts_pickle = settings.CACHEOPS_SERIALIZER.dumps(posts)

def do_pickle():
    settings.CACHEOPS_SERIALIZER.dumps(posts)

def do_unpickle():
    settings.CACHEOPS_SERIALIZER.loads(posts_pickle)


get_key = Category.objects.filter(pk=1).order_by()._cache_key()
def invalidate_get():
    redis_client.delete(get_key)

def do_get():
    Category.objects.cache().get(pk=1)

def do_get_nocache():
    Category.objects.nocache().get(pk=1)


c = Category.objects.first()
def invalidate_count():
    invalidate_obj(c)

def do_count():
    Category.objects.cache().count()

def do_count_nocache():
    Category.objects.nocache().count()


fetch_qs = Category.objects.all()
fetch_key = fetch_qs._cache_key()

def invalidate_fetch():
    redis_client.delete(fetch_key)

def do_fetch():
    list(Category.objects.cache().all())

def do_fetch_nocache():
    list(Category.objects.nocache().all())

def do_fetch_construct():
    Category.objects.all()

def do_fetch_cache_key():
    fetch_qs._cache_key()

filter_qs = Category.objects.filter(pk=1)
def do_filter_cache_key():
    filter_qs._cache_key()


def do_common_construct():
    return Category.objects.filter(pk=1).exclude(title__contains='Hi').order_by('title')[:20]

def do_common_inplace():
    return Category.objects.inplace() \
                   .filter(pk=1).exclude(title__contains='Hi').order_by('title')[:20]

common_qs = do_common_construct()
common_key = common_qs._cache_key()

def do_common_cache_key():
    common_qs._cache_key()

def do_common_dnfs():
    dnfs(common_qs)

def do_common():
    qs = Category.objects.filter(pk=1).exclude(title__contains='Hi').order_by('title').cache()[:20]
    list(qs)

def do_common_nocache():
    qs = Category.objects.filter(pk=1).exclude(title__contains='Hi').order_by('title') \
            .nocache()[:20]
    list(qs)

def invalidate_common():
    redis_client.delete(common_key)

def prepare_obj():
    return Category.objects.cache().get(pk=1)

def do_invalidate_obj(obj):
    invalidate_obj(obj)

def do_save_obj(obj):
    obj.save()


### Complex queryset

from django.db.models import Q

def do_complex_construct():
    return Post.objects.filter(id__gt=1, title='Hi').exclude(category__in=[10, 20]) \
                       .filter(Q(id__range=(10, 20)) | ~Q(title__contains='abc'))   \
                       .select_related('category').prefetch_related('category')     \
                       .order_by('title')[:10]

def do_complex_inplace():
    return Post.objects.inplace()                                                   \
                       .filter(id__gt=1, title='Hi').exclude(category__in=[10, 20]) \
                       .filter(Q(id__range=(10, 20)) | ~Q(title__contains='abc'))   \
                       .select_related('category').prefetch_related('category')     \
                       .order_by('title')[:10]

complex_qs = do_complex_construct()
def do_complex_cache_key():
    complex_qs._cache_key()

def do_complex_dnfs():
    dnfs(complex_qs)


### More invalidation

def prepare_cache():
    def _variants(*args, **kwargs):
        qs = Extra.objects.cache().filter(*args, **kwargs)
        qs.count()
        list(qs)
        list(qs[:2])
        list(qs.values())

    _variants(pk=1)
    _variants(post=1)
    _variants(tag=5)
    _variants(to_tag=10)

    _variants(pk=1, post=1)
    _variants(pk=1, tag=5)
    _variants(post=1, tag=5)

    _variants(pk=1, post=1, tag=5)
    _variants(pk=1, post=1, to_tag=10)

    _variants(Q(pk=1) | Q(tag=5))
    _variants(Q(pk=1) | Q(tag=1))
    _variants(Q(pk=1) | Q(tag=2))
    _variants(Q(pk=1) | Q(tag=3))
    _variants(Q(pk=1) | Q(tag=4))

    return Extra.objects.cache().get(pk=1)

def do_invalidate_model(obj):
    invalidate_model(obj.__class__)


TESTS = [
    ('pickle', {'run': do_pickle}),
    ('unpickle', {'run': do_unpickle}),

    ('get_nocache', {'run': do_get_nocache}),
    ('get_hit', {'prepare_once': do_get, 'run': do_get}),
    ('get_miss', {'prepare': invalidate_get, 'run': do_get}),

    ('count_nocache', {'run': do_count_nocache}),
    ('count_hit', {'prepare_once': do_count, 'run': do_count}),
    ('count_miss', {'prepare': invalidate_count, 'run': do_count}),

    ('fetch_construct', {'run': do_fetch_construct}),
    ('fetch_nocache', {'run': do_fetch_nocache}),
    ('fetch_hit', {'prepare_once': do_fetch, 'run': do_fetch}),
    ('fetch_miss', {'prepare': invalidate_fetch, 'run': do_fetch}),
    ('fetch_cache_key', {'run': do_fetch_cache_key}),

    ('filter_cache_key', {'run': do_filter_cache_key}),
    ('common_construct', {'run': do_common_construct}),
    ('common_inplace', {'run': do_common_inplace}),
    ('common_cache_key', {'run': do_common_cache_key}),
    ('common_dnfs', {'run': do_common_dnfs}),
    ('common_nocache', {'run': do_common_nocache}),
    ('common_hit', {'prepare_once': do_common, 'run': do_common}),
    ('common_miss', {'prepare': invalidate_common, 'run': do_common}),

    ('invalidate_obj', {'prepare': prepare_obj, 'run': do_invalidate_obj}),
    ('save_obj', {'prepare': prepare_obj, 'run': do_save_obj}),

    ('complex_construct', {'run': do_complex_construct}),
    ('complex_inplace', {'run': do_complex_inplace}),
    ('complex_cache_key', {'run': do_complex_cache_key}),
    ('complex_dnfs', {'run': do_complex_dnfs}),

    ('big_invalidate', {'prepare': prepare_cache, 'run': do_invalidate_obj}),
    ('model_invalidate', {'prepare': prepare_cache, 'run': do_invalidate_model}),
]