source: tools/demos/test_flood_database.py @ 3517

Last change on this file since 3517 was 3517, checked in by ole, 18 years ago

Set up for load testing of live flood database

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1""" Stress test GA flood database
2
3This script tests that the flood database is live.
4Then proceeds to crawl through the site based on a html file containing 1005 level 1 links. Detail links below are followed automatically.
5
6If the database works, this test should complete with 3715 Detail links
7responding succesfully
8
9
10Ole Nielsen, RAMP 2006
11x9048
12"""
13
14
15import urllib   
16import string
17#from caching import cache
18
19base_url = 'http://www.ga.gov.au/oracle/flood/'
20servlet_base_url = 'http://www.ga.gov.au/'
21
22def test_site():
23    """Test flood database
24    """
25 
26    top_url = 'flood_input.jsp'
27
28    url = base_url+top_url
29    live = False 
30    print 'Testing %s. ' %url, 
31    T = get_page(url)
32
33    for line in T:
34        #print line.strip()
35        if line.startswith('<title>Geoscience Australia: Online Flood Search'):
36            live = True
37            break
38
39    if live is True:   
40        print 'OK: Page is live'
41    else:
42        msg = 'Page %s is not live' %url         
43        raise msg
44
45    print
46
47
48def test_database(filename):
49    """Read list of studies from html and try them one by one
50    """
51
52    print 'Reading %s' %filename
53    fid = open(filename)
54
55    total_detail = 0
56    for i, link in enumerate(get_individual_studies(fid.readlines())):
57        url = base_url+htmlmap(link)
58     
59        live = False 
60        print 'Testing link %d: %s...' %(i, link[:72]),
61        T = get_page(url)
62
63        live = False 
64        for line in T:
65            if line.startswith('<tr><th>Dataset Name</th>') or\
66                   line.startswith('<h2>Study title only available.</h2>'):
67              live = True
68              break
69
70        if live is True:   
71            print 'OK: Link %d is live' %i
72        else:
73            msg = 'FAIL: Link %d is not live: %s' %(i,url)
74            raise msg
75
76
77        # Secon tier links
78       
79        for j, link in enumerate(get_second_tier_links(T)):
80            url = servlet_base_url+htmlmap(link)
81     
82            live = False 
83            print 'Testing detail %d: %s...' %(j, link[:80]),
84            T = get_page(url)
85
86            live = False
87            for line in T:
88                #print line.strip()
89                if line.startswith('<tr><td><h3>Participants in '):
90                  live = True
91                  break
92
93            if live is True:   
94                print 'OK: Detail link %d is live (total=%d)' %(j, total_detail)
95            else:
96                for line in T:
97                    print line.strip()               
98                msg = 'FAIL: Detail link %d is not live (total=%d)' %(j, total_detail)
99                raise msg
100           
101            total_detail += 1           
102
103
104def get_second_tier_links(lines):
105    """Scan html lines for flood details and yield
106    """   
107    for line in lines:
108     
109        index = line.find('<a href="/servlet/FloodDetailServlet?sno')
110        if index >= 0:
111            start_index = line.find('servlet', index)
112            end_index = line.find('">', start_index)
113            yield line[start_index:end_index]
114
115     
116
117def get_individual_studies(lines):
118    """Scan html lines for individual flood studies and yield links
119    """ 
120 
121    for line in lines:
122        index = line.find('<a href="%sflood_infolist.jsp?sno' %base_url)
123        if index >= 0:
124            start_index = line.find('flood_infolist', index)
125            end_index = line.find('">', start_index)
126
127            yield line[start_index:end_index]
128
129     
130def get_page(URL):
131    """General routine for getting and caching URL pages
132    """
133 
134    F = urllib.urlopen(URL)   
135    T = F.readlines()
136    return(T)
137
138
139
140def htmlmap(s):
141    import types
142    from string import replace, strip
143
144    s = replace(s,' ','%20')   
145    return(strip(s))
146         
147 
148test_site()
149test_database('flood_studies_all.html')
150
151
152       
Note: See TracBrowser for help on using the repository browser.