Skip to content

Commit

Permalink
Added the profiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brantley Harris committed May 13, 2010
1 parent 0ce5f38 commit 72aa69c
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,10 +3,11 @@
pip-log.txt
c
config.json
profiler/settings.py
logs
env
ignore
build
dist
minister.egg-info
_build
_build
103 changes: 103 additions & 0 deletions profiler/profile_minister.py
@@ -0,0 +1,103 @@
import os, eventlet, shlex, paramiko
from eventlet.green import subprocess

from libcloud.types import Provider
from libcloud.providers import get_driver
from libcloud.deployment import MultiStepDeployment, ScriptDeployment, SSHKeyDeployment
from settings import USER, KEY, DRIVER, IMAGE, SIZE, SSH_PUB

### Setup LibCloud ###
Driver = get_driver(getattr(Provider, DRIVER))
conn = Driver(USER, KEY)

image = conn.list_images()[IMAGE]
size = conn.list_sizes()[SIZE]

### Setup Tsung ###
NUM_CLIENTS = 2
TSUNG_TEMPLATE = open('tsung_template.xml').read()

### Ready Deployment Scripts ###
ssh_pub = open( os.path.expanduser(SSH_PUB) ).read()
sd = SSHKeyDeployment( ssh_pub )
server_script = ScriptDeployment( " ; ".join([
"apt-get update",
"apt-get install git-core python-setuptools build-essential gcc erlang python-dev -y",
"easy_install pip virtualenv",
"groupadd minister 2>null",
"useradd -g minister minister 2>null",
"mkdir /www 2>null",
"cd /www",
"pip install -E env -e git+git://github.com/DeadWisdom/Minister.git#egg=minister",
"chown -R minister:minister /www",
"env/bin/minister . --start"
]) )

### Support Functions ###
def shell(cmd):
args = shlex.split(str(cmd))
print "-", args
try:
popen = subprocess.Popen(list(args), cwd=os.curdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = popen.communicate()
except OSError, e:
err = str(e)
out = None

return out, err

def start_server():
print "Staring server..."
msd = MultiStepDeployment([sd, server_script])
node = conn.deploy_node(name='Server', image=image, size=size, deploy=msd)
try:
shell('rsync -qazr test_service root@%s:/www/services' % node.public_ip[0])
except Exception, e:
print "Rsync failure, destroying node..."
node.destroy()
raise e
print "Server started at", node.public_ip[0]
return node

def start_client(index):
print "Staring client..."
node = conn.deploy_node(name='Client %d' % index, image=image, size=size, deploy=sd)
print "Client started at", node.public_ip[0]
return node

def start_tsung(server, clients):
context = {
'server': '<server host="%s" port="8000" type="tcp"/>' % server.public_ip[0],
'clients': "\n".join(['<client host="%s" use_controller_vm="false" maxusers="800"/>' % c.public_ip[0] for c in clients]),
'monitor': '<monitor host="%s" type="erlang"/>' % server.public_ip[0],
'get': '<http url="http://%s/" version="1.1" method="GET"/>' % server.public_ip[0]
}
src = TSUNG_TEMPLATE % context
open("tsung.xml", "w").write(src)
try:
shell('tsung -f tsung.xml start')
finally:
shell('tsung -f tsung.xml stop')

if __name__ == '__main__':
server = start_server()
clients = []
pool = eventlet.GreenPool(200)

try:
clients = [node for node in pool.imap(start_client, range(1, NUM_CLIENTS + 1))]

start_tsung(server, clients)

print "Finished."
except Exception, e:
print "Exception raised:", e

"Destroying server..."
server.destroy()
print "Server dead."
print "Destroying clients..."
for c in clients:
c.destroy()
print "Clients dead."
print "Goodbye."
4 changes: 4 additions & 0 deletions profiler/requirements.pip
@@ -0,0 +1,4 @@
eventlet
paramiko
zope.interface
apache-libcloud
6 changes: 6 additions & 0 deletions profiler/settings.py.example
@@ -0,0 +1,6 @@
USER = '' # User
KEY = '' # Key
DRIVER = "RACKSPACE" # Driver
IMAGE = 5 # Image index
SIZE = 0 # Size index
SSH_PUB = "~/.ssh/id_rsa.pub" # File with ssh public key
10 changes: 10 additions & 0 deletions profiler/stop_servers.py
@@ -0,0 +1,10 @@
from libcloud.types import Provider
from libcloud.providers import get_driver
from settings import DRIVER, USER, KEY

Driver = get_driver(getattr(Provider, DRIVER))
conn = Driver(USER, KEY)

for node in reversed( conn.list_nodes() ):
print "Stopping", node.public_ip[0]
node.destroy()
9 changes: 9 additions & 0 deletions profiler/test_service/pong.py
@@ -0,0 +1,9 @@
def application(environ, start_response):
status = '200 OK'
output = 'Pong!'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]

start_response(status, response_headers)
return [output]
8 changes: 8 additions & 0 deletions profiler/test_service/service.json
@@ -0,0 +1,8 @@
{
"name": "pong.py",
"url": "",
"site": "*",

"type": "wsgi",
"app": "pong.application"
}
31 changes: 31 additions & 0 deletions profiler/tsung.xml
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd" []>
<tsung loglevel="warning">
<clients>
<client host="173.203.99.242" use_controller_vm="false" maxusers="800"/>
<client host="173.203.96.242" use_controller_vm="false" maxusers="800"/>
</clients>
<servers>
<server host="173.203.125.244" port="8000" type="tcp"/>
</servers>
<monitoring>
<monitor host="173.203.125.244" type="erlang"/>
</monitoring>

<load>
<arrivalphase phase="1" duration="5" unit="minute">
<users interarrival="0.002" unit="second"/>
</arrivalphase>
</load>

<sessions>
<session name='wsgitest' probability='100' type='ts_http'>
<for from="0" to="12" incr="1" var="counter">
<request>
<http url="http://173.203.125.244/" version="1.1" method="GET"/>
</request>
<thinktime random='false' value='5'/>
</for>
</session>
</sessions>
</tsung>
30 changes: 30 additions & 0 deletions profiler/tsung_template.xml
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd" []>
<tsung loglevel="warning">
<clients>
%(clients)s
</clients>
<servers>
%(server)s
</servers>
<monitoring>
%(monitor)s
</monitoring>

<load>
<arrivalphase phase="1" duration="5" unit="minute">
<users interarrival="0.002" unit="second"/>
</arrivalphase>
</load>

<sessions>
<session name='wsgitest' probability='100' type='ts_http'>
<for from="0" to="12" incr="1" var="counter">
<request>
%(get)s
</request>
<thinktime random='false' value='5'/>
</for>
</session>
</sessions>
</tsung>

0 comments on commit 72aa69c

Please sign in to comment.