~hardware-certification/zope3/certify-staging-2.5

« back to all changes in this revision

Viewing changes to src/ZEO/tests/.svn/text-base/speed.py.svn-base

  • Committer: Marc Tardif
  • Date: 2008-04-26 19:03:34 UTC
  • Revision ID: cr3@lime-20080426190334-u16xo4llz56vliqf
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
6
# This software is subject to the provisions of the Zope Public License,
 
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
 
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 
11
# FOR A PARTICULAR PURPOSE
 
12
#
 
13
##############################################################################
 
14
usage="""Test speed of a ZODB storage
 
15
 
 
16
Options:
 
17
 
 
18
    -d file    The data file to use as input.
 
19
               The default is this script.
 
20
 
 
21
    -n n       The number of repititions
 
22
 
 
23
    -s module  A module that defines a 'Storage'
 
24
               attribute, which is an open storage.
 
25
               If not specified, a FileStorage will ne
 
26
               used.
 
27
 
 
28
    -z         Test compressing data
 
29
 
 
30
    -D         Run in debug mode
 
31
 
 
32
    -L         Test loads as well as stores by minimizing
 
33
               the cache after eachrun
 
34
 
 
35
    -M         Output means only
 
36
 
 
37
    -C         Run with a persistent client cache
 
38
 
 
39
    -U         Run ZEO using a Unix domain socket
 
40
 
 
41
    -t n       Number of concurrent threads to run.
 
42
"""
 
43
 
 
44
import asyncore
 
45
import sys, os, getopt, time
 
46
##sys.path.insert(0, os.getcwd())
 
47
 
 
48
import persistent
 
49
import transaction
 
50
import ZODB
 
51
from ZODB.POSException import ConflictError
 
52
from ZEO.tests import forker
 
53
 
 
54
class P(persistent.Persistent):
 
55
    pass
 
56
 
 
57
fs_name = "zeo-speed.fs"
 
58
 
 
59
class ZEOExit(asyncore.file_dispatcher):
 
60
    """Used to exit ZEO.StorageServer when run is done"""
 
61
    def writable(self):
 
62
        return 0
 
63
    def readable(self):
 
64
        return 1
 
65
    def handle_read(self):
 
66
        buf = self.recv(4)
 
67
        assert buf == "done"
 
68
        self.delete_fs()
 
69
        os._exit(0)
 
70
    def handle_close(self):
 
71
        print "Parent process exited unexpectedly"
 
72
        self.delete_fs()
 
73
        os._exit(0)
 
74
    def delete_fs(self):
 
75
        os.unlink(fs_name)
 
76
        os.unlink(fs_name + ".lock")
 
77
        os.unlink(fs_name + ".tmp")
 
78
 
 
79
def work(db, results, nrep, compress, data, detailed, minimize, threadno=None):
 
80
    for j in range(nrep):
 
81
        for r in 1, 10, 100, 1000:
 
82
            t = time.time()
 
83
            conflicts = 0
 
84
 
 
85
            jar = db.open()
 
86
            while 1:
 
87
                try:
 
88
                    transaction.begin()
 
89
                    rt = jar.root()
 
90
                    key = 's%s' % r
 
91
                    if rt.has_key(key):
 
92
                        p = rt[key]
 
93
                    else:
 
94
                        rt[key] = p =P()
 
95
                    for i in range(r):
 
96
                        v = getattr(p, str(i), P())
 
97
                        if compress is not None:
 
98
                            v.d = compress(data)
 
99
                        else:
 
100
                            v.d = data
 
101
                        setattr(p, str(i), v)
 
102
                    transaction.commit()
 
103
                except ConflictError:
 
104
                    conflicts = conflicts + 1
 
105
                else:
 
106
                    break
 
107
            jar.close()
 
108
 
 
109
            t = time.time() - t
 
110
            if detailed:
 
111
                if threadno is None:
 
