<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[13609] CalendarServer/trunk</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/13609">13609</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-06-05 13:05:39 -0700 (Thu, 05 Jun 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Adds dashboard support for directory service stats</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarserverdashboard_servicepy">CalendarServer/trunk/calendarserver/dashboard_service.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolsdashboardpy">CalendarServer/trunk/calendarserver/tools/dashboard.py</a></li>
<li><a href="#CalendarServertrunktxdavdpsclientpy">CalendarServer/trunk/txdav/dps/client.py</a></li>
<li><a href="#CalendarServertrunktxdavdpscommandspy">CalendarServer/trunk/txdav/dps/commands.py</a></li>
<li><a href="#CalendarServertrunktxdavdpsserverpy">CalendarServer/trunk/txdav/dps/server.py</a></li>
<li><a href="#CalendarServertrunktxdavwhoaugmentpy">CalendarServer/trunk/txdav/who/augment.py</a></li>
<li><a href="#CalendarServertrunktxdavwhodirectorypy">CalendarServer/trunk/txdav/who/directory.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarserverdashboard_servicepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/dashboard_service.py (13608 => 13609)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/dashboard_service.py        2014-06-04 22:51:50 UTC (rev 13608)
+++ CalendarServer/trunk/calendarserver/dashboard_service.py        2014-06-05 20:05:39 UTC (rev 13609)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> a server admin or developer would like to keep an eye on.
</span><span class="cx"> """
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> class DashboardProtocol (LineReceiver):
</span><span class="cx"> """
</span><span class="cx"> A protocol that receives a line containing a JSON object representing a request,
</span><span class="lines">@@ -153,7 +154,21 @@
</span><span class="cx"> return succeed({"workers": loads, "level": level})
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def data_directory(self):
+ """
+ Return a summary of directory service calls.
</ins><span class="cx">
</span><ins>+ @return: the JSON result.
+ @rtype: L{str}
+ """
+ directory = self.factory.store.directoryService()
+ if hasattr(directory, "stats"):
+ return succeed(directory.stats())
+ else:
+ return succeed({})
+
+
+
</ins><span class="cx"> class DashboardServer(Factory):
</span><span class="cx">
</span><span class="cx"> protocol = DashboardProtocol
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolsdashboardpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/dashboard.py (13608 => 13609)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/dashboard.py        2014-06-04 22:51:50 UTC (rev 13608)
+++ CalendarServer/trunk/calendarserver/tools/dashboard.py        2014-06-05 20:05:39 UTC (rev 13609)
</span><span class="lines">@@ -738,10 +738,12 @@
</span><span class="cx">
</span><span class="cx"> s = " {:<12}{:>8}{:>16}".format(
</span><span class="cx"> "Total:",
</span><del>- sum([
- record["unacknowledged"] + record["acknowledged"]
- for record in records
- ]),
</del><ins>+ sum(
+ [
+ record["unacknowledged"] + record["acknowledged"]
+ for record in records
+ ]
+ ),
</ins><span class="cx"> sum([record["total"] for record in records]),
</span><span class="cx"> )
</span><span class="cx"> if self.usesCurses:
</span><span class="lines">@@ -919,11 +921,103 @@
</span><span class="cx"> self.lastResult = records
</span><span class="cx">
</span><span class="cx">
</span><ins>+class DirectoryStatsWindow(BaseWindow):
+ """
+ Displays the status of the server's directory service calls
+ """
+
+ help = "display directory service stats"
+ clientItem = "directory"
+ FORMAT_WIDTH = 89
+
+
+ def makeWindow(self, top=0, left=0):
+ nlines = len(defaultIfNone(self.readItem("directory"), {}))
+ self.rowCount = nlines
+ self._createWindow(
+ "Directory Service", self.rowCount + 6, ncols=self.FORMAT_WIDTH,
+ begin_y=top, begin_x=left
+ )
+ return self
+
+
+ def update(self):
+ records = defaultIfNone(self.clientData(), {})
+ if len(records) != self.rowCount:
+ self.needsReset = True
+ return
+
+ self.iter += 1
+
+ if self.usesCurses:
+ self.window.erase()
+ self.window.border()
+ self.window.addstr(0, 2, self.title + " {} ({})".format(len(records), self.iter,))
+
+ x = 1
+ y = 1
+ s1 = " {:<40}{:>15}{:>15}{:>15} ".format(
+ "Method", "Calls", "Total", "Average"
+ )
+ s2 = " {:<40}{:>15}{:>15}{:>15} ".format(
+ "", "", "(sec)", "(sec)"
+ )
+ if self.usesCurses:
+ self.window.addstr(y, x, s1, curses.A_REVERSE)
+ self.window.addstr(y + 1, x, s2, curses.A_REVERSE)
+ else:
+ print(s1)
+ print(s2)
+ y += 2
+
+ overallCount = 0
+ overallTimeSpent = 0.0
+
+ for methodName, (count, timeSpent) in sorted(records.items(), key=lambda x: x[0]):
+ overallCount += count
+ overallTimeSpent += timeSpent
+
+ s = " {:<40}{:>15d}{:>15.1f}{:>15.5f} ".format(
+ methodName,
+ count,
+ timeSpent,
+ timeSpent / count,
+ )
+ try:
+ if self.usesCurses:
+ self.window.addstr(y, x, s)
+ else:
+ print(s)
+ except curses.error:
+ pass
+ y += 1
+
+
+ s = " {:<40}{:>15d}{:>15.1f}{:>15.5f} ".format(
+ "Total:",
+ overallCount,
+ overallTimeSpent,
+ safeDivision(overallTimeSpent, overallCount, 1.0)
+ )
+ if self.usesCurses:
+ self.window.hline(y, x, "-", self.FORMAT_WIDTH - 2)
+ y += 1
+ self.window.addstr(y, x, s)
+ else:
+ print(s)
+ y += 1
+
+ if self.usesCurses:
+ self.window.refresh()
+
+
+
</ins><span class="cx"> Dashboard.registerWindow(SystemWindow, "s")
</span><span class="cx"> Dashboard.registerWindow(RequestStatsWindow, "r")
</span><span class="cx"> Dashboard.registerWindow(JobsWindow, "j")
</span><span class="cx"> Dashboard.registerWindow(AssignmentsWindow, "w")
</span><span class="cx"> Dashboard.registerWindow(HTTPSlotsWindow, "c")
</span><ins>+Dashboard.registerWindow(DirectoryStatsWindow, "d")
</ins><span class="cx"> Dashboard.registerWindow(HelpWindow, "h")
</span><span class="cx">
</span><span class="cx">
</span></span></pre></div>
<a id="CalendarServertrunktxdavdpsclientpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/dps/client.py (13608 => 13609)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/dps/client.py        2014-06-04 22:51:50 UTC (rev 13608)
+++ CalendarServer/trunk/txdav/dps/client.py        2014-06-05 20:05:39 UTC (rev 13609)
</span><span class="lines">@@ -39,7 +39,7 @@
</span><span class="cx"> RecordsMatchingTokensCommand, RecordsMatchingFieldsCommand,
</span><span class="cx"> MembersCommand, GroupsCommand, SetMembersCommand,
</span><span class="cx"> VerifyPlaintextPasswordCommand, VerifyHTTPDigestCommand,
</span><del>- WikiAccessForUID, ContinuationCommand
</del><ins>+ WikiAccessForUIDCommand, ContinuationCommand
</ins><span class="cx"> )
</span><span class="cx"> from txdav.who.directory import (
</span><span class="cx"> CalendarDirectoryRecordMixin, CalendarDirectoryServiceMixin
</span><span class="lines">@@ -401,7 +401,7 @@
</span><span class="cx"> def accessForRecord(self, record):
</span><span class="cx"> log.debug("DPS Client accessForRecord")
</span><span class="cx"> return self.service._call(
</span><del>- WikiAccessForUID,
</del><ins>+ WikiAccessForUIDCommand,
</ins><span class="cx"> self._convertAccess,
</span><span class="cx"> wikiUID=self.uid.encode("utf-8"),
</span><span class="cx"> uid=record.uid.encode("utf-8")
</span></span></pre></div>
<a id="CalendarServertrunktxdavdpscommandspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/dps/commands.py (13608 => 13609)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/dps/commands.py        2014-06-04 22:51:50 UTC (rev 13608)
+++ CalendarServer/trunk/txdav/dps/commands.py        2014-06-05 20:05:39 UTC (rev 13609)
</span><span class="lines">@@ -191,7 +191,7 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><del>-class WikiAccessForUID(amp.Command):
</del><ins>+class WikiAccessForUIDCommand(amp.Command):
</ins><span class="cx"> arguments = [
</span><span class="cx"> ('wikiUID', amp.String()),
</span><span class="cx"> ('uid', amp.String()),
</span></span></pre></div>
<a id="CalendarServertrunktxdavdpsserverpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/dps/server.py (13608 => 13609)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/dps/server.py        2014-06-04 22:51:50 UTC (rev 13608)
+++ CalendarServer/trunk/txdav/dps/server.py        2014-06-05 20:05:39 UTC (rev 13609)
</span><span class="lines">@@ -36,7 +36,7 @@
</span><span class="cx"> RecordsMatchingTokensCommand, RecordsMatchingFieldsCommand,
</span><span class="cx"> MembersCommand, GroupsCommand, SetMembersCommand,
</span><span class="cx"> VerifyPlaintextPasswordCommand, VerifyHTTPDigestCommand,
</span><del>- WikiAccessForUID, ContinuationCommand
</del><ins>+ WikiAccessForUIDCommand, ContinuationCommand
</ins><span class="cx"> # UpdateRecordsCommand, RemoveRecordsCommand
</span><span class="cx"> )
</span><span class="cx"> from txdav.who.util import directoryFromConfig
</span><span class="lines">@@ -430,7 +430,7 @@
</span><span class="cx"> returnValue(response)
</span><span class="cx">
</span><span class="cx">
</span><del>- @WikiAccessForUID.responder
</del><ins>+ @WikiAccessForUIDCommand.responder
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def wikiAccessForUID(self, wikiUID, uid):
</span><span class="cx"> wikiUID = wikiUID.decode("utf-8")
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhoaugmentpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/augment.py (13608 => 13609)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/augment.py        2014-06-04 22:51:50 UTC (rev 13608)
+++ CalendarServer/trunk/txdav/who/augment.py        2014-06-05 20:05:39 UTC (rev 13609)
</span><span class="lines">@@ -23,6 +23,8 @@
</span><span class="cx"> "AugmentedDirectoryService",
</span><span class="cx"> ]
</span><span class="cx">
</span><ins>+import time
+
</ins><span class="cx"> from zope.interface import implementer
</span><span class="cx">
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue
</span><span class="lines">@@ -47,6 +49,47 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><ins>+def timed(f):
+ """
+ A decorator which keeps track of the wrapped function's call count and
+ total duration
+ """
+
+ def recordTiming(result, key, startTime):
+ """
+ Figures out how much time to add to the total time spent within the
+ method identified by key and stores that in the timings dict.
+
+ @param result: the result of the wrapped method
+ @param timings: the dictionary to store timings in
+ @type timings: C{dict}
+ @param key: the method name
+ @type key: C{str}
+ @param startTime: the start time of the call in seconds
+ @type startTime: C{float}
+ """
+ timings = AugmentedDirectoryService._timings
+ if key not in timings:
+ timings[key] = (0, 0.0)
+ count, timeSpent = timings[key]
+ count += 1
+ duration = (time.time() - startTime)
+ timeSpent += duration
+ timings[key] = (count, timeSpent)
+ return result
+
+
+ def timingWrapper(self, *args, **kwds):
+ """
+ Records the start time of the call and the method's name
+ """
+ d = f(self, *args, **kwds)
+ d.addBoth(recordTiming, f.func_name, time.time())
+ return d
+
+ return timingWrapper
+
+
</ins><span class="cx"> @implementer(IDirectoryService, IStoreDirectoryService)
</span><span class="cx"> class AugmentedDirectoryService(
</span><span class="cx"> BaseDirectoryService, CalendarDirectoryServiceMixin
</span><span class="lines">@@ -63,7 +106,9 @@
</span><span class="cx"> FieldName,
</span><span class="cx"> ))
</span><span class="cx">
</span><ins>+ _timings = {}
</ins><span class="cx">
</span><ins>+
</ins><span class="cx"> def __init__(self, directory, store, augmentDB):
</span><span class="cx"> BaseDirectoryService.__init__(self, directory.realmName)
</span><span class="cx"> self._directory = directory
</span><span class="lines">@@ -71,6 +116,10 @@
</span><span class="cx"> self._augmentDB = augmentDB
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def stats(self):
+ return self._timings
+
+
</ins><span class="cx"> @property
</span><span class="cx"> def recordType(self):
</span><span class="cx"> # Defer to the directory service we're augmenting
</span><span class="lines">@@ -104,6 +153,7 @@
</span><span class="cx"> returnValue(augmented)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def recordWithUID(self, uid):
</span><span class="cx"> # MOVE2WHO, REMOVE THIS:
</span><span class="lines">@@ -116,6 +166,7 @@
</span><span class="cx"> returnValue(record)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def recordWithGUID(self, guid):
</span><span class="cx"> record = yield self._directory.recordWithGUID(guid)
</span><span class="lines">@@ -123,6 +174,7 @@
</span><span class="cx"> returnValue(record)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def recordsWithRecordType(self, recordType):
</span><span class="cx"> records = yield self._directory.recordsWithRecordType(recordType)
</span><span class="lines">@@ -133,6 +185,7 @@
</span><span class="cx"> returnValue(augmented)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def recordWithShortName(self, recordType, shortName):
</span><span class="cx"> # MOVE2WHO, REMOVE THIS:
</span><span class="lines">@@ -147,6 +200,7 @@
</span><span class="cx"> returnValue(record)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def recordsWithEmailAddress(self, emailAddress):
</span><span class="cx"> # MOVE2WHO, REMOVE THIS:
</span><span class="lines">@@ -162,6 +216,28 @@
</span><span class="cx"> returnValue(augmented)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
+ def recordWithCalendarUserAddress(self, *args, **kwds):
+ return CalendarDirectoryServiceMixin.recordWithCalendarUserAddress(
+ self, *args, **kwds
+ )
+
+
+ @timed
+ def recordsMatchingTokens(self, *args, **kwds):
+ return CalendarDirectoryServiceMixin.recordsMatchingTokens(
+ self, *args, **kwds
+ )
+
+
+ @timed
+ def recordsMatchingFields(self, *args, **kwds):
+ return CalendarDirectoryServiceMixin.recordsMatchingFields(
+ self, *args, **kwds
+ )
+
+
+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def updateRecords(self, records, create=False):
</span><span class="cx"> """
</span><span class="lines">@@ -364,6 +440,7 @@
</span><span class="cx"> self._baseRecord = baseRecord
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def members(self):
</span><span class="cx"> augmented = []
</span><span class="lines">@@ -375,6 +452,7 @@
</span><span class="cx"> returnValue(augmented)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def groups(self):
</span><span class="cx"> augmented = []
</span><span class="lines">@@ -397,13 +475,16 @@
</span><span class="cx"> returnValue(augmented)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> def verifyPlaintextPassword(self, password):
</span><span class="cx"> return self._baseRecord.verifyPlaintextPassword(password)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> def verifyHTTPDigest(self, *args):
</span><span class="cx"> return self._baseRecord.verifyHTTPDigest(*args)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @timed
</ins><span class="cx"> def accessForRecord(self, record):
</span><span class="cx"> return self._baseRecord.accessForRecord(record)
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhodirectorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/directory.py (13608 => 13609)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/directory.py        2014-06-04 22:51:50 UTC (rev 13608)
+++ CalendarServer/trunk/txdav/who/directory.py        2014-06-05 20:05:39 UTC (rev 13609)
</span><span class="lines">@@ -459,7 +459,7 @@
</span><span class="cx"> return candidate
</span><span class="cx">
</span><span class="cx"> # fall back to using the first one
</span><del>- return sortedCuas[0] if sortedCuas else None # groups may not have cua
</del><ins>+ return sortedCuas[0] if sortedCuas else None # groups may not have cua
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> def enabledAsOrganizer(self):
</span></span></pre>
</div>
</div>
</body>
</html>