2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Issue a series of GetHash requests to the SafeBrowsing servers and measure
11 $ ./gethash_timer.py --period=600 --samples=20 --output=resp.csv
13 --period (or -p): The amount of time (in seconds) to wait between GetHash
14 requests. Using a value of more than 300 (5 minutes) to
15 include the effect of DNS.
17 --samples (or -s): The number of requests to issue. If this parameter is not
18 specified, the test will run indefinitely.
20 --output (or -o): The path to a file where the output will be written in
21 CSV format: sample_number,response_code,elapsed_time_ms
29 _GETHASH_HOST
= 'safebrowsing.clients.google.com'
31 '/safebrowsing/gethash?client=googleclient&appver=1.0&pver=2.1')
33 # Global logging file handle.
37 def IssueGetHash(prefix
):
38 '''Issue one GetHash request to the safebrowsing servers.
40 prefix: A 4 byte value to look up on the server.
42 The HTTP response code for the GetHash request.
44 body
= '4:4\n' + prefix
45 h
= httplib
.HTTPConnection(_GETHASH_HOST
)
46 h
.putrequest('POST', _GETHASH_REQUEST
)
47 h
.putheader('content-length', str(len(body
)))
50 response_code
= h
.getresponse().status
55 def TimedGetHash(prefix
):
56 '''Measure the amount of time it takes to receive a GetHash response.
58 prefix: A 4 byte value to look up on the the server.
60 A tuple of HTTP resonse code and the response time (in milliseconds).
63 response_code
= IssueGetHash(prefix
)
64 return response_code
, (time
.time() - start
) * 1000
67 def RunTimedGetHash(period
, samples
=None):
68 '''Runs an experiment to measure the amount of time it takes to receive
69 multiple responses from the GetHash servers.
72 period: A floating point value that indicates (in seconds) the delay
74 samples: An integer value indicating the number of requests to make.
75 If 'None', the test continues indefinitely.
80 prefix
= '\x50\x61\x75\x6c'
83 response_code
, elapsed_time
= TimedGetHash(prefix
)
84 LogResponse(sample_count
, response_code
, elapsed_time
)
86 if samples
is not None and sample_count
== samples
:
91 def LogResponse(sample_count
, response_code
, elapsed_time
):
92 '''Output the response for one GetHash query.
94 sample_count: The current sample number.
95 response_code: The HTTP response code for the GetHash request.
96 elapsed_time: The round-trip time (in milliseconds) for the
102 output_list
= (sample_count
, response_code
, elapsed_time
)
103 print 'Request: %d, status: %d, elapsed time: %f ms' % output_list
104 if g_file_handle
is not None:
105 g_file_handle
.write(('%d,%d,%f' % output_list
) + '\n')
106 g_file_handle
.flush()
109 def SetupOutputFile(file_name
):
110 '''Open a file for logging results.
112 file_name: A path to a file to store the output.
117 g_file_handle
= open(file_name
, 'w')
124 options
, args
= getopt
.getopt(sys
.argv
[1:],
126 ['samples=', 'period=', 'output='])
127 for option
, value
in options
:
128 if option
== '-s' or option
== '--samples':
130 elif option
== '-p' or option
== '--period':
131 period
= float(value
)
132 elif option
== '-o' or option
== '--output':
135 print 'Bad option: %s' % option
138 print 'Starting Timed GetHash ----------'
139 SetupOutputFile(file_name
)
140 RunTimedGetHash(period
, samples
)
141 except KeyboardInterrupt:
144 print 'Timed GetHash complete ----------'
145 g_file_handle
.close()
148 if __name__
== '__main__':