PageRenderTime 294ms CodeModel.GetById 91ms app.highlight 12ms RepoModel.GetById 129ms app.codeStats 0ms

/historical/hive_speed.py

https://bitbucket.org/lindenlab/apiary/
Python | 129 lines | 83 code | 21 blank | 25 comment | 13 complexity | c27044b03fa0eaf660bd41535d3d67f4 MD5 | raw file
  1#
  2# $LicenseInfo:firstyear=2010&license=mit$
  3# 
  4# Copyright (c) 2010, Linden Research, Inc.
  5# 
  6# Permission is hereby granted, free of charge, to any person obtaining a copy
  7# of this software and associated documentation files (the "Software"), to deal
  8# in the Software without restriction, including without limitation the rights
  9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10# copies of the Software, and to permit persons to whom the Software is
 11# furnished to do so, subject to the following conditions:
 12# 
 13# The above copyright notice and this permission notice shall be included in
 14# all copies or substantial portions of the Software.
 15# 
 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22# THE SOFTWARE.
 23# $/LicenseInfo$
 24#
 25
 26import os
 27import random
 28import time
 29
 30import hive
 31
 32
 33class StatWorker(hive.Worker):
 34    def __init__(self, options, arguments):
 35        hive.Worker.__init__(self, options, arguments)
 36        self._events = 0
 37        self._bytes = 0
 38    
 39    def start(self):
 40        self._events = 0
 41        self._bytes = 0
 42    
 43    def event(self, data):
 44        self._events += 1
 45        self._bytes += len(data)
 46        #time.sleep(0.01)
 47        
 48    def end(self):
 49        return ("%s,%d,%d" % (self._id, self._events, self._bytes))
 50        
 51
 52class StatCentral(hive.Central):
 53    def __init__(self, options, arguments):
 54        hive.Central.__init__(self, options, arguments)
 55        self.results = {}
 56        self._nseq = 2000
 57        self._nevents = 5
 58        self._nlen = 800
 59        self._seqcount = 0
 60        self._seqs = []
 61        self._seqevents = {}
 62        self._data = None
 63            
 64    def next(self):
 65        shy = self._seqcount < self._nseq
 66        empty = len(self._seqs) == 0
 67        if shy and (empty or random.randint(0,10) == 0):
 68            self._seqcount += 1
 69            seq = "Q%d" % (self._seqcount)
 70            self._seqs.append(seq)
 71            self._seqevents[seq] = 0
 72            self.start(seq)
 73            return True
 74
 75        if empty:
 76            return False
 77        
 78        seq = random.choice(self._seqs)
 79        i = self._seqevents[seq]
 80        if i >= self._nevents:
 81            del self._seqevents[seq]
 82            self._seqs = self._seqevents.keys()
 83            self.end(seq)
 84            return True
 85        
 86        self._seqevents[seq] = i + 1
 87        self.event(seq, self.gendata())
 88        return True
 89    
 90    def result(self, seq, data):
 91        (worker, events, bytes) = data.split(',')
 92        if worker not in self.results:
 93            self.results[worker] = { 'sequences': 0, 'events': 0, 'bytes': 0 }
 94        r = self.results[worker]
 95        r['sequences'] += 1
 96        r['events'] += int(events)
 97        r['bytes'] += int(bytes)
 98
 99    def gendata(self):
100        if self._data is None:
101            s = "abcdefghijklmnopqrstuvwxyz"
102            while len(s) < self._nlen:
103                s += s
104            self._data = s[0:self._nlen]
105        return self._data
106        
107    def main(self):
108        t = - time.time()
109        c = - time.clock()
110        hive.Central.main(self)
111        c += time.clock()
112        t += time.time()
113
114        print ("Timing: %f process clock, %f wall clock" % (c, t))
115        print ("Central: %d sequences @ %d events @ %d bytes"
116                % (self._seqcount, self._nevents, self._nlen))
117        for k,r in self.results.iteritems():
118            print ("Worker %s:" % k), (
119                '%(sequences)d sequences @ %(events)d events @ %(bytes)d bytes'
120                % r)
121
122
123class StatHive(hive.Hive):
124    def __init__(self):
125        hive.Hive.__init__(self, StatCentral, StatWorker)
126
127
128if __name__ == '__main__':
129    StatHive().main()