<!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>[13855] 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/13855">13855</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2014-08-07 14:30:05 -0700 (Thu, 07 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make sure the dashboard can get directory stats from the DPS server.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarserverdashboard_servicepy">CalendarServer/trunk/calendarserver/dashboard_service.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertapcaldavpy">CalendarServer/trunk/calendarserver/tap/caldav.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="#CalendarServertrunktxdavwhocachepy">CalendarServer/trunk/txdav/who/cache.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 (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/dashboard_service.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/calendarserver/dashboard_service.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -14,12 +14,19 @@
</span><span class="cx"> # limitations under the License.
</span><span class="cx"> ##
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> from twext.enterprise.jobqueue import JobItem
</span><span class="cx"> 
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, succeed, returnValue
</span><ins>+from twisted.internet.error import ConnectError
</ins><span class="cx"> from twisted.internet.protocol import Factory
</span><span class="cx"> from twisted.protocols.basic import LineReceiver
</span><span class="cx"> 
</span><ins>+from twistedcaldav.config import config
+
+from txdav.dps.client import DirectoryService as DirectoryProxyClientService
+from txdav.who.cache import CachingDirectoryService
+
</ins><span class="cx"> import json
</span><span class="cx"> 
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="lines">@@ -162,8 +169,8 @@
</span><span class="cx">         @rtype: L{str}
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         try:
</span><del>-            return succeed(self.factory.store.directoryService().stats())
-        except AttributeError:
</del><ins>+            return self.factory.directory.stats()
+        except (AttributeError, ConnectError):
</ins><span class="cx">             return succeed({})
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -176,3 +183,13 @@
</span><span class="cx">         self.logger = logObserver
</span><span class="cx">         self.limiter = limiter
</span><span class="cx">         self.store = None
</span><ins>+        self.directory = None
+
+
+    def makeDirectoryProxyClient(self):
+        self.directory = DirectoryProxyClientService(config.DirectoryRealmName)
+        if config.DirectoryProxy.InProcessCachingSeconds:
+            self.directory = CachingDirectoryService(
+                self.directory,
+                expireSeconds=config.DirectoryProxy.InProcessCachingSeconds
+            )
</ins></span></pre></div>
<a id="CalendarServertrunkcalendarservertapcaldavpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tap/caldav.py (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tap/caldav.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/calendarserver/tap/caldav.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -511,13 +511,14 @@
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx">     def __init__(
</span><del>-        self, maker, monitor, dispenser, dispatcher, configPath,
</del><ins>+        self, maker, monitor, dispenser, dispatcher, stats, configPath,
</ins><span class="cx">         inheritFDs=None, inheritSSLFDs=None
</span><span class="cx">     ):
</span><span class="cx">         self.maker = maker
</span><span class="cx">         self.monitor = monitor
</span><span class="cx">         self.dispenser = dispenser
</span><span class="cx">         self.dispatcher = dispatcher
</span><ins>+        self.stats = stats
</ins><span class="cx">         self.configPath = configPath
</span><span class="cx">         self.inheritFDs = inheritFDs
</span><span class="cx">         self.inheritSSLFDs = inheritSSLFDs
</span><span class="lines">@@ -562,6 +563,9 @@
</span><span class="cx">             )
</span><span class="cx">             self.monitor.addProcessObject(process, PARENT_ENVIRONMENT)
</span><span class="cx"> 
</span><ins>+        # Hook up the stats service directory to the DPS server after a short delay
+        if self.stats is not None:
+            self.monitor._reactor.callLater(5, self.stats.makeDirectoryProxyClient)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -1795,7 +1799,8 @@
</span><span class="cx">             )
</span><span class="cx">             statsService.setName(&quot;unix-stats&quot;)
</span><span class="cx">             statsService.setServiceParent(s)
</span><del>-        if config.Stats.EnableTCPStatsSocket:
</del><ins>+
+        elif config.Stats.EnableTCPStatsSocket:
</ins><span class="cx">             stats = DashboardServer(logger.observer, cl if config.UseMetaFD else None)
</span><span class="cx">             statsService = TCPServer(
</span><span class="cx">                 config.Stats.TCPStatsPort, stats, interface=&quot;&quot;
</span><span class="lines">@@ -1867,7 +1872,7 @@
</span><span class="cx">             multi = MultiService()
</span><span class="cx">             pool.setServiceParent(multi)
</span><span class="cx">             spawner = SlaveSpawnerService(
</span><del>-                self, monitor, dispenser, dispatcher, options[&quot;config&quot;],
</del><ins>+                self, monitor, dispenser, dispatcher, stats, options[&quot;config&quot;],
</ins><span class="cx">                 inheritFDs=inheritFDs, inheritSSLFDs=inheritSSLFDs
</span><span class="cx">             )
</span><span class="cx">             spawner.setServiceParent(multi)
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolsdashboardpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/dashboard.py (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/dashboard.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/calendarserver/tools/dashboard.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -946,7 +946,7 @@
</span><span class="cx">         nlines = len(defaultIfNone(self.readItem(&quot;directory&quot;), {}))
</span><span class="cx">         self.rowCount = nlines
</span><span class="cx">         self._createWindow(
</span><del>-            &quot;Directory Service&quot;, self.rowCount + 6, ncols=self.FORMAT_WIDTH,
</del><ins>+            &quot;Directory Service&quot;, self.rowCount + 8, ncols=self.FORMAT_WIDTH,
</ins><span class="cx">             begin_y=top, begin_x=left
</span><span class="cx">         )
</span><span class="cx">         return self
</span><span class="lines">@@ -982,10 +982,16 @@
</span><span class="cx">         y += 2
</span><span class="cx"> 
</span><span class="cx">         overallCount = 0
</span><ins>+        overallCountCached = 0
+        overallCountUncached = 0
</ins><span class="cx">         overallTimeSpent = 0.0
</span><span class="cx"> 
</span><span class="cx">         for methodName, (count, timeSpent) in sorted(records.items(), key=lambda x: x[0]):
</span><span class="cx">             overallCount += count
</span><ins>+            if methodName.endswith(&quot;-hit&quot;):
+                overallCountCached += count
+            if &quot;-&quot; not in methodName:
+                overallCountUncached += count
</ins><span class="cx">             overallTimeSpent += timeSpent
</span><span class="cx"> 
</span><span class="cx">             s = &quot; {:&lt;40}{:&gt;15d}{:&gt;15.1f}{:&gt;15.3f} &quot;.format(
</span><span class="lines">@@ -1003,19 +1009,35 @@
</span><span class="cx">                 pass
</span><span class="cx">             y += 1
</span><span class="cx"> 
</span><del>-        s = &quot; {:&lt;40}{:&gt;15d}{:&gt;15.1f}{:&gt;15.5f} &quot;.format(
</del><ins>+        s = &quot; {:&lt;40}{:&gt;15d}{:&gt;15.1f}{:&gt;15.3f} &quot;.format(
</ins><span class="cx">             &quot;Total:&quot;,
</span><span class="cx">             overallCount,
</span><span class="cx">             overallTimeSpent,
</span><span class="cx">             safeDivision(overallTimeSpent, overallCount, 1000.0)
</span><span class="cx">         )
</span><ins>+        s_cached = &quot; {:&lt;40}{:&gt;15d}{:&gt;14.1f}%{:&gt;15s} &quot;.format(
+            &quot;Total Cached:&quot;,
+            overallCountCached,
+            safeDivision(overallCountCached, overallCount, 100.0),
+            &quot;&quot;,
+        )
+        s_uncached = &quot; {:&lt;40}{:&gt;15d}{:&gt;14.1f}%{:&gt;15s} &quot;.format(
+            &quot;Total Uncached:&quot;,
+            overallCountUncached,
+            safeDivision(overallCountUncached, overallCount, 100.0),
+            &quot;&quot;,
+        )
</ins><span class="cx">         if self.usesCurses:
</span><span class="cx">             self.window.hline(y, x, &quot;-&quot;, self.FORMAT_WIDTH - 2)
</span><span class="cx">             y += 1
</span><span class="cx">             self.window.addstr(y, x, s)
</span><ins>+            self.window.addstr(y + 1, x, s_cached)
+            self.window.addstr(y + 2, x, s_uncached)
</ins><span class="cx">         else:
</span><span class="cx">             print(s)
</span><del>-        y += 1
</del><ins>+            print(s1)
+            print(s2)
+        y += 3
</ins><span class="cx"> 
</span><span class="cx">         if self.usesCurses:
</span><span class="cx">             self.window.refresh()
</span></span></pre></div>
<a id="CalendarServertrunktxdavdpsclientpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/dps/client.py (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/dps/client.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/txdav/dps/client.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> from twext.who.util import ConstantsContainer
</span><span class="cx"> from twisted.internet import reactor
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</span><ins>+from twisted.internet.error import ConnectError
</ins><span class="cx"> from twisted.internet.protocol import ClientCreator
</span><span class="cx"> from twisted.protocols import amp
</span><span class="cx"> from twisted.python.constants import Names, NamedConstant
</span><span class="lines">@@ -40,7 +41,7 @@
</span><span class="cx">     RecordsMatchingTokensCommand, RecordsMatchingFieldsCommand,
</span><span class="cx">     MembersCommand, GroupsCommand, SetMembersCommand,
</span><span class="cx">     VerifyPlaintextPasswordCommand, VerifyHTTPDigestCommand,
</span><del>-    WikiAccessForUIDCommand, ContinuationCommand
</del><ins>+    WikiAccessForUIDCommand, ContinuationCommand, StatsCommand
</ins><span class="cx"> )
</span><span class="cx"> from txdav.who.delegates import RecordType as DelegatesRecordType
</span><span class="cx"> from txdav.who.directory import (
</span><span class="lines">@@ -345,7 +346,16 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
+    def stats(self):
+        try:
+            result = yield self._sendCommand(StatsCommand)
+            returnValue(pickle.loads(result['stats']))
+        except ConnectError:
+            returnValue({})
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> @implementer(ICalendarStoreDirectoryRecord)
</span><span class="cx"> class DirectoryRecord(BaseDirectoryRecord, CalendarDirectoryRecordMixin):
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavdpscommandspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/dps/commands.py (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/dps/commands.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/txdav/dps/commands.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -199,3 +199,11 @@
</span><span class="cx">     response = [
</span><span class="cx">         ('access', amp.String()),
</span><span class="cx">     ]
</span><ins>+
+
+
+class StatsCommand(amp.Command):
+    arguments = []
+    response = [
+        ('stats', amp.String()),
+    ]
</ins></span></pre></div>
<a id="CalendarServertrunktxdavdpsserverpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/dps/server.py (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/dps/server.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/txdav/dps/server.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -37,7 +37,8 @@
</span><span class="cx">     RecordsMatchingTokensCommand, RecordsMatchingFieldsCommand,
</span><span class="cx">     MembersCommand, GroupsCommand, SetMembersCommand,
</span><span class="cx">     VerifyPlaintextPasswordCommand, VerifyHTTPDigestCommand,
</span><del>-    WikiAccessForUIDCommand, ContinuationCommand
</del><ins>+    WikiAccessForUIDCommand, ContinuationCommand,
+    StatsCommand,
</ins><span class="cx">     # UpdateRecordsCommand, RemoveRecordsCommand
</span><span class="cx"> )
</span><span class="cx"> from txdav.who.cache import CachingDirectoryService
</span><span class="lines">@@ -457,7 +458,17 @@
</span><span class="cx">         returnValue(response)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @StatsCommand.responder
+    @inlineCallbacks
+    def stats(self):
+        stats = yield self._directory.stats()
+        response = {
+            &quot;stats&quot;: pickle.dumps(stats),
+        }
+        returnValue(response)
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> class DirectoryProxyAMPFactory(Factory):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="lines">@@ -583,7 +594,7 @@
</span><span class="cx">             setproctitle(&quot;CalendarServer Directory Proxy Service&quot;)
</span><span class="cx"> 
</span><span class="cx">         try:
</span><del>-            pool, txnFactory = getDBPool(config)
</del><ins>+            _ignore_pool, txnFactory = getDBPool(config)
</ins><span class="cx">             store = storeFromConfig(config, txnFactory, None)
</span><span class="cx">             directory = directoryFromConfig(config)
</span><span class="cx">             if config.DirectoryProxy.InSidecarCachingSeconds:
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhoaugmentpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/augment.py (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/augment.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/txdav/who/augment.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -27,7 +27,7 @@
</span><span class="cx"> 
</span><span class="cx"> from zope.interface import implementer
</span><span class="cx"> 
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue
</del><ins>+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</ins><span class="cx"> from twistedcaldav.directory.augment import AugmentRecord
</span><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> from twext.who.directory import DirectoryRecord
</span><span class="lines">@@ -68,14 +68,7 @@
</span><span class="cx">         @param startTime: the start time of the call in seconds
</span><span class="cx">         @type startTime: C{float}
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        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)
</del><ins>+        AugmentedDirectoryService._addTiming(key, time.time() - startTime)
</ins><span class="cx">         return result
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -118,8 +111,18 @@
</span><span class="cx">         self._augmentDB = augmentDB
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @classmethod
+    def _addTiming(cls, key, duration):
+        if key not in cls._timings:
+            cls._timings[key] = (0, 0.0)
+        count, timeSpent = cls._timings[key]
+        count += 1
+        timeSpent += duration
+        cls._timings[key] = (count, timeSpent)
+
+
</ins><span class="cx">     def stats(self):
</span><del>-        return self._timings
</del><ins>+        return succeed(self._timings)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @property
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhocachepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/cache.py (13854 => 13855)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/cache.py        2014-08-07 21:07:22 UTC (rev 13854)
+++ CalendarServer/trunk/txdav/who/cache.py        2014-08-07 21:30:05 UTC (rev 13855)
</span><span class="lines">@@ -61,6 +61,7 @@
</span><span class="cx">     emailAddress = ValueConstant(&quot;emailAddress&quot;)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> @implementer(IDirectoryService, IStoreDirectoryService)
</span><span class="cx"> class CachingDirectoryService(
</span><span class="cx">     BaseDirectoryService, CalendarDirectoryServiceMixin
</span><span class="lines">@@ -81,10 +82,16 @@
</span><span class="cx">     def __init__(self, directory, expireSeconds=30):
</span><span class="cx">         BaseDirectoryService.__init__(self, directory.realmName)
</span><span class="cx">         self._directory = directory
</span><ins>+        self._directoryTiming = hasattr(self._directory, &quot;_addTiming&quot;)
</ins><span class="cx">         self._expireSeconds = expireSeconds
</span><span class="cx">         self.resetCache()
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def _addTiming(self, key, duration):
+        if self._directoryTiming:
+            self._directory._addTiming(key, duration)
+
+
</ins><span class="cx">     def resetCache(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Clear the cache
</span><span class="lines">@@ -156,12 +163,12 @@
</span><span class="cx">             now = time.time()
</span><span class="cx"> 
</span><span class="cx">         for indexType in self._cache:
</span><del>-            for key, (cachedTime, record) in self._cache[indexType].items():
</del><ins>+            for key, (cachedTime, _ignore_record) in self._cache[indexType].items():
</ins><span class="cx">                 if now - self._expireSeconds &gt; cachedTime:
</span><span class="cx">                     del self._cache[indexType][key]
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    def lookupRecord(self, indexType, key):
</del><ins>+    def lookupRecord(self, indexType, key, name):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Looks for a record in the specified index, under the specified key.
</span><span class="cx">         After every SCAN_AFTER_LOOKUP_COUNT lookups are done,
</span><span class="lines">@@ -200,6 +207,7 @@
</span><span class="cx">                 )
</span><span class="cx">                 # This record has expired
</span><span class="cx">                 del self._cache[indexType][key]
</span><ins>+                self._addTiming(&quot;{}-expired&quot;.format(name), 0)
</ins><span class="cx">                 return None
</span><span class="cx"> 
</span><span class="cx">             log.debug(
</span><span class="lines">@@ -208,6 +216,7 @@
</span><span class="cx">                 key=key
</span><span class="cx">             )
</span><span class="cx">             self._hitCount += 1
</span><ins>+            self._addTiming(&quot;{}-hit&quot;.format(name), 0)
</ins><span class="cx">             return record
</span><span class="cx">         else:
</span><span class="cx">             log.debug(
</span><span class="lines">@@ -215,6 +224,8 @@
</span><span class="cx">                 index=indexType.value,
</span><span class="cx">                 key=key
</span><span class="cx">             )
</span><ins>+
+        self._addTiming(&quot;{}-miss&quot;.format(name), 0)
</ins><span class="cx">         return None
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -224,7 +235,7 @@
</span><span class="cx">     def recordWithUID(self, uid):
</span><span class="cx"> 
</span><span class="cx">         # First check our cache
</span><del>-        record = self.lookupRecord(IndexType.uid, uid)
</del><ins>+        record = self.lookupRecord(IndexType.uid, uid, &quot;recordWithUID&quot;)
</ins><span class="cx">         if record is None:
</span><span class="cx">             record = yield self._directory.recordWithUID(uid)
</span><span class="cx">             if record is not None:
</span><span class="lines">@@ -241,7 +252,7 @@
</span><span class="cx">     def recordWithGUID(self, guid):
</span><span class="cx"> 
</span><span class="cx">         # First check our cache
</span><del>-        record = self.lookupRecord(IndexType.guid, guid)
</del><ins>+        record = self.lookupRecord(IndexType.guid, guid, &quot;recordWithGUID&quot;)
</ins><span class="cx">         if record is None:
</span><span class="cx">             record = yield self._directory.recordWithGUID(guid)
</span><span class="cx">             if record is not None:
</span><span class="lines">@@ -260,7 +271,8 @@
</span><span class="cx">         # First check our cache
</span><span class="cx">         record = self.lookupRecord(
</span><span class="cx">             IndexType.shortName,
</span><del>-            (recordType.name, shortName)
</del><ins>+            (recordType.name, shortName),
+            &quot;recordWithShortName&quot;
</ins><span class="cx">         )
</span><span class="cx">         if record is None:
</span><span class="cx">             record = yield self._directory.recordWithShortName(
</span><span class="lines">@@ -280,7 +292,11 @@
</span><span class="cx">     def recordsWithEmailAddress(self, emailAddress):
</span><span class="cx"> 
</span><span class="cx">         # First check our cache
</span><del>-        record = self.lookupRecord(IndexType.emailAddress, emailAddress)
</del><ins>+        record = self.lookupRecord(
+            IndexType.emailAddress,
+            emailAddress,
+            &quot;recordsWithEmailAddress&quot;
+        )
</ins><span class="cx">         if record is None:
</span><span class="cx">             records = yield self._directory.recordsWithEmailAddress(emailAddress)
</span><span class="cx">             if len(records) == 1:
</span><span class="lines">@@ -367,3 +383,7 @@
</span><span class="cx">         return CalendarDirectoryServiceMixin.recordWithCalendarUserAddress(
</span><span class="cx">             self, cua
</span><span class="cx">         )
</span><ins>+
+
+    def stats(self):
+        return self._directory.stats()
</ins></span></pre>
</div>
</div>

</body>
</html>