namebench-1.3.1+dfsg.orig/ 0000700 0000764 0001750 00000000000 11402745252 014351 5 ustar jwilk users namebench-1.3.1+dfsg.orig/COPYING 0000600 0000764 0001750 00000026136 11402745233 015415 0 ustar jwilk users Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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. namebench-1.3.1+dfsg.orig/templates/ 0000700 0000764 0001750 00000000000 11402745253 016350 5 ustar jwilk users namebench-1.3.1+dfsg.orig/templates/style.css 0000600 0000764 0001750 00000007526 11402745233 020234 0 ustar jwilk users body { font-family: Helvetica, Arial, sans-serif; background-color: #bbb; background-image: -moz-linear-gradient(0% 100% 90deg, #e5e5e5, #666, #e5e5e5 99%); background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#e5e5e5), to(#bbb), color-stop(.1,#666)); } td { font-size: x-small; } #container { margin-top: 4px; margin-bottom: 4px; margin-left: auto; margin-right: auto; width: 985px; background-color: #f5f5f5; padding: 0.5em; padding-top: 1px; border: 2px solid #666; border-radius: .6em; -moz-border-radius: .6em; -webkit-border-radius: .6em; } h1 { color: #00CC33; font-size: 75px; text-shadow: 3px 3px #eee; padding-top: 4px; margin: 0; } h2 { padding-top: 1em; clear: both; font-size: 1em; } h3 { font-size: 0.8em; padding-top: 0; margin: 0; } h4 { margin: 0; padding: 0; } h5.faster { font-size: 30px; font-style: italic; color: #009911; margin: 0; padding-top: 4px; } #nsdetails { display: block; background-color: #C2CFF1; border-radius: .5em; -moz-border-radius: .5em; -webkit-border-radius: .5em; padding-left: 3px; padding-bottom: 3px; padding-right: 3px; margin-left: 0.25em; } table#nstable { width: 100%; border-collapse: collapse; font-size: small; } #nstable tr { vertical-align: top; } #nstable td { font-size: small; } #nstable td.name_cell { font-size: x-small; } #nstable td.hostname_cell { font-size: x-small; } .ip { margin-left: 1em; font-weight: bold; font-family: monospace; border: 1px solid #999; background-color: #f5f5f5; width: 10em; padding: 1px; color: #000; } ul.warnings { padding: 0; margin: 0; margin-left: 0.em; list-style-position: inside; } .warnings li { } #nstable td { padding: 2px; border-right: 1px solid #C2CFF1; } .even { background-color: #fff } .odd { background-color: #ebeff9; } .unhealthy_host { background-color: #FFD6D8; border-bottom: 1px solid #990000; border-top: 1px solid #990000; } .primary_row { font-weight: bold; background-color: #FFFFEE; border-top: 1px solid #EEEE00; border-bottom: 1px solid #EEEE00; } .disabled { background-color: #333; border-bottom: 1px solid #666; color: #fff; } .disabled A { color: #fff; } .disabled A:visited { color: #e5e5e5; } .greybox { border: 1px solid #999; border-radius: .5em; -moz-border-radius: .5em; -webkit-border-radius: .5em; padding: 1em; height: 11em; background-color: #fff; } #big_summary { display: inline-table; float: left; text-align: center; width: 24em; } #recommended_config { float: right; display: inline-table; } #recommended_config table { margin-top: 1em; } #recommended_config td { font-size: medium; } .rec_name { margin-left: 8px; display: inline; color: #999; } .section { margin-left: 0.25em; padding: 1em; background-color: #fff; max-width: 910px; } .config { background-color: #fff; } .configtable { font-size: 10px; } .configtable tr { border: 9px solid #f00; } #nstable thead td { font-weight: bold; font-size: x-small; } h2:before { color: #555; content: '\25BA'; padding-right: 0.25em; } input[type=text] { color: #000; } .normal .error_count { background-color: #900; color: #fff; } .version { text-align: right; font-size: x-small; color: #ccc; clear: both; } /* links with tooltips */ a.info { position:relative; z-index:24; color: #000; text-decoration:none } .disabled a.info { color: #fff; } a.info:hover {z-index:25; } a.info span {display: none} a.info:hover span { display:block; position:absolute; top:2em; left:2em; width:15em; border:1px solid #0cf; background-color:#cff; color:#000; text-align: center } #notice { font-size: small; text-align: center; background-color: #ffa; border: 1px solid #cc7; } namebench-1.3.1+dfsg.orig/templates/resolv.conf.tmpl 0000600 0000764 0001750 00000000171 11402745233 021503 0 ustar jwilk users {% for ns in recommended %}{{ "nameserver %-15.15s # %s %s\n"|format(ns.ip, ns.name, ns.warnings_string) }}{% endfor %} namebench-1.3.1+dfsg.orig/templates/ascii.tmpl 0000600 0000764 0001750 00000002473 11402745233 020344 0 ustar jwilk users Fastest individual response (in milliseconds): ---------------------------------------------- {% for item in lowest_latency %}{{ "%-16.16s %s %2.5f\n"|format(item[0], item[1], item[2]) }}{% endfor %} Mean response (in milliseconds): -------------------------------- {% for item in mean_duration %}{{ "%-16.16s %s %2.2f\n"|format(item[0], item[1], item[2]) }}{% endfor %} Response Distribution Chart URL (200ms): ---------------------------------------- {{ distribution_url_200 }} Response Distribution Chart URL (Full): --------------------------------------- {{ distribution_url }} Recommended configuration (fastest + nearest): ---------------------------------------------- {% for ns in recommended %}{{ "nameserver %-15.15s # %s %s\n"|format(ns.ip, ns.name, ns.warnings_string) }}{% endfor %} {% if compare_title %} ******************************************************************************** In this test, {% if best_ns.ip == system_primary %}Your current primary DNS server{% else %}{{ best_ns.name }}{% endif %} is {{ compare_title }}: {{compare_subtitle }} {% if compare_reference.ns %}than {% if compare_reference.ip == system_primary %}your current primary DNS server{% else %}{{ compare_reference.name }}{% endif %}{% endif %} ******************************************************************************** {% endif %} namebench-1.3.1+dfsg.orig/templates/html.tmpl 0000600 0000764 0001750 00000010651 11402745233 020215 0 ustar jwilk users
{% if loop.index == 1 %}Primary{% elif loop.index == 2 %}Secondary{% else %}Tertiary{% endif %} Server | {{ ns.ip }} | {% if ns.name != ns.ip %}{{ ns.name }}{% endif %} |
IP | Descr. | Hostname | Avg (ms) | Diff | Min | Max | TO | NX | Notes |
{{ row.ip }}{% if row.version %}{{ row.version }}{% endif %} | {{ row.name }} | {{ row.hostname }}{% if row.node_ids %}{% for node in row.node_ids %}{{ node }} {% endfor %}{% endif %} | {% if row.overall_average %}{{ "%0.2f"|format(row.overall_average) }}{% else %}{{ "%0.2f"|format(row.check_average) }}{% endif %} | {% if row.diff %}{{ "%0.1f"|format(row.diff) }}%{% endif %} | {% if row.duration_min %}{{ "%0.1f"|format(row.duration_min) }}{% endif %} | {% if row.duration_max %}{{ "%0.1f"|format(row.duration_max) }}{% endif %} | {{ row.timeout_count }} | {{ row.nx_count }} |
{% if row.notes %}
|
Name | Value |
{{ item.0 }} | {{ item.1 }} |
([\w\.-]+\.[\w]{2,})\<\/p>.*?tc1.*?(\d+\.\d+)%', content, re.M | re.S)
def GetHostsForDomain(domain):
content = FetchCachedAlexaPage(domain)
return ParseAlexaSubdomains(content)
if __name__ == '__main__':
index = 0
results = {}
for domain in sys.stdin:
index += 1
domain = domain.rstrip()
for host, percentage in GetHostsForDomain(domain):
if host == domain and domain not in NAKED_DOMAINS:
host = '.'.join(('www', domain))
if percentage == '0.0':
continue
score = index / (float(percentage) / 100)
if host not in results:
results[host] = score
print >> sys.stderr, "%s: %s (%s)" % (score, host, percentage)
for host, score in sorted(results.items(), key=operator.itemgetter(1)):
print "A %s." % host
namebench-1.3.1+dfsg.orig/tools/release_win.bat 0000600 0000764 0001750 00000001234 11402745214 020476 0 ustar jwilk users rem Tool to assemble Windows builds
rem Requirements are 7-zip, py2exe, and FreeExtractor
PATH=C:\python27;C:\python26;C:\progra~1\7-zip;C:\progra~2\7-zip;%PATH%
rem ****** Clean out the old junk
del /s /f /q dist
rem ****** Compile our executable and core zipfile
python setup.py py2exe
rem ****** Remove extras from core zipfile
cd dist
7z d namebench.zip tcl\*.*
rmdir /s /q tcl\tcl8.5\tzdata tcl\tk8.5\demos
del tcl\tk8.5\images\*.eps
rem ****** Final assembly of zipfile
copy ..\README.txt .
7z a namebench_for_Windows.zip -r * >nul
rem ****** Test assembled zipfile
namebench -x -O 8.8.8.8 -q5 -o test.html
start test.html
cd ..
namebench-1.3.1+dfsg.orig/tools/check_nameserver_popularity.py 0000700 0000764 0001750 00000003764 11402745214 023672 0 ustar jwilk users #!/usr/bin/env python
import os
import sys
import pickle
import time
import traceback
import yahoo.search
from yahoo.search.web import WebSearch
APP_ID = 'P5ihFKzV34G69QolFfb3nN7p0rSsYfC9tPGq.IUS.NLWEeJ14SG9Lei0rwFtgwL8cDBrA6Egdw--'
QUERY_MODIFIERS = '-site:txdns.net -site:sitedossier.com -mx -site:dataopedia.com -site:l0t3k.net -syslog -"4.2.2.1" -site:cqcounter.com -site:flow.nttu.edu.tw -site:websiteoutlook.com -site:ipgeolocator.com -site:tdyndns.org -site:ebrara.com -site:onsamehost.com -site:ipaddresscentral.com -site:quia.jp -inetnum -site:domaintools.com -site:domainbyip.com -site:pdos.csail.mit.edu -statistics -"country name" -"Q_RTT" -site:botsvsbrowsers.com -"ptr record" -site:ip-db.com -site:chaip.com.cn -site:lookup365.com -"IP Country" -site:iptoolboxes.com -"Unknown Country" -"Q_RTT" -amerika -whois -Mozilla -site:domaincrawler.com -site:geek-tools.org -site:visualware.com -site:robtex.com -site:domaintool.se -site:opendns.se -site:ungefiltert-surfen.de -site:datakitteh.org -"SLOVAKIA (SK)" -"IP Search" -site:www.medicore.com.ua -site:dig.similarbase.com -site:ipcorporationwiki.com -site:coolwhois.com'
CACHE_DIR = os.getenv('HOME') + '/.ycache'
def CheckPopularity(ip):
cache_path = os.path.join(CACHE_DIR, ip) + '.pickle'
if os.path.exists(cache_path):
f = open(cache_path)
return pickle.load(f)
else:
try:
query = '"%s" %s' % (ip, QUERY_MODIFIERS)
srch = WebSearch(APP_ID, query=query, results=50)
results = srch.parse_results()
pf = open(cache_path + '.pickle', 'w')
pickle.dump(results.results, pf)
pf.close()
return results
except yahoo.search.SearchError:
print "%s failed" % (ip)
return []
if __name__ == "__main__":
for ip in sys.argv[1:]:
print '%s = %s' % (ip, total)
for result in results.results:
try:
print ' - %s: %s' % (result['Url'], result['Title'])
except UnicodeEncodeError:
print ' - %s' % result['Url']# print results.results
time.sleep(0.5)
namebench-1.3.1+dfsg.orig/tools/update-alexa-data.sh 0000700 0000764 0001750 00000002526 11402745215 021335 0 ustar jwilk users #!/bin/sh
# Copyright 2009 Google 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.
#
#
#
# This script is provided to update the data/top-10000.txt file.
CSV='top-1m.csv'
# TODO(tstromberg): Replace this bad hack. In real-world observations, hosts
# only use 1-2 Google TLD's. Clean all but the real one to match reality.
REMOVE='google\.[a-z][a-z]$|google\.co\.|google\.com\.[a-z][a-z]'
ALEXA_URL=http://s3.amazonaws.com/alexa-static/$CSV.zip
TOP_COUNT=10000
UNIQ_COUNT=14000
OUTPUT=alexa-top-${TOP_COUNT}-global.txt
if [ ! -f "$CSV" ]; then
if [ ! -f "${CSV}.zip" ]; then
echo "${CSV}.zip not found - Fetching $ALEXA_URL"
curl -O $ALEXA_URL
fi
unzip -o $CSV.zip $CSV
fi
rm $OUTPUT
cut -d, -f2 $CSV | cut -d/ -f1 | head -$UNIQ_COUNT | ./ordered-uniq.py | \
egrep -v $REMOVE | head -$TOP_COUNT > $OUTPUT
ls -la $OUTPUT
namebench-1.3.1+dfsg.orig/tools/release_src.sh 0000700 0000764 0001750 00000002364 11402745214 020342 0 ustar jwilk users #!/bin/sh
# Create a tarball from the subversion repository.
#
# Copyright 2009 Google 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.
tmp=$$
cd /tmp
svn checkout http://namebench.googlecode.com/svn/trunk/ namebench-$$
version=`grep '^VERSION' namebench-$$/libnamebench/version.py | cut -d\' -f2`
mv namebench-$$ namebench-$version
cd namebench-$version
svn log > ChangeLog.txt &
./namebench.py -q5 -j50 -x -o /tmp/$$.csv -O 8.8.8.8
echo "Waiting for changelog"
wait
find . -name "*.pyc" -delete
find . -name ".svn" -exec rm -Rf {} \; 2>/dev/null
cd ..
GZIP="-9" tar -zcvf namebench-${version}-source.tgz namebench-${version}/
if [ -d "$HOME/Desktop" ]; then
mv namebench-${version}-source.tgz $HOME/Desktop/
fi
rm -Rf namebench-${version}
namebench-1.3.1+dfsg.orig/tools/release_mac.sh 0000700 0000764 0001750 00000003000 11402745215 020300 0 ustar jwilk users #!/bin/sh
# Copyright 2009 Google 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.
PKG_DIR="$HOME/Desktop"
if [ ! -d "cocoa" ]; then
echo "cocoa/ directory not find in pwd"
exit 1
fi
rm -Rf $PKG_DIR/namebench.app
find cocoa/build/Release -name "*.pyc" -delete
find cocoa/build/Release -name "*~" -delete
find cocoa/build/Release -name "*." -delete
cp -Rp cocoa/build/Release/namebench.app $PKG_DIR/
# No longer required now that our Xcode project was setup properly.
#tmp="/tmp/namebench-$$"
#svn checkout http://namebench.googlecode.com/svn/trunk/ $tmp
#rsync -va --exclude ".svn/" --exclude "*~" --exclude "*.pyc" $tmp/ $PKG_DIR/namebench.app/Contents/Resources/
version=`grep "^VERSION" libnamebench/version.py | cut -d\' -f2`
dmg="$PKG_DIR/namebench-${version}-Mac_OS_X.dmg"
rm bundle.dmg
if [ -f "$dmg" ]; then
rm -f "$dmg"
fi
hdiutil create -srcfolder $PKG_DIR/namebench.app bundle.dmg
rm -Rf $PKG_DIR/namebench.app
hdiutil convert -imagekey zlib-level=9 -format UDZO -o $dmg bundle.dmg
open $dmg
namebench-1.3.1+dfsg.orig/libnamebench/ 0000700 0000764 0001750 00000000000 11402745253 016761 5 ustar jwilk users namebench-1.3.1+dfsg.orig/libnamebench/nameserver_list.py 0000600 0000764 0001750 00000063376 11402745220 022550 0 ustar jwilk users # Copyright 2009 Google 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.
"""Classes to work with bunches of nameservers."""
__author__ = 'tstromberg@google.com (Thomas Stromberg)'
import datetime
import operator
import os
import pickle
import Queue
import random
import subprocess
import sys
import tempfile
import threading
import time
# 3rd party libraries
import dns.resolver
import conn_quality
import nameserver
import addr_util
import util
NS_CACHE_SLACK = 2
CACHE_VER = 4
PREFERRED_HEALTH_TIMEOUT_MULTIPLIER = 1.5
SYSTEM_HEALTH_TIMEOUT_MULTIPLIER = 2
TOO_DISTANT_MULTIPLIER = 4.75
MAX_NEARBY_SERVERS = 400
# If we can't ping more than this, go into slowmode.
MIN_PINGABLE_PERCENT = 20
MIN_HEALTHY_PERCENT = 10
SLOW_MODE_THREAD_COUNT = 6
# Windows behaves in unfortunate ways if too many threads are specified
if sys.platform == 'win32':
MAX_SANE_THREAD_COUNT = 32
else:
MAX_SANE_THREAD_COUNT = 100
# Slow down for these, as they are used for timing.
MAX_INITIAL_HEALTH_THREAD_COUNT = 35
def InternalNameServers():
"""Return list of DNS server IP's used by the host via dnspython"""
try:
servers = dns.resolver.Resolver().nameservers
except:
print "Unable to get list of internal DNS servers."
servers = []
# dnspython does not always get things right on Windows, particularly in
# versions with right-to-left languages. Fall back to ipconfig /all
if not servers and sys.platform[:3] == 'win':
return WinIpConfigNameServers()
return servers
def WinIpConfigNameServers():
"""Return a list of DNS servers via ipconfig (Windows only)"""
servers = []
output = subprocess.Popen(['ipconfig', '/all'], stdout=subprocess.PIPE).stdout.read()
for line in output.split('\r\n'):
if 'DNS Servers' in line:
print "ipconfig: %s" % line
servers.extend(addr_util.ExtractIPsFromString(line))
return servers
class OutgoingUdpInterception(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class TooFewNameservers(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ThreadFailure(Exception):
def __init__(self):
pass
class QueryThreads(threading.Thread):
"""Quickly see which nameservers are awake."""
def __init__(self, input_queue, results_queue, action_type, checks=None):
threading.Thread.__init__(self)
self.input = input_queue
self.action_type = action_type
self.results = results_queue
self.checks = checks
self.halt = False
def stop(self):
self.halt = True
def run(self):
"""Iterate over the queue, processing each item."""
while not self.halt and not self.input.empty():
# check_wildcards is special: it has a tuple of two nameservers
if self.action_type == 'wildcard_check':
try:
(ns, other_ns) = self.input.get_nowait()
except Queue.Empty:
return
if ns.disabled or other_ns.disabled:
self.results.put(None)
continue
else:
self.results.put((ns, ns.TestSharedCache(other_ns)))
# everything else only has a single nameserver.
else:
try:
ns = self.input.get_nowait()
except Queue.Empty:
return
if ns.disabled:
self.results.put(None)
continue
if self.action_type == 'ping':
self.results.put(ns.CheckHealth(fast_check=True))
elif self.action_type == 'health':
self.results.put(ns.CheckHealth(sanity_checks=self.checks))
elif self.action_type == 'final':
self.results.put(ns.CheckHealth(sanity_checks=self.checks, final_check=True))
elif self.action_type == 'port_behavior':
self.results.put(ns.CheckHealth(sanity_checks=self.checks, port_check=True))
elif self.action_type == 'censorship':
self.results.put(ns.CheckCensorship(self.checks))
elif self.action_type == 'store_wildcards':
self.results.put(ns.StoreWildcardCache())
else:
raise ValueError('Invalid action type: %s' % self.action_type)
class NameServers(list):
def __init__(self, nameservers, global_servers=None, regional_servers=None,
num_servers=1,
include_internal=False, threads=5, status_callback=None,
timeout=5, health_timeout=5, ping_timeout=1,
skip_cache_collusion_checks=False,
ipv6_only=False):
self.seen_ips = set()
self.seen_names = set()
self.timeout = timeout
self.ping_timeout = ping_timeout
self.num_servers = num_servers
self.requested_health_timeout = health_timeout
self.skip_cache_collusion_checks = skip_cache_collusion_checks
self.health_timeout = health_timeout
self.min_healthy_percent = MIN_HEALTHY_PERCENT
self.status_callback = status_callback
self.cache_dir = tempfile.gettempdir()
self.ipv6_only = ipv6_only
if threads > MAX_SANE_THREAD_COUNT:
self.msg('Lowing thread count from %s to sane limit of %s' %
(threads, MAX_SANE_THREAD_COUNT))
self.thread_count = MAX_SANE_THREAD_COUNT
else:
self.thread_count = threads
self.ApplyCongestionFactor()
super(NameServers, self).__init__()
self.system_nameservers = InternalNameServers()
if nameservers:
for (ip, name) in nameservers:
if (not name or name == ip) and ip in self.system_nameservers:
name = 'SYS-%s' % ip
self.AddServer(ip, name, is_custom=True, is_preferred=True)
if global_servers:
for (ip, name) in global_servers:
self.AddServer(ip, name, is_global=True, is_preferred=True)
if regional_servers:
for (ip, name) in regional_servers:
self.AddServer(ip, name)
if include_internal:
for ip in self.system_nameservers:
self.AddServer(ip, 'SYS-%s' % ip, is_preferred=True)
@property
def preferred(self):
return [x for x in self if x.is_preferred]
@property
def enabled_preferred(self):
return [x for x in self.preferred if not x.disabled]
@property
def secondaries(self):
return [x for x in self.SortByNearest() if not x.is_preferred]
@property
def enabled_secondaries(self):
return [x for x in self.secondaries if not x.disabled]
@property
def enabled(self):
return [x for x in self if not x.disabled]
@property
def check_average(self):
return util.CalculateListAverage([x.check_average for x in self if not x.disabled])
def msg(self, msg, count=None, total=None, **kwargs):
if self.status_callback:
self.status_callback(msg, count=count, total=total, **kwargs)
else:
print '%s [%s/%s]' % (msg, count, total)
def AddServer(self, ip, name, is_preferred=False, is_global=False, is_regional=False,
is_custom=False):
"""Add a server to the list given an IP and name."""
ns = nameserver.NameServer(ip, name=name, preferred=is_preferred)
if self.ipv6_only and not ns.is_ipv6:
return
ns.is_custom = is_custom
if ip in self.system_nameservers:
ns.is_system = True
ns.is_preferred = True
ns.is_custom = False
ns.system_position = self.system_nameservers.index(ip)
ns.is_global = is_global
ns.is_regional = is_regional
ns.timeout = self.timeout
ns.ping_timeout = self.ping_timeout
# Give them a little extra love for the road.
if ns.is_system:
ns.health_timeout = self.health_timeout * SYSTEM_HEALTH_TIMEOUT_MULTIPLIER
ns.ping_timeout = self.ping_timeout * SYSTEM_HEALTH_TIMEOUT_MULTIPLIER
elif ns.is_preferred:
ns.health_timeout = self.health_timeout * PREFERRED_HEALTH_TIMEOUT_MULTIPLIER
ns.ping_timeout = self.ping_timeout * PREFERRED_HEALTH_TIMEOUT_MULTIPLIER
else:
ns.health_timeout = self.health_timeout
self.append(ns)
def append(self, ns):
"""Add a nameserver to the list, guaranteeing uniqueness."""
if ns.ip in self.seen_ips:
# Perhaps we already know of the IP, but do not have a proper name for it
if ns.name != ns.ip:
for existing_ns in self:
if existing_ns.ip == ns.ip and existing_ns.name == existing_ns.ip:
existing_ns.name = ns.name
return None
# Add an identifier to the name if necessary.
if ns.name in self.seen_names:
for identifier in range(2, 10):
new_name = ''.join((ns.name, '-', str(identifier)))
if new_name not in self.seen_names:
ns.name = new_name
break
# print "Adding: %s [%s]" % (ns.name, ns.ip)
super(NameServers, self).append(ns)
self.seen_ips.add(ns.ip)
self.seen_names.add(ns.name)
def ApplyCongestionFactor(self):
# If we are only benchmarking one server, don't bother w/ congestion checking.
if len(self) == 1:
return
cq = conn_quality.ConnectionQuality(status_callback=self.status_callback)
(intercepted, unused_level, multiplier) = cq.CheckConnectionQuality()[0:3]
if intercepted:
raise OutgoingUdpInterception(
'Your router or Internet Service Provider appears to be intercepting '
'and redirecting all outgoing DNS requests. This means you cannot '
'benchmark or utilize alternate DNS servers. Please adjust your '
'router configuration or file a support request with your ISP.'
)
if multiplier > 1:
# TODO(tstromberg): Consider retiring this feature for good.
# self.timeout *= multiplier
self.health_timeout *= multiplier
self.ping_timeout *= multiplier
self.msg('Applied %.2fX timeout multiplier due to congestion: %2.1f ping, %2.1f health.'
% (multiplier, self.ping_timeout, self.health_timeout))
def InvokeSecondaryCache(self):
"""Delete secondary ips that do not exist in the cache file."""
cached = False
if self.cache_dir:
cpath = self._SecondaryCachePath()
cache_data = self._LoadSecondaryCache(cpath)
if cache_data:
for ns in self:
ns.warnings = set()
ns.checks = []
cached = True
cached_ips = [x.ip for x in cache_data if not x.is_preferred]
for ns in list(self.secondaries):
if ns.ip not in cached_ips:
self.remove(ns)
return cached
def RemoveBrokenServers(self, delete_unwanted=False):
"""Quietly remove broken servers."""
for ns in list(self):
if ns.disabled and delete_unwanted and (ns.is_ipv6 or not ns.is_preferred):
self.remove(ns)
def DisableDistantServers(self, multiplier=TOO_DISTANT_MULTIPLIER, max_servers=MAX_NEARBY_SERVERS):
"""Disable servers who's fastest duration is multiplier * average of best 10 servers."""
self.RemoveBrokenServers(delete_unwanted=True)
secondaries = self.secondaries
fastest = [x for x in self.SortByFastest() if not x.disabled ][:10]
best_10 = util.CalculateListAverage([x.fastest_check_duration for x in fastest])
cutoff = best_10 * multiplier
self.msg("Removing secondary nameservers slower than %0.2fms (max=%s)" % (cutoff, max_servers))
for idx, ns in enumerate(secondaries):
if (ns.fastest_check_duration > cutoff) or idx > max_servers:
self.remove(ns)
def DisableUnwantedServers(self, target_count, delete_unwanted=False):
"""Given a target count, delete nameservers that we do not plan to test."""
self.RemoveBrokenServers(delete_unwanted)
# Magic secondary mixing algorithm:
# - Half of them should be the "nearest" nameservers
# - Half of them should be the "fastest average" nameservers
preferred_count = len(self.enabled_preferred)
secondaries_needed = target_count - preferred_count
if secondaries_needed < 1 or not self.secondaries:
return
nearest_needed = int(secondaries_needed / 2.0)
if secondaries_needed < 50:
self.msg("Picking %s secondary servers to use (%s nearest, %s fastest)" %
(secondaries_needed, nearest_needed, secondaries_needed - nearest_needed))
# Phase two is picking the nearest secondary server
secondaries_to_keep = []
for ns in self.SortByNearest():
if not ns.is_preferred and not ns.disabled:
if not secondaries_to_keep and secondaries_needed < 15:
self.msg('%s appears to be the nearest regional (%0.2fms)' % (ns, ns.fastest_check_duration))
secondaries_to_keep.append(ns)
if len(secondaries_to_keep) >= nearest_needed:
break
# Phase three is removing all of the slower secondary servers
for ns in self.SortByFastest():
if not ns.is_preferred and not ns.disabled and ns not in secondaries_to_keep:
secondaries_to_keep.append(ns)
if len(secondaries_to_keep) >= secondaries_needed:
break
for ns in self.secondaries:
if ns not in secondaries_to_keep:
# print "REMOVE: Fastest: %0.2f Avg: %0.2f: %s - %s" % (ns.fastest_check_duration, ns.check_average, ns, ns.checks)
self.remove(ns)
# else:
# print "KEEP : Fastest: %0.2f Avg: %0.2f: %s - %s" % (ns.fastest_check_duration, ns.check_average, ns, ns.checks)
def CheckHealth(self, primary_checks, secondary_checks, cache_dir=None, censor_tests=None):
"""Filter out unhealthy or slow replica servers."""
if len(self) == 1:
return None
if cache_dir:
self.cache_dir = cache_dir
cpath = self._SecondaryCachePath()
try:
cached = self.InvokeSecondaryCache()
except:
self.msg('Failed to use secondary cache in [%s]: %s' % (cpath, util.GetLastExceptionString()))
cached = False
if not cached:
self.msg('Building initial DNS cache for %s nameservers (%s threads)' %
(len(self), self.thread_count))
self.PingNameServers()
if len(self.enabled) > int(self.num_servers * NS_CACHE_SLACK):
self.DisableDistantServers()
self.RunHealthCheckThreads(primary_checks)
if len(self.enabled) > self.num_servers:
self._DemoteSecondaryGlobalNameServers()
self.DisableUnwantedServers(target_count=int(self.num_servers * NS_CACHE_SLACK), delete_unwanted=True)
if not cached:
try:
self._UpdateSecondaryCache(cpath)
except:
self.msg('Failed to save secondary cache in [%s]: %s' % (cpath, util.GetLastExceptionString()))
if not self.skip_cache_collusion_checks:
self.CheckCacheCollusion()
self.DisableUnwantedServers(self.num_servers)
self.RunFinalHealthCheckThreads(secondary_checks)
if censor_tests:
self.RunCensorshipCheckThreads(censor_tests)
else:
# If we aren't doing censorship checks, quiet any possible false positives.
self._RemoveGlobalWarnings()
if not self.enabled:
raise TooFewNameservers('None of the nameservers tested are healthy')
def _RemoveGlobalWarnings(self):
"""If all nameservers have the same warning, remove it. It's likely false."""
ns_count = len(self.enabled)
seen_counts = {}
# No sense in checking for duplicate warnings if we only have one server.
if len(self.enabled) == 1:
return
for ns in self.enabled:
for warning in ns.warnings:
seen_counts[warning] = seen_counts.get(warning, 0) + 1
for warning in seen_counts:
if seen_counts[warning] == ns_count:
self.msg('All nameservers have warning: %s (likely a false positive)' % warning)
for ns in self.enabled:
ns.warnings.remove(warning)
def _DemoteSecondaryGlobalNameServers(self):
"""For global nameservers, demote the slower IP to secondary status."""
seen = {}
for ns in self.SortByFastest():
if ns.is_preferred:
# TODO(tstromberg): Have a better way of denoting secondary anycast.
provider = ns.name.replace('-2', '')
if provider in seen and not ns.is_system:
faster_ns = seen[provider]
self.msg('Making %s the primary anycast - faster than %s by %2.2fms' %
(faster_ns.name_and_node, ns.name_and_node, ns.check_average - faster_ns.check_average))
ns.is_preferred = False
else:
seen[provider] = ns
def _SecondaryCachePath(self):
"""Find a usable and unique location to store health results."""
secondary_ips = [x.ip for x in self.secondaries]
checksum = hash(str(sorted(secondary_ips)))
basefile = '.'.join(map(str, ('namebench', CACHE_VER, len(secondary_ips),
'_'.join(self.system_nameservers),
self.num_servers,
self.requested_health_timeout, checksum)))
return os.path.join(self.cache_dir, basefile)
def InvalidateSecondaryCache(self):
cpath = self._SecondaryCachePath()
if os.path.exists(cpath):
self.msg('Removing %s' % cpath)
os.unlink(cpath)
def _LoadSecondaryCache(self, cpath):
"""Check if our health cache has any good data."""
if os.path.exists(cpath) and os.path.isfile(cpath):
# self.msg('Loading local server health cache: %s' % cpath)
cf = open(cpath, 'r')
try:
return pickle.load(cf)
except EOFError:
self.msg('No cached nameserver data found')
return False
def _UpdateSecondaryCache(self, cpath):
"""Update the cache with our object."""
cf = open(cpath, 'w')
try:
pickle.dump(list(self), cf)
except TypeError, exc:
self.msg('Could not save cache: %s' % exc)
def SortByFastest(self):
"""Return a list of healthy servers in fastest-first order."""
return sorted(self, key=operator.attrgetter('check_average'))
def SortByNearest(self):
"""Return a list of healthy servers in fastest-first order."""
return sorted(self, key=operator.attrgetter('fastest_check_duration'))
def ResetTestResults(self):
"""Reset the testng status of all disabled hosts."""
return [ns.ResetTestStatus() for ns in self]
def CheckCacheCollusion(self):
"""Mark if any nameservers share cache, especially if they are slower."""
self.RunWildcardStoreThreads()
sleepy_time = 4
self.msg("Waiting %ss for TTL's to decrement." % sleepy_time)
time.sleep(sleepy_time)
test_combos = []
good_nameservers = [x for x in self.SortByFastest() if not x.disabled]
for ns in good_nameservers:
for compare_ns in good_nameservers:
if ns != compare_ns:
test_combos.append((compare_ns, ns))
results = self.RunCacheCollusionThreads(test_combos)
while not results.empty():
(ns, shared_ns) = results.get()
if shared_ns:
ns.shared_with.add(shared_ns)
shared_ns.shared_with.add(ns)
if ns.disabled or shared_ns.disabled:
continue
if ns.check_average > shared_ns.check_average:
slower = ns
faster = shared_ns
else:
slower = shared_ns
faster = ns
if slower.system_position == 0:
faster.disabled = 'Shares-cache with current primary DNS server'
slower.warnings.add('Replica of %s' % faster.ip)
elif slower.is_preferred and not faster.is_preferred:
faster.disabled = 'Replica of %s [%s]' % (slower.name, slower.ip)
slower.warnings.add('Replica of %s [%s]' % (faster.name, faster.ip))
else:
diff = slower.check_average - faster.check_average
self.msg("Disabling %s - slower replica of %s by %0.1fms." % (slower.name_and_node, faster.name_and_node, diff))
slower.disabled = 'Slower replica of %s [%s]' % (faster.name, faster.ip)
faster.warnings.add('Replica of %s [%s]' % (slower.name, slower.ip))
def _LaunchQueryThreads(self, action_type, status_message, items,
thread_count=None, **kwargs):
"""Launch query threads for a given action type.
Args:
action_type: a string describing an action type to pass
status_message: Status to show during updates.
items: A list of items to pass to the queue
thread_count: How many threads to use (int)
kwargs: Arguments to pass to QueryThreads()
Returns:
results_queue: Results from the query tests.
Raises:
TooFewNameservers: If no tested nameservers are healthy.
"""
threads = []
input_queue = Queue.Queue()
results_queue = Queue.Queue()
# items are usually nameservers
random.shuffle(items)
for item in items:
input_queue.put(item)
if not thread_count:
thread_count = self.thread_count
if thread_count > len(items):
thread_count = len(items)
status_message += ' (%s threads)' % thread_count
self.msg(status_message, count=0, total=len(items))
for _ in range(0, thread_count):
thread = QueryThreads(input_queue, results_queue, action_type, **kwargs)
try:
thread.start()
except:
self.msg("ThreadingError with %s threads: waiting for completion before retrying." % thread_count)
for thread in threads:
thread.stop()
thread.join()
raise ThreadFailure()
threads.append(thread)
while results_queue.qsize() != len(items):
self.msg(status_message, count=results_queue.qsize(), total=len(items))
time.sleep(0.5)
self.msg(status_message, count=results_queue.qsize(), total=len(items))
for thread in threads:
thread.join()
if not self.enabled:
raise TooFewNameservers('None of the %s nameservers tested are healthy' % len(self))
return results_queue
def RunCacheCollusionThreads(self, test_combos):
"""Schedule and manage threading for cache collusion checks."""
return self._LaunchQueryThreads('wildcard_check', 'Running cache-sharing checks on %s servers' % len(self.enabled), test_combos)
def PingNameServers(self):
"""Quickly ping nameservers to see which are available."""
start = datetime.datetime.now()
try:
results = self._LaunchQueryThreads('ping', 'Checking nameserver availability', list(self.enabled))
except ThreadFailure:
self.msg("It looks like you couldn't handle %s threads, trying again with %s (slow)" % (self.thread_count, SLOW_MODE_THREAD_COUNT))
self.thread_count = SLOW_MODE_THREAD_COUNT
self.ResetTestResults()
results = self._LaunchQueryThreads('ping', 'Checking nameserver availability', list(self.enabled))
success_rate = (float(len(self.enabled)) / float(len(self))) * 100
if success_rate < MIN_PINGABLE_PERCENT:
self.msg('How odd! Only %0.1f percent of name servers were pingable. Trying again with %s threads (slow)'
% (success_rate, SLOW_MODE_THREAD_COUNT))
self.ResetTestResults()
self.thread_count = SLOW_MODE_THREAD_COUNT
results = self._LaunchQueryThreads('ping', 'Checking nameserver availability', list(self))
if self.enabled:
success_rate = (float(len(self.enabled)) / float(len(self))) * 100
self.msg('%s of %s servers are available (duration: %s)' %
(len(self.enabled), len(self), datetime.datetime.now() - start))
return results
def RunHealthCheckThreads(self, checks):
"""Quickly ping nameservers to see which are healthy."""
status_msg = 'Running initial health checks on %s servers' % len(self.enabled)
if self.thread_count > MAX_INITIAL_HEALTH_THREAD_COUNT:
thread_count = MAX_INITIAL_HEALTH_THREAD_COUNT
else:
thread_count = self.thread_count
try:
results = self._LaunchQueryThreads('health', status_msg, list(self.enabled),
checks=checks, thread_count=thread_count)
except ThreadFailure:
self.msg("It looks like you couldn't handle %s threads, trying again with %s (slow)" % (thread_count, SLOW_MODE_THREAD_COUNT))
self.thread_count = SLOW_MODE_THREAD_COUNT
self.ResetTestResults()
results = self._LaunchQueryThreads('ping', 'Checking nameserver availability', list(self))
success_rate = (float(len(self.enabled)) / float(len(self))) * 100
if success_rate < self.min_healthy_percent:
self.msg('How odd! Only %0.1f percent of name servers are healthy. Trying again with %s threads (slow)'
% (success_rate, SLOW_MODE_THREAD_COUNT))
self.ResetTestResults()
self.thread_count = SLOW_MODE_THREAD_COUNT
time.sleep(5)
results = self._LaunchQueryThreads('health', status_msg, list(self.enabled),
checks=checks, thread_count=thread_count)
if self.enabled:
success_rate = (float(len(self.enabled)) / float(len(self))) * 100
self.msg('%s of %s tested name servers are healthy' % (len(self.enabled), len(self)))
return results
def RunFinalHealthCheckThreads(self, checks):
"""Quickly ping nameservers to see which are healthy."""
status_msg = 'Running final health checks on %s servers' % len(self.enabled)
return self._LaunchQueryThreads('final', status_msg, list(self.enabled), checks=checks)
def RunCensorshipCheckThreads(self, checks):
"""Quickly ping nameservers to see which are healthy."""
status_msg = 'Running censorship checks on %s servers' % len(self.enabled)
return self._LaunchQueryThreads('censorship', status_msg, list(self.enabled), checks=checks)
def RunPortBehaviorThreads(self):
"""Get port behavior data."""
status_msg = 'Running port behavior checks on %s servers' % len(self.enabled)
return self._LaunchQueryThreads('port_behavior', status_msg, list(self.enabled))
def RunWildcardStoreThreads(self):
"""Store a wildcard cache value for all nameservers (using threads)."""
status_msg = 'Waiting for wildcard cache queries from %s servers' % len(self.enabled)
return self._LaunchQueryThreads('store_wildcards', status_msg, list(self.enabled))
namebench-1.3.1+dfsg.orig/libnamebench/util.py 0000600 0000764 0001750 00000007707 11402745220 020317 0 ustar jwilk users # Copyright 2009 Google 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.
"""Little utility functions."""
__author__ = 'tstromberg@google.com (Thomas Stromberg)'
import datetime
import math
import os.path
import sys
import tempfile
def CalculateListAverage(values):
"""Computes the arithmetic mean of a list of numbers."""
if not values:
return 0
return sum(values) / float(len(values))
def DrawTextBar(value, max_value, max_width=53):
"""Return a simple ASCII bar graph, making sure it fits within max_width.
Args:
value: integer or float representing the value of this bar.
max_value: integer or float representing the largest bar.
max_width: How many characters this graph can use (int)
Returns:
string
"""
hash_width = max_value / max_width
return int(math.ceil(value/hash_width)) * '#'
def SecondsToMilliseconds(seconds):
return seconds * 1000
def SplitSequence(seq, size):
"""Split a list.
Args:
seq: sequence
size: int
Returns:
New list.
Recipe From http://code.activestate.com/recipes/425397/ (Modified to not return blank values)
"""
newseq = []
splitsize = 1.0/size*len(seq)
for i in range(size):
newseq.append(seq[int(round(i*splitsize)):int(round((i+1)*splitsize))])
return [x for x in newseq if x]
def FindDataFile(filename):
"""Find a datafile, searching various relative and OS paths."""
filename = os.path.expanduser(filename)
if os.path.exists(filename):
return filename
# If it's not a relative path, we can't do anything useful.
if os.path.isabs(filename):
return filename
other_places = [os.getcwd(),
os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'Contents', 'Resources'),
os.path.join(os.getcwd(), 'namebench.app', 'Contents', 'Resources'),
os.path.join(os.getcwd(), '..'),
os.path.join(sys.prefix, 'namebench'),
'/usr/local/share/namebench'
'/usr/local/etc/namebench',
'/usr/local/namebench',
'/etc/namebench',
'/usr/share/namebench',
'/usr/namebench']
for directory in reversed(sys.path):
other_places.append(directory)
other_places.append(os.path.join(directory, 'namebench'))
for place in other_places:
path = os.path.join(place, filename)
if os.path.exists(path):
return path
print 'I could not find "%s". Tried:' % filename
for path in other_places:
print ' %s' % path
return filename
def GenerateOutputFilename(extension):
"""Generate a decent default output filename for a given extensio."""
# used for resolv.conf
if '.' in extension:
filename = extension
else:
output_base = 'namebench_%s' % datetime.datetime.strftime(datetime.datetime.now(),
'%Y-%m-%d %H%M')
output_base = output_base.replace(':', '').replace(' ', '_')
filename = '.'.join((output_base, extension))
output_dir = tempfile.gettempdir()
return os.path.join(output_dir, filename)
def GetLastExceptionString():
"""Get the last exception and return a good looking string for it."""
(exc, error) = sys.exc_info()[0:2]
exc_msg = str(exc)
if '