112
                    print "%s\t%s\t%.4f\t%d" % (j, r, t, conflicts)
 
113
                else:
 
114
                    print "%s\t%s\t%.4f\t%d\t%d" % (j, r, t, conflicts,
 
115
                                                    threadno)
 
116
            results[r].append((t, conflicts))
 
117
            rt=d=p=v=None # release all references
 
118
            if minimize:
 
119
                time.sleep(3)
 
120
                jar.cacheMinimize()
 
121
 
 
122
def main(args):
 
123
    opts, args = getopt.getopt(args, 'zd:n:Ds:LMt:U')
 
124
    s = None
 
125
    compress = None
 
126
    data=sys.argv[0]
 
127
    nrep=5
 
128
    minimize=0
 
129
    detailed=1
 
130
    cache = None
 
131
    domain = 'AF_INET'
 
132
    threads = 1
 
133
    for o, v in opts:
 
134
        if o=='-n': nrep = int(v)
 
135
        elif o=='-d': data = v
 
136
        elif o=='-s': s = v
 
137
        elif o=='-z':
 
138
            import zlib
 
139
            compress = zlib.compress
 
140
        elif o=='-L':
 
141
            minimize=1
 
142
        elif o=='-M':
 
143
            detailed=0
 
144
        elif o=='-D':
 
145
            global debug
 
146
            os.environ['STUPID_LOG_FILE']=''
 
147
            os.environ['STUPID_LOG_SEVERITY']='-999'
 
148
            debug = 1
 
149
        elif o == '-C':
 
150
            cache = 'speed'
 
151
        elif o == '-U':
 
152
            domain = 'AF_UNIX'
 
153
        elif o == '-t':
 
154
            threads = int(v)
 
155
 
 
156
    zeo_pipe = None
 
157
    if s:
 
158
        s = __import__(s, globals(), globals(), ('__doc__',))
 
159
        s = s.Storage
 
160
        server = None
 
161
    else:
 
162
        s, server, pid = forker.start_zeo("FileStorage",
 
163
                                          (fs_name, 1), domain=domain)
 
164
 
 
165
    data=open(data).read()
 
166
    db=ZODB.DB(s,
 
167
               # disable cache deactivation
 
168
               cache_size=4000,
 
169
               cache_deactivate_after=6000,)
 
170
 
 
171
    print "Beginning work..."
 
172
    results={1:[], 10:[], 100:[], 1000:[]}
 
173
    if threads > 1:
 
174
        import threading
 
175
        l = []
 
176
        for i in range(threads):
 
177
            t = threading.Thread(target=work,
 
178
                                 args=(db, results, nrep, compress, data,
 
179
                                       detailed, minimize, i))
 
180
            l.append(t)
 
181
        for t in l:
 
182
            t.start()
 
183
        for t in l:
 
184
            t.join()
 
185
 
 
186
    else:
 
187
        work(db, results, nrep, compress, data, detailed, minimize)
 
188
 
 
189
    if server is not None:
 
190
        server.close()
 
191
        os.waitpid(pid, 0)
 
192
 
 
193
    if detailed:
 
194
        print '-'*24
 
195
    print "num\tmean\tmin\tmax"
 
196
    for r in 1, 10, 100, 1000:
 
197
        times = []
 
198
        for time, conf in results[r]:
 
199
            times.append(time)
 
200
        t = mean(times)
 
201
        print "%d\t%.4f\t%.4f\t%.4f" % (r, t, min(times), max(times))
 
202
 
 
203
def mean(l):
 
204
    tot = 0
 
205
    for v in l:
 
206
        tot = tot + v
 
207
    return tot / len(l)
 
208
 
 
209
##def compress(s):
 
210
##    c = zlib.compressobj()
 
211
##    o = c.compress(s)
 
212
##    return o + c.flush()
 
213
 
 
214
if __name__=='__main__':
 
215
    main(sys.argv[1:])