<!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>[15678] CalendarServer/trunk/calendarserver/tools/dashtime.py</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/15678">15678</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2016-06-16 13:34:40 -0700 (Thu, 16 Jun 2016)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add tool to do time-based plots of various dashboard stats.</pre>
<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolsdashtimepy">CalendarServer/trunk/calendarserver/tools/dashtime.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolsdashtimepy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/calendarserver/tools/dashtime.py (0 => 15678)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/dashtime.py         (rev 0)
+++ CalendarServer/trunk/calendarserver/tools/dashtime.py        2016-06-16 20:34:40 UTC (rev 15678)
</span><span class="lines">@@ -0,0 +1,551 @@
</span><ins>+#!/usr/bin/env python
+##
+# Copyright (c) 2015-2016 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# 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 "AS IS" 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.
+##
+"""
+Tool that extracts time series data from a dashcollect log.
+"""
+
+from bz2 import BZ2File
+from collections import OrderedDict, defaultdict
+import argparse
+import json
+import matplotlib.pyplot as plt
+import operator
+import os
+
+
+verbose = False
+def _verbose(log):
+ if verbose:
+ print(log)
+
+
+
+def safeDivision(value, total, factor=1):
+ return value * factor / total if total else 0
+
+
+
+class DataType(object):
+ """
+ Base class for object that can process the different types of data in a
+ dashcollect log.
+ """
+
+ allTypes = OrderedDict()
+ key = ""
+
+ # This indicates whether the class of data is based on a 1 minute average -
+ # which means the data represents a 60 second delay compared to the "real-
+ # time" value. If it is the average then setting this flag will cause the
+ # first 60 data items to be skipped.
+ skip60 = False
+
+
+ @staticmethod
+ def getTitle(measurement):
+ if "-" in measurement:
+ measurement, item = measurement.split("-", 1)
+ else:
+ item = ""
+ return DataType.allTypes[measurement].title(item)
+
+
+ @staticmethod
+ def skip(measurement):
+ if "-" in measurement:
+ measurement = measurement.split("-", 1)[0]
+ return DataType.allTypes[measurement].skip60
+
+
+ @staticmethod
+ def process(measurement, stats, host):
+ if "-" in measurement:
+ measurement, item = measurement.split("-", 1)
+ else:
+ item = ""
+ return DataType.allTypes[measurement].calculate(stats, item, host)
+
+
+ @staticmethod
+ def title(item):
+ raise NotImplementedError
+
+
+ @staticmethod
+ def maxY(stats, item):
+ raise NotImplementedError
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ """
+ If hosts is L{None} then data from all hosts will be aggregated.
+
+ @param stats: per-Pod L{dict} of data from each host in the pod.
+ @type stats: L{dict}
+ @param item: additional L{dict} key for data of interest
+ @type item: L{str}
+ @param hosts: list of hosts to process
+ @type hosts: L{list}
+ """
+ raise NotImplementedError
+
+
+
+class CPUDataType(DataType):
+ """
+ CPU use.
+ """
+
+ key = "cpu"
+
+ @staticmethod
+ def title(item):
+ return "CPU Use %"
+
+
+ @staticmethod
+ def maxY(stats, item, host):
+ return 100 * len(stats) if host is None else 100
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ if hosts is None:
+ hosts = stats.keys()
+ return sum([stats[onehost]["stats_system"]["cpu use"] for onehost in hosts])
+
+
+
+class RequestsDataType(DataType):
+ """
+ Number of requests.
+ """
+
+ key = "reqs"
+ skip60 = True
+
+ @staticmethod
+ def title(item):
+ return "Requests/sec"
+
+
+ @staticmethod
+ def maxY(stats, item, host):
+ return None
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ if hosts is None:
+ hosts = stats.keys()
+ return sum([stats[onehost]["stats"]["1m"]["requests"] for onehost in hosts]) / 60.0
+
+
+
+class ResponseDataType(DataType):
+ """
+ Average response time.
+ """
+
+ key = "respt"
+ skip60 = True
+
+ @staticmethod
+ def title(item):
+ return "Av. Response Time (ms)"
+
+
+ @staticmethod
+ def maxY(stats, item, host):
+ return None
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ if hosts is None:
+ hosts = stats.keys()
+ tsum = sum([stats[onehost]["stats"]["1m"]["t"] for onehost in hosts])
+ rsum = sum([stats[onehost]["stats"]["1m"]["requests"] for onehost in hosts])
+ return safeDivision(tsum, rsum)
+
+
+
+class JobsCompletedDataType(DataType):
+ """
+ Job completion count from job assignments.
+ """
+
+ key = "jcomp"
+
+ lastCompleted = defaultdict(int)
+
+ @staticmethod
+ def title(item):
+ return "Completed"
+
+
+ @staticmethod
+ def maxY(stats, item, host):
+ return None
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ if hosts is None:
+ hosts = stats.keys()
+ result = 0
+ for onehost in hosts:
+ completed = sum(map(operator.itemgetter(2), stats[onehost]["job_assignments"]["workers"]))
+ result += completed - JobsCompletedDataType.lastCompleted[onehost] if JobsCompletedDataType.lastCompleted[onehost] else 0
+ JobsCompletedDataType.lastCompleted[onehost] = completed
+ return result
+
+
+
+class MethodCountDataType(DataType):
+ """
+ Count of specified methods. L{item} should be set to the full name of the
+ "decorated" method seen in dashview.
+ """
+
+ key = "methodc"
+ skip60 = True
+
+ @staticmethod
+ def title(item):
+ return item
+
+
+ @staticmethod
+ def maxY(stats, item, host):
+ return None
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ if hosts is None:
+ hosts = stats.keys()
+ return sum([stats[onehost]["stats"]["1m"]["method"].get(item, 0) for onehost in hosts])
+
+
+
+class MethodResponseDataType(DataType):
+ """
+ Average response time of specified methods. L{item} should be set to the
+ full name of the "decorated" method seen in dashview.
+ """
+
+ key = "methodr"
+ skip60 = True
+
+ @staticmethod
+ def title(item):
+ return item
+
+
+ @staticmethod
+ def maxY(stats, item, host):
+ return None
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ if hosts is None:
+ hosts = stats.keys()
+ tsum = sum([stats[onehost]["stats"]["1m"]["method-t"].get(item, 0) for onehost in hosts])
+ rsum = sum([stats[onehost]["stats"]["1m"]["method"].get(item, 0) for onehost in hosts])
+ return safeDivision(tsum, rsum)
+
+
+
+class JobQueueDataType(DataType):
+ """
+ Count of queued job items. L{item} should be set to the full name or prefix
+ of job types to process. Or if set to L{None}, all jobs are counted.
+ """
+
+ key = "jqueue"
+
+ @staticmethod
+ def title(item):
+ return ("JQ " + "_".join(map(operator.itemgetter(0), item.split("_")))) if item else "Jobs Queued"
+
+
+ @staticmethod
+ def maxY(stats, item, host):
+ return None
+
+
+ @staticmethod
+ def calculate(stats, item, hosts):
+ onehost = sorted(stats.keys())[0]
+
+ if item:
+ return sum(map(operator.itemgetter("queued"), {k: v for k, v in stats[onehost]["jobs"].items() if k.startswith(item)}.values()))
+ else:
+ return sum(map(operator.itemgetter("queued"), stats[onehost]["jobs"].values()))
+
+
+# Register the known L{DataType}s
+for dtype in DataType.__subclasses__():
+ DataType.allTypes[dtype.key] = dtype
+
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Dashboard time series processor.",
+ epilog="cpu - CPU use\nreqs - requests per second\nrespt - average response time",
+ )
+ parser.add_argument("-l", help="Log file to process")
+ parser.add_argument("-p", help="Name of pod to analyze")
+ parser.add_argument("-s", help="Name of server to analyze")
+ parser.add_argument("-v", action="store_true", help="Verbose")
+ args = parser.parse_args()
+ if args.v:
+ global verbose
+ verbose = True
+
+ # Get the log file
+ try:
+ if args.l.endswith(".bz2"):
+ logfile = BZ2File(os.path.expanduser(args.l))
+ else:
+ logfile = open(os.path.expanduser(args.l))
+ except:
+ print("Failed to open logfile {}".format(args.l))
+
+ # Start/end lines in log file to process
+ line_start = 0
+ line_count = 10000
+
+ # Plot arrays that will be generated
+ x = []
+ y = OrderedDict()
+ titles = {}
+ ymaxes = {}
+
+ def singleHost(valuekeys):
+ """
+ Generate data for a single host only.
+
+ @param valuekeys: L{DataType} keys to process
+ @type valuekeys: L{list} or L{str}
+ """
+ _plotHosts(valuekeys, (args.s,))
+
+
+ def combinedHosts(valuekeys):
+ """
+ Generate data for all hosts.
+
+ @param valuekeys: L{DataType} keys to process
+ @type valuekeys: L{list} or L{str}
+ """
+ _plotHosts(valuekeys, None)
+
+
+ def _plotHosts(valuekeys, hosts):
+ """
+ Generate data for a the specified list of hosts.
+
+ @param valuekeys: L{DataType} keys to process
+ @type valuekeys: L{list} or L{str}
+ @param hosts: lists of hosts to process
+ @type hosts: L{list} or L{str}
+ """
+
+ # Initialize the plot arrays
+ for measurement in valuekeys:
+ y[measurement] = []
+ titles[measurement] = DataType.getTitle(measurement)
+ ymaxes[measurement] = None
+
+ # For each log file line, process the data for each required measurement
+ with logfile:
+ line = logfile.readline()
+ ctr = 0
+ while line:
+ if ctr < line_start:
+ ctr += 1
+ line = logfile.readline()
+ continue
+
+ if line[0] == "\x1e":
+ line = line[1:]
+ jline = json.loads(line)
+
+ x.append(ctr)
+ ctr += 1
+
+ for measurement in valuekeys:
+ stats = jline["pods"][args.p]
+ y[measurement].append(DataType.process(measurement, stats, hosts))
+
+ line = logfile.readline()
+ if ctr > line_start + line_count:
+ break
+
+ # Offset data that is averaged over the previous minute
+ for measurement in valuekeys:
+ if DataType.skip(measurement):
+ y[measurement] = y[measurement][60:]
+ y[measurement].extend([None] * 60)
+
+
+ def perHost(perhostkeys, combinedkeys):
+ """
+ Generate a set of per-host plots, together we a set of plots for all-
+ host data.
+
+ @param perhostkeys: L{DataType} keys for per-host data to process
+ @type perhostkeys: L{list} or L{str}
+ @param combinedkeys: L{DataType} keys for all-host data to process
+ @type combinedkeys: L{list} or L{str}
+ """
+
+ # For each log file line, process the data for each required measurement
+ with logfile:
+ line = logfile.readline()
+ ctr = 0
+ while line:
+ if ctr < line_start:
+ ctr += 1
+ line = logfile.readline()
+ continue
+
+ if line[0] == "\x1e":
+ line = line[1:]
+ jline = json.loads(line)
+
+ x.append(ctr)
+ ctr += 1
+
+ # Initialize the plot arrays when we know how many hosts there are
+ if len(y) == 0:
+ hosts = sorted(jline["pods"][args.p].keys())
+
+ for host in hosts:
+ for measurement in perhostkeys:
+ ykey = "{}={}".format(measurement, host)
+ y[ykey] = []
+ titles[ykey] = DataType.getTitle(measurement)
+ ymaxes[ykey] = None
+
+ for measurement in combinedkeys:
+ y[measurement] = []
+ titles[measurement] = DataType.getTitle(measurement)
+ ymaxes[measurement] = None
+
+ # Get actual measurement data
+ for host in hosts:
+ for measurement in perhostkeys:
+ ykey = "{}={}".format(measurement, host)
+ stats = jline["pods"][args.p]
+ y[ykey].append(DataType.process(measurement, stats, (host,)))
+
+ for measurement in combinedkeys:
+ stats = jline["pods"][args.p]
+ y[measurement].append(DataType.process(measurement, stats, hosts))
+
+ line = logfile.readline()
+ if ctr > line_start + line_count:
+ break
+
+ # Offset data that is averaged over the previous minute
+ for host in hosts:
+ for measurement in perhostkeys:
+ if DataType.skip(measurement):
+ ykey = "{}={}".format(measurement, host)
+ y[ykey] = y[ykey][60:]
+ y[ykey].extend([None] * 60)
+ for measurement in combinedkeys:
+ if DataType.skip(measurement):
+ y[measurement] = y[measurement][60:]
+ y[measurement].extend([None] * 60)
+
+
+# singleHost((
+# CPUDataType.key,
+# RequestsDataType.key,
+# ResponseDataType.key,
+# JobsCompletedDataType.key,
+# JobQueueDataType.key + "-SCHEDULE",
+# JobQueueDataType.key + "-PUSH",
+# JobQueueDataType.key,
+# ))
+# combinedHosts((
+# CPUDataType.key,
+# RequestsDataType.key,
+# ResponseDataType.key,
+# JobsCompletedDataType.key,
+# JobQueueDataType.key + "-SCHEDULE",
+# JobQueueDataType.key + "-PUSH",
+# JobQueueDataType.key,
+# ))
+ combinedHosts((
+ CPUDataType.key,
+ RequestsDataType.key,
+ ResponseDataType.key,
+ MethodCountDataType.key + "-PUT ics",
+ MethodCountDataType.key + "-REPORT cal-home-sync",
+ MethodCountDataType.key + "-PROPFIND Calendar Home",
+ MethodCountDataType.key + "-REPORT cal-sync",
+ MethodCountDataType.key + "-PROPFIND Calendar",
+ ))
+# perHost((
+# RequestsDataType.key,
+# ), (
+# CPUDataType.key,
+# JobQueueDataType.key,
+# ))
+
+ # Generate a single stacked plot of the data
+ for plotnum, measurement in enumerate(y.keys()):
+ plt.subplot(len(y), 1, plotnum + 1)
+ plotSeries(titles[measurement], x, y[measurement], 0, ymaxes[measurement])
+ plt.show()
+
+
+
+def plotSeries(title, x, y, ymin=None, ymax=None):
+ """
+ Plot the chosen dataset key for each scanned data file.
+
+ @param key: data set key to use
+ @type key: L{str}
+ @param ymin: minimum value for y-axis or L{None} for default
+ @type ymin: L{int} or L{float}
+ @param ymax: maximum value for y-axis or L{None} for default
+ @type ymax: L{int} or L{float}
+ """
+
+ plt.plot(x, y)
+
+ plt.xlabel("Time")
+ plt.ylabel(title)
+ if ymin is not None:
+ plt.ylim(ymin=ymin)
+ if ymax is not None:
+ plt.ylim(ymax=ymax)
+ plt.minorticks_on()
+ plt.grid(True, "major", "x", alpha=0.5, linewidth=0.5)
+ plt.grid(True, "minor", "x", alpha=0.5, linewidth=0.5)
+
+if __name__ == "__main__":
+ main()
</ins><span class="cx">Property changes on: CalendarServer/trunk/calendarserver/tools/dashtime.py
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4>Added: svn:executable</h4></div>
</div>
</body>
</html>