-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
196 lines (160 loc) · 5.16 KB
/
main.py
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
import argparse
import copy
from datetime import timedelta
import json
from multiprocessing import freeze_support
import random
import sys
import time
import uuid
from scipy import rand
from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import (ClusterOptions, ClusterTimeoutOptions, UpsertOptions)
from multiprocessing.pool import ThreadPool as Pool
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--num-of-requests', help='number of requests', type=int)
parser.add_argument('-t', '--num-of-threads', help='number of threads', type=int)
parser.add_argument('-d', '--debug', help='debug', action='store_true')
parser.add_argument('-r', '--retry', help='number of retries', type=int)
parser.add_argument('-a', '--retry-random-delay', help='random delay for retry', action='store_true')
args = parser.parse_args(sys.argv[1:])
with open('./config.json') as config_file:
configs = json.load(config_file)
bucket_name = configs['bucket']
collection = configs['collection']
scope = configs['scope']
pool_size = args.num_of_threads
num_of_requests = args.num_of_requests
print(f"""start with:
- {pool_size} threads
- {num_of_requests} requests
- debug: {args.debug}
- retry: {args.retry}
- retry random delay: {args.retry_random_delay}
""")
timeout_opts = ClusterTimeoutOptions(kv_timeout=timedelta(seconds=10))
auth = PasswordAuthenticator(
configs['username'],
configs['password']
)
cluster = Cluster('couchbase://localhost', ClusterOptions(auth, timeout_options=timeout_opts))
cluster.wait_until_ready(timedelta(seconds=5))
cb = cluster.bucket(bucket_name)
cb_coll = cb.scope(scope).collection(collection)
cb_coll_default = cb.default_collection()
challenge_id = 'a75d9637-01d5-4714-6ef1-7144a70e537f'
challenge_answer = {
'uuid': challenge_id,
'quizId':'',
'quizTitle':'',
'quizVisibility':'',
'quizType':'',
'gameMode':'',
'quizCreator':'',
'quizCoverMetadata':'',
'quizMaster':{},
'organisationId':'',
'hostOrganisationId':'',
'sessionId':'',
'startTime':'',
'smartPracticeUnlockTimes':'',
'numQuestions':'',
'inGameIndex':'',
'device':'',
'question':'',
'kickedPlayers':[],
'metadata':{
'location':''
},
'ghostAnswersId':'',
'teamMode':'',
'collaborationMode':'',
'liveChallengeId':'',
'liveGameId':''
}
cb_coll.upsert(challenge_id, challenge_answer)
random.seed()
def mutate_document_with_optilock(retry_time=0):
try:
result = cb_coll.get(challenge_id)
res = result.content_as[dict]
res['quizId']=str(uuid.uuid4())
cb_coll.replace(res['uuid'], res, cas=result.cas)
except Exception as e:
if retry_time < args.retry:
if args.retry_random_delay:
time.sleep(random.randint(0, 300)/100)
if mutate_document_with_optilock(retry_time+1):
return True
if args.debug:
print(f'F:{e}')
return False
return True
def mutate_document_with_mutex_lock(retry_time=0):
try:
result = cb_coll.get_and_lock(challenge_id, timedelta(seconds=200))
except Exception as e:
if retry_time < args.retry and mutate_document_with_mutex_lock(retry_time+1):
return True
if args.debug:
print(f'F:{e}')
return False
try:
res = result.content_as[dict]
res['quizId']=str(uuid.uuid4())
cb_coll.replace(res['uuid'], res, cas=result.cas)
except Exception as e:
if args.debug:
print(f'F:{e}')
return False
return True
def write_unique_doc(retry_time=0):
try:
doc = copy.deepcopy(challenge_answer)
doc['uuid']=str(uuid.uuid4())
cb_coll.upsert(doc['uuid'], doc)
except Exception as e:
if retry_time < args.retry:
write_unique_doc(retry_time+1)
return False
return True
def read_doc(retry_time=0):
try:
cb_coll.get(challenge_id)
except Exception as e:
if retry_time < args.retry:
read_doc(retry_time+1)
return False
return True
def worker_opt():
res = mutate_document_with_optilock()
return 1 if res else 0
def worker_mutex():
res = mutate_document_with_mutex_lock()
return 1 if res else 0
def shared_doc_readonly():
res = read_doc()
return 1 if res else 0
def none_shared_doc_writeonly():
res = write_unique_doc()
return 1 if res else 0
benchmark_func = {
'shared_doc_opt': worker_opt,
'shared_doc_mutex': worker_mutex,
'shared_doc_readonly': shared_doc_readonly,
'none_shared_doc_writeonly': none_shared_doc_writeonly
}
def benchmark(title, worker):
print(f'{title} started ...')
pool = Pool(pool_size)
start = time.time_ns()
results = [pool.apply_async(worker, ()) for i in range(num_of_requests)]
pool.close()
pool.join()
succeed = sum([res.get(timeout=1) for res in results])
time_lapse = time.time_ns() - start
print(f'Send with {pool_size} threads, finished in {time_lapse/1000000000}\n{succeed}/{num_of_requests} succeeded, with througput {succeed * 1000000000/time_lapse} ops')
if __name__ == '__main__':
for key, value in benchmark_func.items():
benchmark(key, value)