Python to measure HTTP and HTTPS performances: best way ???

Thomas Guettler guettli at thomas-guettler.de
Mon Nov 15 10:42:15 EST 2004


Am Fri, 12 Nov 2004 02:40:13 -0800 schrieb vincent delft:

> I want to write a script that will monitore the performance of a web
> application delivering HTTP and HTTPS pages.
> 
> I would like to know the best way to do it...

Hi, 

I did the same some days ago. Here is my code:
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-


# Python Imports
import os
import sys
import time
import base64
import signal
import urllib2

MAX_PROCESSES=40

URL="https://yourserver/....?var=foo..."

# HTTP Basic Auth
USER="..."
PASSWORD="..."

def usage():
    print """Usage: %s
StressTest ...
    """ % (os.path.basename(sys.argv[0]))

def mache_stress():
    dauer_list=[]
    try:
        while 1:
            request=urllib2.Request(URL)
            base64string=base64.encodestring('%s:%s' % (
                USER, PASSWORD))
            request.add_header("Authorization", "Basic %s" % base64string)
            start=time.time()
            fd=urllib2.urlopen(request)
            fd.read()
            dauer=time.time()-start
            print "%s OK %05.2f" % (os.getpid(),
                                    dauer)
            dauer_list.append(dauer)
    except KeyboardInterrupt:
        if dauer_list:
            all=0
            for d in dauer_list:
                all+=d
            print "Ende %s %s Requests %05.2f avg." % (
                os.getpid(), len(dauer_list), all/len(dauer_list))
        raise

def main():
    if len(sys.argv)!=1:
        usage()
        sys.exit(1)

    pids=[]
    try:
        print "Mit CTRL-C beenden."
        print "URL:", URL
        print "Anzahl der Prozesse:", MAX_PROCESSES
        for i in range(MAX_PROCESSES):
            pid=os.fork()
            if not pid:
                # Hier ist das Kind
                mache_stress()
            else:
                pids.append(pid)
        os.waitpid(0, 0)
    except KeyboardInterrupt:
        #for pid in pids:
        #    os.kill(pid, signal.SIGINT)
        pass
        
if __name__=="__main__":
    main()




More information about the Python-list mailing list