<!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>[15091] 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/15091">15091</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-09-02 13:34:57 -0700 (Wed, 02 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add a tool to look at the processing rate of TestWork items. Make it easy to use the loadtest tool with other server accounts.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarserverdashboard_servicepy">CalendarServer/trunk/calendarserver/dashboard_service.py</a></li>
<li><a href="#CalendarServertrunkcontribperformancejobqueueloadtestpy">CalendarServer/trunk/contrib/performance/jobqueue/loadtest.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcontribperformancejobqueueworkratepy">CalendarServer/trunk/contrib/performance/jobqueue/workrate.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 (15090 => 15091)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/dashboard_service.py        2015-09-02 19:54:05 UTC (rev 15090)
+++ CalendarServer/trunk/calendarserver/dashboard_service.py        2015-09-02 20:34:57 UTC (rev 15091)
</span><span class="lines">@@ -24,9 +24,12 @@
</span><span class="cx"> 
</span><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> 
</span><ins>+from txdav.common.datastore.work.load_work import TestWork
</ins><span class="cx"> from txdav.dps.client import DirectoryService as DirectoryProxyClientService
</span><span class="cx"> from txdav.who.cache import CachingDirectoryService
</span><span class="cx"> 
</span><ins>+from twext.enterprise.jobs.queue import WorkerConnectionPool
+
</ins><span class="cx"> import json
</span><span class="cx"> 
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="lines">@@ -161,6 +164,28 @@
</span><span class="cx">         return succeed({&quot;workers&quot;: loads, &quot;level&quot;: level})
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
+    def data_test_work(self):
+        &quot;&quot;&quot;
+        Return the number of TEST_WORK items in the job queue.
+
+        @return: a string containing the JSON result.
+        @rtype: L{str}
+        &quot;&quot;&quot;
+
+        results = {}
+        if self.factory.store:
+            txn = self.factory.store.newTransaction()
+            results[&quot;queued&quot;] = yield TestWork.count(txn)
+            results[&quot;completed&quot;] = WorkerConnectionPool.completed.get(TestWork.workType(), 0)
+            yield txn.commit()
+        else:
+            results[&quot;queued&quot;] = 0
+            results[&quot;completed&quot;] = 0
+
+        returnValue(results)
+
+
</ins><span class="cx">     def data_directory(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Return a summary of directory service calls.
</span></span></pre></div>
<a id="CalendarServertrunkcontribperformancejobqueueloadtestpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/contrib/performance/jobqueue/loadtest.py (15090 => 15091)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/performance/jobqueue/loadtest.py        2015-09-02 19:54:05 UTC (rev 15090)
+++ CalendarServer/trunk/contrib/performance/jobqueue/loadtest.py        2015-09-02 20:34:57 UTC (rev 15091)
</span><span class="lines">@@ -23,6 +23,7 @@
</span><span class="cx"> import random
</span><span class="cx"> import sys
</span><span class="cx"> import time
</span><ins>+from urlparse import urlparse
</ins><span class="cx"> 
</span><span class="cx"> PRIORITY = {
</span><span class="cx">     &quot;low&quot;: 0,
</span><span class="lines">@@ -35,14 +36,23 @@
</span><span class="cx">     # Random time delay
</span><span class="cx">     time.sleep(random.randint(0, config[&quot;interval&quot;]) / 1000.0)
</span><span class="cx"> 
</span><ins>+    url = urlparse(config[&quot;server&quot;])
+    use_ssl = url[0] == &quot;https&quot;
+    if &quot;@&quot; in url[1]:
+        auth, net_loc = url[1].split(&quot;@&quot;)
+    else:
+        auth = &quot;admin:admin&quot;
+        net_loc = url[1]
+    host, port = net_loc.split(&quot;:&quot;)
+    port = int(port)
+    user, pswd = auth.split(&quot;:&quot;)
+
</ins><span class="cx">     headers = {}
</span><span class="cx">     headers[&quot;User-Agent&quot;] = &quot;httploop/1&quot;
</span><span class="cx">     headers[&quot;Depth&quot;] = &quot;1&quot;
</span><del>-    headers[&quot;Authorization&quot;] = &quot;Basic &quot; + &quot;admin:admin&quot;.encode(&quot;base64&quot;)[:-1]
</del><ins>+    headers[&quot;Authorization&quot;] = &quot;Basic &quot; + &quot;{}:{}&quot;.format(user, pswd).encode(&quot;base64&quot;)[:-1]
</ins><span class="cx">     headers[&quot;Content-Type&quot;] = &quot;application/json&quot;
</span><span class="cx"> 
</span><del>-    host, port = config[&quot;server&quot;].split(&quot;:&quot;)
-    port = int(port)
</del><span class="cx">     interval = config[&quot;interval&quot;] / 1000.0
</span><span class="cx">     total = config[&quot;limit&quot;] / config[&quot;numProcesses&quot;]
</span><span class="cx"> 
</span><span class="lines">@@ -59,7 +69,7 @@
</span><span class="cx"> 
</span><span class="cx">     base_time = time.time()
</span><span class="cx">     while not complete.value:
</span><del>-        http = SmartHTTPConnection(host, port, True, False)
</del><ins>+        http = SmartHTTPConnection(host, port, use_ssl, False)
</ins><span class="cx"> 
</span><span class="cx">         try:
</span><span class="cx">             count += 1
</span><span class="lines">@@ -101,7 +111,7 @@
</span><span class="cx">     -i MSEC        Millisecond delay between each request [1000]
</span><span class="cx">     -r RATE        Requests/second rate [10]
</span><span class="cx">     -j JOBS        Number of jobs per HTTP request [1]
</span><del>-    -s HOST:PORT   Host/port to connect to [localhost:8443]
</del><ins>+    -s URL         URL to connect to [https://localhost:8443]
</ins><span class="cx">     -b SEC         Number of seconds for notBefore [0]
</span><span class="cx">     -d MSEC        Number of milliseconds for the work [10]
</span><span class="cx">     -l NUM         Total number of requests from all processes
</span><span class="lines">@@ -139,7 +149,7 @@
</span><span class="cx">         &quot;numProcesses&quot;: 10,
</span><span class="cx">         &quot;interval&quot;: 1000,
</span><span class="cx">         &quot;jobs&quot;: 1,
</span><del>-        &quot;server&quot;: &quot;localhost:8443&quot;,
</del><ins>+        &quot;server&quot;: &quot;https://localhost:8443&quot;,
</ins><span class="cx">         &quot;when&quot;: 0,
</span><span class="cx">         &quot;delay&quot;: 10,
</span><span class="cx">         &quot;priority&quot;: &quot;high&quot;,
</span></span></pre></div>
<a id="CalendarServertrunkcontribperformancejobqueueworkratepy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/contrib/performance/jobqueue/workrate.py (0 => 15091)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/contrib/performance/jobqueue/workrate.py                                (rev 0)
+++ CalendarServer/trunk/contrib/performance/jobqueue/workrate.py        2015-09-02 20:34:57 UTC (rev 15091)
</span><span class="lines">@@ -0,0 +1,224 @@
</span><ins>+#!/usr/bin/env python
+##
+# Copyright (c) 2012-2015 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 getopt import getopt, GetoptError
+import errno
+import json
+import os
+import sched
+import socket
+import sys
+import time
+
+
+def usage(e=None):
+    name = os.path.basename(sys.argv[0])
+    print(&quot;usage: %s [options]&quot; % (name,))
+    print(&quot;&quot;)
+    print(&quot;options:&quot;)
+    print(&quot;  -h --help: print this help and exit&quot;)
+    print(&quot;  -s: server host (and optional port) [localhost:8100]&quot;)
+    print(&quot;      or unix socket path prefixed by 'unix:'&quot;)
+    print(&quot;&quot;)
+    print(&quot;This tool monitors the server's job assignment rate.&quot;)
+
+    if e:
+        sys.exit(64)
+    else:
+        sys.exit(0)
+
+
+
+def main():
+    try:
+        (optargs, _ignore_args) = getopt(
+            sys.argv[1:], &quot;hs:&quot;, [
+                &quot;help&quot;,
+            ],
+        )
+    except GetoptError, e:
+        usage(e)
+
+    #
+    # Get configuration
+    #
+    server = (&quot;localhost&quot;, 8100)
+
+    for opt, arg in optargs:
+        if opt in (&quot;-h&quot;, &quot;--help&quot;):
+            usage()
+
+        elif opt in (&quot;-s&quot;):
+            if not arg.startswith(&quot;unix:&quot;):
+                server = arg.split(&quot;:&quot;)
+                if len(server) == 1:
+                    server.append(8100)
+                else:
+                    server[1] = int(server[1])
+                server = tuple(server)
+            else:
+                server = arg
+
+        else:
+            raise NotImplementedError(opt)
+
+    d = Monitor(server)
+    d.run()
+
+
+
+class Monitor(object):
+    &quot;&quot;&quot;
+    Main monitor controller. Use Python's L{sched} feature to schedule
+    updates.
+    &quot;&quot;&quot;
+
+    screen = None
+    registered_windows = {}
+    registered_order = []
+
+    def __init__(self, server):
+        self.paused = False
+        self.seconds = 1.0
+        self.sched = sched.scheduler(time.time, time.sleep)
+        self.client = MonitorClient(server)
+        self.client.addItem(&quot;test_work&quot;)
+        self.last_queued = None
+        self.last_completed = None
+        self.last_time = None
+
+
+    def run(self):
+        &quot;&quot;&quot;
+        Create the initial window and run the L{scheduler}.
+        &quot;&quot;&quot;
+        self.sched.enter(self.seconds, 0, self.updateResults, ())
+        self.sched.run()
+
+
+    def updateResults(self):
+        &quot;&quot;&quot;
+        Periodic update of the current window and check for a key press.
+        &quot;&quot;&quot;
+
+        t = time.time()
+        self.client.update()
+        if len(self.client.currentData) == 0:
+            print(&quot;Failed to read any valid data from the server - exiting&quot;)
+            sys.exit(1)
+
+        queued = self.client.currentData[&quot;test_work&quot;][&quot;queued&quot;]
+        completed = self.client.currentData[&quot;test_work&quot;][&quot;completed&quot;]
+        if self.last_queued is not None:
+            diff_queued = (self.last_queued - queued) / (t - self.last_time)
+            diff_completed = (completed - self.last_completed) / (t - self.last_time)
+        else:
+            diff_queued = 0
+            diff_completed = 0
+        self.last_queued = queued
+        self.last_completed = completed
+        self.last_time = t
+        print(&quot;{}\t{:.1f}\t{:.1f}&quot;.format(queued, diff_queued, diff_completed,))
+
+        self.sched.enter(max(self.seconds - (time.time() - t), 0), 0, self.updateResults, ())
+
+
+
+class MonitorClient(object):
+    &quot;&quot;&quot;
+    Client that connects to a server and fetches information.
+    &quot;&quot;&quot;
+
+    def __init__(self, sockname):
+        self.socket = None
+        if isinstance(sockname, str):
+            self.sockname = sockname[5:]
+            self.useTCP = False
+        else:
+            self.sockname = sockname
+            self.useTCP = True
+        self.currentData = {}
+        self.items = []
+
+
+    def readSock(self, items):
+        &quot;&quot;&quot;
+        Open a socket, send the specified request, and retrieve the response. Keep the socket open.
+        &quot;&quot;&quot;
+        try:
+            if self.socket is None:
+                self.socket = socket.socket(socket.AF_INET if self.useTCP else socket.AF_UNIX, socket.SOCK_STREAM)
+                self.socket.connect(self.sockname)
+                self.socket.setblocking(0)
+            self.socket.sendall(json.dumps(items) + &quot;\r\n&quot;)
+            data = &quot;&quot;
+            t = time.time()
+            while not data.endswith(&quot;\n&quot;):
+                try:
+                    d = self.socket.recv(1024)
+                except socket.error as se:
+                    if se.args[0] != errno.EWOULDBLOCK:
+                        raise
+                    if time.time() - t &gt; 5:
+                        raise socket.error
+                    continue
+                if d:
+                    data += d
+                else:
+                    break
+            data = json.loads(data)
+        except socket.error:
+            data = {}
+            self.socket = None
+        except ValueError:
+            data = {}
+        return data
+
+
+    def update(self):
+        &quot;&quot;&quot;
+        Update the current data from the server.
+        &quot;&quot;&quot;
+
+        # Only read each item once
+        self.currentData = self.readSock(list(set(self.items)))
+
+
+    def getOneItem(self, item):
+        &quot;&quot;&quot;
+        Update the current data from the server.
+        &quot;&quot;&quot;
+        data = self.readSock([item])
+        return data[item] if data else None
+
+
+    def addItem(self, item):
+        &quot;&quot;&quot;
+        Add a server data item to monitor.
+        &quot;&quot;&quot;
+        self.items.append(item)
+
+
+    def removeItem(self, item):
+        &quot;&quot;&quot;
+        No need to monitor this item.
+        &quot;&quot;&quot;
+        self.items.remove(item)
+
+if __name__ == &quot;__main__&quot;:
+    main()
</ins></span></pre>
</div>
</div>

</body>
</html>