1#!/usr/bin/env python 2# Copyright 2009 Google Inc. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""Tests for all functions related to chart generation.""" 17 18__author__ = 'tstromberg@google.com (Thomas Stromberg)' 19 20import unittest 21import nameserver 22import charts 23 24 25def _ExampleRunsData(): 26 odns = nameserver.NameServer('208.67.220.220', name='OpenDNS') 27 udns = nameserver.NameServer('156.154.70.1', name='UltraDNS') 28 data = [(odns, [79.0, 191.0, 84.0, 878.0, 82.0, 85.0, 882.0, 187.0, 79.0, 29 80.0, 79.0, 261.0, 79.0, 83.0, 82.0, 420.0, 822.0, 1890.0, 30 78.0, 79.0, 86.0, 89.0, 125.0, 94.0, 81.0, 79.0, 81.0, 79.0, 31 1105.0, 84.0]), 32 (udns, [9.0, 8.0, 13.0, 329.0, 9.0, 9.0, 773.0, 52.0, 9.0, 8.0, 8.0, 33 143.0, 27.0, 104.0, 8.0, 8.0, 320.0, 594.0, 8.0, 312.0, 11.0, 34 9.0, 174.0, 83.0, 8.0, 9.0, 8.0, 8.0, 496.0, 533.0])] 35 return data 36 37 38# TODO(tstromberg): Clean up long lines, cleanse IP/hostnames. 39class ChartFunctionsTest(unittest.TestCase): 40 def testDarkenHexColorCode(self): 41 self.assertEquals(charts.DarkenHexColorCode('ffffff', 0), 'ffffff') 42 self.assertEquals(charts.DarkenHexColorCode('2c2c2c', 1), '0c0c0c') 43 self.assertEquals(charts.DarkenHexColorCode('ff0000', 1), 'df0000') 44 self.assertEquals(charts.DarkenHexColorCode('ff00ff', 2), 'bf00bf') 45 46 def testGoodTicks(self): 47 self.assertEquals(charts._GoodTicks(50), 5) 48 self.assertEquals(charts._GoodTicks(9.8, tick_size=0.5, num_ticks=7), 2.0) 49 50 51class BasicChartTests(unittest.TestCase): 52 def testPerRunDurationBarGraph(self): 53 sorted_averages = [ 54 ('10.0.0.1', [5.871, 2.6599]), 55 ('192.168.1.2', [15.0867, 15.2531]), 56 ('172.168.1.2', [70.7752, 15.02163]), 57 ] 58 results = charts.PerRunDurationBarGraph(sorted_averages) 59 self.assertTrue('e%3AFBM48Y%2CCRNBM' in results) 60 expected = ( 61 'http://chart.apis.google.com/chart?chxt=y%2Cx%2Cx&chd=e%3AFBM48Y%2' 62 'CCRNBM0&chxp=2%2C31&chxr=1%2C0%2C75%7C2%2C-3.75%2C78.75&chxtc=1%2C-720' 63 '&chco=4684ee%2C00248e&chbh=a&chs=720x130&cht=bhg&chxl=0%3A%7C' 64 '172.168.1.2%7C192.168.1.2%7C10.0.0.1%7C1%3A%7C0%7C5%7C10%7C15%7C20' 65 '%7C25%7C30%7C35%7C40%7C45%7C50%7C55%7C60%7C65%7C70%7C75%7C2%3A%7C' 66 'Duration%20in%20ms.&chdl=Run%201%7CRun%202' 67 ) 68 self.assertEqual(results, expected) 69 70 def testMinimumDurationBarGraph(self): 71 fastest = ((nameserver.NameServer('208.67.220.220', name='OpenDNS'), 10.0), 72 (nameserver.NameServer('156.154.70.1', name='UltraDNS'), 15.75)) 73 74 expected = ( 75 'http://chart.apis.google.com/chart?chxt=y%2Cx%2Cx&chd=e%3AgAyZ&' 76 'chxp=2%2C9&chxr=1%2C0%2C20%7C2%2C-1.0%2C21.0&chxtc=1%2C-720' 77 '&chco=0000ff&chbh=a&chs=720x78&cht=bhg&chxl=0%3A%7CUltraDNS%7COpenDNS' 78 '%7C1%3A%7C0%7C3%7C6%7C9%7C12%7C15%7C18%7C20%7C2%3A%7C' 79 'Duration%20in%20ms.' 80 ) 81 self.assertEquals(charts.MinimumDurationBarGraph(fastest), expected) 82 83 def testMaximumRunDuration(self): 84 runs_data = [ 85 ('G', [3.851, 4.7690, 423.971998, 189.674001, 14.477, 174.788001]), 86 ('Y', [99.99, 488.88]) 87 ] 88 self.assertEquals(charts._MaximumRunDuration(runs_data), 488.88) 89 90class DistributionChartTests(unittest.TestCase): 91 92 def testMakeCumulativeDistribution(self): 93 runs_data = _ExampleRunsData() 94 expected = [ 95 (runs_data[0][0], 96 [(0, 0), (3.3333333333333335, 78.0),(26.666666666666668, 79.0), 97 (30.0, 80.0), (36.666666666666664, 81.0), (43.333333333333336, 82.0), 98 (46.666666666666664, 83.0), (53.333333333333336, 84.0), 99 (56.666666666666664, 85.0), (60.0, 86.0), (63.333333333333329, 89.0), 100 (66.666666666666657, 94.0), (70.0, 125.0), (73.333333333333329, 187.0), 101 (76.666666666666671, 191.0), (80.0, 261.0), (83.333333333333343, 420.0), 102 (86.666666666666671, 822.0), (90.0, 878.0), (93.333333333333329, 882.0), 103 (96.666666666666671, 1105.0), (100, 1890.0)]), 104 (runs_data[1][0], 105 [(0, 0), (30.0, 8.0), (50.0, 9.0), (53.333333333333336, 11.0), 106 (56.666666666666664, 13.0), (60.0, 27.0), (63.333333333333329, 52.0), 107 (66.666666666666657, 83.0), (70.0, 104.0), (73.333333333333329, 143.0), 108 (76.666666666666671, 174.0), (80.0, 312.0), (83.333333333333343, 320.0), 109 (86.666666666666671, 329.0), (90.0, 496.0), (93.333333333333329, 533.0), 110 (96.666666666666671, 594.0), (100, 773.0)])] 111 self.assertEquals(charts._MakeCumulativeDistribution(runs_data), expected) 112 113 def testDistributionLineGraph(self): 114 runs_data = _ExampleRunsData() 115 url = charts.DistributionLineGraph(runs_data, scale=350) 116 expected = ( 117 'http://chart.apis.google.com/chart?cht=lxy&chs=720x410&chxt=x,y&' 118 'chg=10,20&chxr=0,0,350|1,0,100&chd=t:0,22,23,23,23,23,24,24,24,25,25' 119 ',27,36,53,55,75,120|0,3,27,30,37,43,47,53,57,60,63,67,70,73,77,80,83|' 120 '0,2,3,3,4,8,15,24,30,41,50,89,91,94,142|0,30,50,53,57,60,63,67,70,73,' 121 '77,80,83,87,90&chco=ff9900,1a00ff&chxt=x,y,x,y&chxl=2:||Duration+in+ms' 122 '||3:||%25|&chdl=OpenDNS|UltraDNS' 123 ) 124 self.assertTrue('0,3,27,30,37,43,47,53,57,60,63,67,70,73,77' in expected) 125 self.assertTrue('0,0,350|1,0,100' in expected) 126 self.assertEquals(url, expected) 127 128 129if __name__ == '__main__': 130 unittest.main() 131