Converted server/profiling/ to Google-style docstrings, as per #709.

This commit is contained in:
Griatch 2015-06-23 13:39:45 +02:00
parent 6d0122906f
commit ccae355175
2 changed files with 76 additions and 15 deletions

View file

@ -162,7 +162,13 @@ Setup:
ICOUNT = 0 ICOUNT = 0
def idcounter(): def idcounter():
"makes unique ids" """
Makes unique ids.
Returns:
count (int): A globally unique counter.
"""
global ICOUNT global ICOUNT
ICOUNT += 1 ICOUNT += 1
return str(ICOUNT) return str(ICOUNT)
@ -170,17 +176,29 @@ def idcounter():
GCOUNT = 0 GCOUNT = 0
def gidcounter(): def gidcounter():
"makes globally unique ids" """
Makes globally unique ids.
Returns:
count (int); A globally unique counter.
"""
global GCOUNT global GCOUNT
GCOUNT += 1 GCOUNT += 1
return "%s-%s" % (time.strftime(DATESTRING), GCOUNT) return "%s-%s" % (time.strftime(DATESTRING), GCOUNT)
def makeiter(obj): def makeiter(obj):
"makes everything iterable" """
if not hasattr(obj, '__iter__'): Makes everything iterable.
return [obj]
return obj Args:
obj (any): Object to turn iterable.
Returns:
iterable (iterable): An iterable object.
"""
return obj if hasattr(obj, '__iter__') else [obj]
#------------------------------------------------------------ #------------------------------------------------------------
# Client classes # Client classes
@ -191,10 +209,14 @@ class DummyClient(telnet.StatefulTelnetProtocol):
Handles connection to a running Evennia server, Handles connection to a running Evennia server,
mimicking a real player by sending commands on mimicking a real player by sending commands on
a timer. a timer.
""" """
def connectionMade(self): def connectionMade(self):
"""
Called when connection is first established.
"""
# public properties # public properties
self.cid = idcounter() self.cid = idcounter()
self.key = "Dummy-%s" % self.cid self.key = "Dummy-%s" % self.cid
@ -215,7 +237,14 @@ class DummyClient(telnet.StatefulTelnetProtocol):
reactor.addSystemEventTrigger('before', 'shutdown', self.logout) reactor.addSystemEventTrigger('before', 'shutdown', self.logout)
def dataReceived(self, data): def dataReceived(self, data):
"Wait to start stepping until the server actually responds" """
Called when data comes in over the protocol. We wait to start
stepping until the server actually responds
Args:
data (str): Incoming data.
"""
if not self._connected and not data.startswith(chr(255)): if not self._connected and not data.startswith(chr(255)):
# wait until we actually get text back (not just telnet # wait until we actually get text back (not just telnet
# negotiation) # negotiation)
@ -227,20 +256,40 @@ class DummyClient(telnet.StatefulTelnetProtocol):
d.start(timestep, now=True).addErrback(self.error) d.start(timestep, now=True).addErrback(self.error)
def connectionLost(self, reason): def connectionLost(self, reason):
"loosing the connection" """
Called when loosing the connection.
Args:
reason (str): Reason for loosing connection.
"""
if not self._logging_out: if not self._logging_out:
print "client %s(%s) lost connection (%s)" % (self.key, self.cid, reason) print "client %s(%s) lost connection (%s)" % (self.key, self.cid, reason)
def error(self, err): def error(self, err):
"error callback" """
Error callback.
Args:
err (Failure): Error instance.
"""
print err print err
def counter(self): def counter(self):
"produces a unique id, also between clients" """
Produces a unique id, also between clients.
Returns:
counter (int): A unique counter.
"""
return gidcounter() return gidcounter()
def logout(self): def logout(self):
"Causes the client to log out of the server. Triggered by ctrl-c signal." """
Causes the client to log out of the server. Triggered by ctrl-c signal.
"""
self._logging_out = True self._logging_out = True
cmd = self._logout(self) cmd = self._logout(self)
print "client %s(%s) logout (%s actions)" % (self.key, self.cid, self.istep) print "client %s(%s) logout (%s actions)" % (self.key, self.cid, self.istep)
@ -248,9 +297,10 @@ class DummyClient(telnet.StatefulTelnetProtocol):
def step(self): def step(self):
""" """
Perform a step. This is called repeatedly by the runner Perform a step. This is called repeatedly by the runner and
and causes the client to issue commands to the server. causes the client to issue commands to the server. This holds
This holds all "intelligence" of the dummy client. all "intelligence" of the dummy client.
""" """
global NLOGGED_IN global NLOGGED_IN
@ -294,7 +344,13 @@ class DummyFactory(protocol.ClientFactory):
#------------------------------------------------------------ #------------------------------------------------------------
def start_all_dummy_clients(nclients): def start_all_dummy_clients(nclients):
"""
Initialize all clients, connect them and start to step them
Args:
nclients (int): Number of dummy clients to connect.
"""
global NCLIENTS global NCLIENTS
NCLIENTS = int(nclients) NCLIENTS = int(nclients)
actions = DUMMYRUNNER_SETTINGS.ACTIONS actions = DUMMYRUNNER_SETTINGS.ACTIONS

View file

@ -18,7 +18,12 @@ LOGFILE = "logs/memoryusage.log"
INTERVAL = 30 # log every 30 seconds INTERVAL = 30 # log every 30 seconds
class Memplot(ev.Script): class Memplot(ev.Script):
"""
Describes a memory plotting action.
"""
def at_script_creation(self): def at_script_creation(self):
"Called at script creation"
self.key = "memplot" self.key = "memplot"
self.desc = "Save server memory stats to file" self.desc = "Save server memory stats to file"
self.start_delay = False self.start_delay = False
@ -27,7 +32,7 @@ class Memplot(ev.Script):
self.db.starttime = time.time() self.db.starttime = time.time()
def at_repeat(self): def at_repeat(self):
"Regularly save memory statistics."
pid = os.getpid() pid = os.getpid()
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0 # resident memory rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0 # resident memory
vmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "vsz")).read()) / 1000.0 # virtual memory vmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "vsz")).read()) / 1000.0 # virtual memory