<!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>[15573] CalendarServer/trunk/contrib/performance/loadtest</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/15573">15573</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2016-05-05 10:12:41 -0700 (Thu, 05 May 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Simulator clients now use a single AMP connection per server rather than each client connecting to each server</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcontribperformanceloadtesticalpy">CalendarServer/trunk/contrib/performance/loadtest/ical.py</a></li>
<li><a href="#CalendarServertrunkcontribperformanceloadtestsimpy">CalendarServer/trunk/contrib/performance/loadtest/sim.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcontribperformanceloadtestamphubpy">CalendarServer/trunk/contrib/performance/loadtest/amphub.py</a></li>
<li><a href="#CalendarServertrunkcontribperformanceloadtesttest_amphubpy">CalendarServer/trunk/contrib/performance/loadtest/test_amphub.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcontribperformanceloadtestamphubpy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/contrib/performance/loadtest/amphub.py (0 => 15573)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/performance/loadtest/amphub.py                                (rev 0)
+++ CalendarServer/trunk/contrib/performance/loadtest/amphub.py        2016-05-05 17:12:41 UTC (rev 15573)
</span><span class="lines">@@ -0,0 +1,87 @@
</span><ins>+##
+# Copyright (c) 2016 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+##
+from __future__ import print_function
+
+from calendarserver.push.amppush import AMPPushClientFactory, SubscribeToID
+from twisted.internet.endpoints import TCP4ClientEndpoint
+from twisted.internet.defer import inlineCallbacks
+from twisted.internet import reactor
+from uuid import uuid4
+
+
+class AMPHub(object):
+
+    _hub = None
+
+    def __init__(self):
+        # self.hostsAndPorts = hostsAndPorts
+        self.protocols = []
+        self.callbacks = {}
+
+
+    @classmethod
+    @inlineCallbacks
+    def start(cls, hostsAndPorts):
+        &quot;&quot;&quot;
+        Instantiates the AMPHub singleton and connects to the hosts.
+
+        @param hostsAndPorts: The hosts and ports to connect to
+        @type hostsAndPorts: A list of (hostname, port) tuples
+        &quot;&quot;&quot;
+
+        cls._hub = cls()
+
+        for host, port in hostsAndPorts:
+            endpoint = TCP4ClientEndpoint(reactor, host, port)
+            factory = AMPPushClientFactory(cls._hub._pushReceived)
+            protocol = yield endpoint.connect(factory)
+            cls._hub.protocols.append(protocol)
+
+
+    @classmethod
+    @inlineCallbacks
+    def subscribeToIDs(cls, ids, callback):
+        &quot;&quot;&quot;
+        Clients can call this method to register a callback which
+        will get called whenever a push notification is fired for any
+        id in the ids list.
+
+        @param ids: The push IDs to subscribe to
+        @type ids: list of strings
+        @param callback: The method to call whenever a notification is
+            received.
+        @type callback: callable which is passed an id (string), a timestamp
+            of the change in data triggering this push (integer), and the
+            priority level (integer)
+
+        &quot;&quot;&quot;
+        hub = cls._hub
+
+        for id in ids:
+            hub.callbacks.setdefault(id, []).append(callback)
+            for protocol in hub.protocols:
+                yield protocol.callRemote(SubscribeToID, token=str(uuid4()), id=id)
+
+
+    def _pushReceived(self, id, dataChangedTimestamp, priority=5):
+        &quot;&quot;&quot;
+        Called for every incoming push notification, this method then calls
+        each callback registered for the given ID.
+        &quot;&quot;&quot;
+
+        for callback in self.callbacks.setdefault(id, []):
+            callback(id, dataChangedTimestamp, priority=priority)
</ins></span></pre></div>
<a id="CalendarServertrunkcontribperformanceloadtesticalpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/contrib/performance/loadtest/ical.py (15572 => 15573)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/performance/loadtest/ical.py        2016-05-03 16:46:28 UTC (rev 15572)
+++ CalendarServer/trunk/contrib/performance/loadtest/ical.py        2016-05-05 17:12:41 UTC (rev 15573)
</span><span class="lines">@@ -25,9 +25,9 @@
</span><span class="cx"> from caldavclientlibrary.protocol.webdav.definitions import davxml
</span><span class="cx"> from caldavclientlibrary.protocol.webdav.propfindparser import PropFindParser
</span><span class="cx"> 
</span><del>-from calendarserver.push.amppush import subscribeToIDs
</del><span class="cx"> from calendarserver.tools.notifications import PubSubClientFactory
</span><span class="cx"> 
</span><ins>+from contrib.performance.loadtest.amphub import AMPHub
</ins><span class="cx"> from contrib.performance.httpauth import AuthHandlerAgent
</span><span class="cx"> from contrib.performance.httpclient import StringProducer, readBody
</span><span class="cx"> from contrib.performance.loadtest.subscribe import Periodical
</span><span class="lines">@@ -1510,11 +1510,7 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Start monitoring for AMP-based push notifications
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        for host in self.ampPushHosts:
-            subscribeToIDs(
-                host, self.ampPushPort, pushKeys,
-                self._receivedPush, self.reactor
-            )
</del><ins>+        AMPHub.subscribeToIDs(pushKeys, self._receivedPush)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span></span></pre></div>
<a id="CalendarServertrunkcontribperformanceloadtestsimpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/contrib/performance/loadtest/sim.py (15572 => 15573)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/performance/loadtest/sim.py        2016-05-03 16:46:28 UTC (rev 15572)
+++ CalendarServer/trunk/contrib/performance/loadtest/sim.py        2016-05-05 17:12:41 UTC (rev 15573)
</span><span class="lines">@@ -59,6 +59,8 @@
</span><span class="cx">     CalendarClientSimulator)
</span><span class="cx"> from contrib.performance.loadtest.webadmin import LoadSimAdminResource
</span><span class="cx"> 
</span><ins>+from contrib.performance.loadtest.amphub import AMPHub
+
</ins><span class="cx"> class _DirectoryRecord(object):
</span><span class="cx">     def __init__(self, uid, password, commonName, email, guid, podID=&quot;PodA&quot;):
</span><span class="cx">         self.uid = uid
</span><span class="lines">@@ -487,12 +489,24 @@
</span><span class="cx">         attachService(self.reactor, self, self.ms)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def startAmpHub(self):
+        hostsAndPorts = []
+        for serverInfo in self.servers.itervalues():
+            if serverInfo[&quot;enabled&quot;]:
+                for host in serverInfo.get(&quot;ampPushHosts&quot;, []):
+                    hostsAndPorts.append((host, serverInfo[&quot;ampPushPort&quot;]))
+        if hostsAndPorts:
+            AMPHub.start(hostsAndPorts)
+
+
+
</ins><span class="cx">     def run(self, output=stdout):
</span><span class="cx">         self.attachServices(output)
</span><span class="cx">         if self.runtime is not None:
</span><span class="cx">             self.reactor.callLater(self.runtime, self.stopAndReport)
</span><span class="cx">         if self.webadminPort:
</span><span class="cx">             self.reactor.listenTCP(self.webadminPort, Site(LoadSimAdminResource(self)))
</span><ins>+        self.startAmpHub()
</ins><span class="cx">         self.reactor.run()
</span><span class="cx"> 
</span><span class="cx">         # Return code to indicate pass or fail
</span></span></pre></div>
<a id="CalendarServertrunkcontribperformanceloadtesttest_amphubpy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/contrib/performance/loadtest/test_amphub.py (0 => 15573)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/performance/loadtest/test_amphub.py                                (rev 0)
+++ CalendarServer/trunk/contrib/performance/loadtest/test_amphub.py        2016-05-05 17:12:41 UTC (rev 15573)
</span><span class="lines">@@ -0,0 +1,56 @@
</span><ins>+##
+# Copyright (c) 2011-2016 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+##
+
+from twisted.internet.defer import inlineCallbacks, succeed
+from twisted.trial.unittest import TestCase
+
+from contrib.performance.loadtest.amphub import AMPHub
+
+class StubProtocol(object):
+
+    def __init__(self, history):
+        self.history = history
+
+    def callRemote(self, *args, **kwds):
+        self.history.append((args, kwds))
+        return succeed(None)
+
+
+class AMPHubTestCase(TestCase):
+
+    def callback(self, id, dataChangedTimestamp, priority=5):
+        self.callbackHistory.append(id)
+
+    @inlineCallbacks
+    def test_amphub(self):
+        amphub = AMPHub()
+        subscribeHistory = []
+        protocol = StubProtocol(subscribeHistory)
+        amphub.protocols.append(protocol)
+        AMPHub._hub = amphub
+        keys = (&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)
+        yield AMPHub.subscribeToIDs(keys, self.callback)
+        self.assertEquals(len(subscribeHistory), 3)
+        for key in keys:
+            self.assertEquals(len(amphub.callbacks[key]), 1)
+
+        self.callbackHistory = []
+        amphub._pushReceived(&quot;a&quot;, 0)
+        amphub._pushReceived(&quot;b&quot;, 0)
+        amphub._pushReceived(&quot;a&quot;, 0)
+        amphub._pushReceived(&quot;c&quot;, 0)
+        self.assertEquals(self.callbackHistory, [&quot;a&quot;, &quot;b&quot;, &quot;a&quot;, &quot;c&quot;])
</ins></span></pre>
</div>
</div>

</body>
</html>