forked from couchbase/couchbase-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.py
More file actions
executable file
·129 lines (104 loc) · 4.07 KB
/
Copy pathbench.py
File metadata and controls
executable file
·129 lines (104 loc) · 4.07 KB
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
#
# Copyright 2013, Couchbase, Inc.
# All Rights Reserved
#
# 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.
#
#!/usr/bin/env python
import argparse
from threading import Thread
from time import sleep, time
from couchbase.bucket import Bucket, FMT_BYTES
from couchbase.transcoder import Transcoder
ap = argparse.ArgumentParser()
ap.add_argument('-t', '--threads', default=4, type=int,
help="Number of threads to spawn. 0 means no threads "
"but workload will still run in the main thread")
ap.add_argument('-d', '--delay', default=0, type=float,
help="Number of seconds to wait between each op. "
"may be a fraction")
ap.add_argument('-U', '--connstr', default='couchbase://localhost/default',
help="Connection string")
ap.add_argument('-p', '--password', default=None, type=str)
ap.add_argument('-D', '--duration', default=10, type=int,
help="Duration of run (in seconds)")
ap.add_argument('-T', '--transcoder', default=False,
action='store_true',
help="Use the Transcoder object rather than built-in "
"conversion routines")
ap.add_argument('--ksize', default=12, type=int,
help="Key size to use")
ap.add_argument('--vsize', default=128, type=int,
help="Value size to use")
ap.add_argument('--iops', default=False, action='store_true',
help="Use Pure-Python IOPS plugin")
ap.add_argument('--batch', '-N', default=1, type=int,
help="Number of commands to schedule per iteration")
options = ap.parse_args()
DO_UNLOCK_GIL = options.threads > 0
TC = Transcoder()
class Worker(Thread):
def __init__(self):
self.delay = options.delay
self.key = 'K' * options.ksize
self.value = b'V' * options.vsize
self.kv = {}
for x in range(options.batch):
self.kv[self.key + str(x)] = self.value
self.wait_time = 0
self.opcount = 0
connopts = { "connstr" : options.connstr,
"unlock_gil": DO_UNLOCK_GIL }
if options.iops:
connopts["experimental_gevent_support"] = True
self.cb = Bucket(**connopts)
if options.transcoder:
self.cb.transcoder = TC
self.end_time = time() + options.duration
super(Worker, self).__init__()
def run(self, *args, **kwargs):
cb = self.cb
while time() < self.end_time:
begin_time = time()
rv = cb.upsert_multi(self.kv, format=FMT_BYTES)
assert rv.all_ok, "Operation failed: "
self.wait_time += time() - begin_time
if self.delay:
sleep(self.delay)
self.opcount += options.batch
global_begin = None
worker_threads = []
if not options.threads:
# No threding requested:
w = Worker()
worker_threads.append(w)
global_begin = time()
w.run()
else:
for x in range(options.threads):
worker_threads.append(Worker())
global_begin = time()
for t in worker_threads:
t.start()
for t in worker_threads:
t.join()
global_duration = time() - global_begin
total_ops = sum([w.opcount for w in worker_threads])
total_time = 0
for t in worker_threads:
total_time += t.wait_time
print("Total run took an absolute time of %0.2f seconds" % (global_duration,))
print("Did a total of %d operations" % (total_ops,))
print("Total wait time of %0.2f seconds" % (total_time,))
print("[WAIT] %0.2f ops/second" % (float(total_ops)/float(total_time),))
print("[ABS] %0.2f ops/second" % (float(total_ops)/float(global_duration),))