Index: trunk/web/templates/main.py
===================================================================
--- trunk/web/templates/main.py	(revision 228)
+++ trunk/web/templates/main.py	(revision 235)
@@ -36,4 +36,5 @@
 sys.path.append('/home/ecprice/.local/lib/python2.5/site-packages')
 
+import templates
 from Cheetah.Template import Template
 from sipb_xen_database import Machine, CDROM, ctx, connect
@@ -41,4 +42,20 @@
 from webcommon import InvalidInput, CodeError, g
 import controls
+
+class Checkpoint:
+    def __init__(self):
+        self.start_time = time.time()
+        self.checkpoints = []
+
+    def checkpoint(self, s):
+        self.checkpoints.append((s, time.time()))
+
+    def __str__(self):
+        return ('Timing info:\n%s\n' %
+                '\n'.join(['%s: %s' % (d, t - self.start_time) for
+                           (d, t) in self.checkpoints]))
+
+checkpoint = Checkpoint()
+
 
 def helppopup(subj):
@@ -99,5 +116,5 @@
     d = dict(op=op, user=user, errorMessage=str(err),
              stderr=emsg)
-    return Template(file='error.tmpl', searchList=[d])
+    return templates.error(searchList=[d])
 
 def invalidInput(op, user, fields, err, emsg):
@@ -106,5 +123,5 @@
              err_value=str(err.err_value), stderr=emsg,
              errorMessage=str(err))
-    return Template(file='invalid.tmpl', searchList=[d])
+    return templates.invalid(searchList=[d])
 
 def hasVnc(status):
@@ -164,5 +181,5 @@
     else:
         d['new_machine'] = parsed_fields['name']
-    return Template(file='list.tmpl', searchList=[d])
+    return templates.list(searchList=[d])
 
 
@@ -170,7 +187,9 @@
     machines = [m for m in Machine.select() 
                 if validation.haveAccess(user, m)]    
+    checkpoint.checkpoint('Got my machines')
     on = {}
     has_vnc = {}
     on = g.uptimes
+    checkpoint.checkpoint('Got uptimes')
     for m in machines:
         m.uptime = g.uptimes.get(m)
@@ -183,8 +202,10 @@
     max_memory = validation.maxMemory(user)
     max_disk = validation.maxDisk(user)
+    checkpoint.checkpoint('Got max mem/disk')
     defaults = Defaults(max_memory=max_memory,
                         max_disk=max_disk,
                         owner=user,
                         cdrom='gutsy-i386')
+    checkpoint.checkpoint('Got defaults')
     d = dict(user=user,
              cant_add_vm=validation.cantAddVm(user),
@@ -200,6 +221,8 @@
 def listVms(user, fields):
     """Handler for list requests."""
+    checkpoint.checkpoint('Getting list dict')
     d = getListDict(user)
-    return Template(file='list.tmpl', searchList=[d])
+    checkpoint.checkpoint('Got list dict')
+    return templates.list(searchList=[d])
             
 def vnc(user, fields):
@@ -247,5 +270,5 @@
              hostname=os.environ.get('SERVER_NAME', 'localhost'),
              authtoken=token)
-    return Template(file='vnc.tmpl', searchList=[d])
+    return templates.vnc(searchList=[d])
 
 def getNicInfo(data_dict, machine):
@@ -303,17 +326,18 @@
         result = 'Success!'
         if not back:
-            return Template(file='command.tmpl', searchList=[d])
+            return templates.command(searchList=[d])
     if back == 'list':
         g.clear() #Changed global state
         d = getListDict(user)
         d['result'] = result
-        return Template(file='list.tmpl', searchList=[d])
+        return templates.list(searchList=[d])
     elif back == 'info':
         machine = validation.testMachineId(user, fields.getfirst('machine_id'))
         d = infoDict(user, machine)
         d['result'] = result
-        return Template(file='info.tmpl', searchList=[d])
-    else:
-        raise InvalidInput('back', back, 'Not a known back page.')
+        return templates.info(searchList=[d])
+    else:
+        raise InvalidInput
+    ('back', back, 'Not a known back page.')
 
 def modifyDict(user, fields):
@@ -384,5 +408,5 @@
             setattr(info_dict['defaults'], field, fields.getfirst(field))
     info_dict['result'] = result
-    return Template(file='info.tmpl', searchList=[info_dict])
+    return templates.info(searchList=[info_dict])
     
 
@@ -428,5 +452,5 @@
              mapping=help_mapping)
     
-    return Template(file="help.tmpl", searchList=[d])
+    return templates.help(searchList=[d])
     
 
@@ -436,4 +460,5 @@
 def infoDict(user, machine):
     status = controls.statusInfo(machine)
+    checkpoint.checkpoint('Getting status info')
     has_vnc = hasVnc(status)
     if status is None:
@@ -448,4 +473,5 @@
         cpu_time_float = float(main_status.get('cpu_time', 0))
         cputime = datetime.timedelta(seconds=int(cpu_time_float))
+    checkpoint.checkpoint('Status')
     display_fields = """name uptime memory state cpu_weight on_reboot 
      on_poweroff on_crash on_xend_start on_xend_stop bootloader""".split()
@@ -498,5 +524,10 @@
             pass
             #fields.append((disp, None))
-    max_mem = validation.maxMemory(user, machine)
+
+    checkpoint.checkpoint('Got fields')
+
+
+    max_mem = validation.maxMemory(user, machine, False)
+    checkpoint.checkpoint('Got mem')
     max_disk = validation.maxDisk(user, machine)
     defaults = Defaults()
@@ -504,4 +535,5 @@
         setattr(defaults, name, getattr(machine, name))
     defaults.disk = "%0.2f" % (machine.disks[0].size/1024.)
+    checkpoint.checkpoint('Got defaults')
     d = dict(user=user,
              cdroms=CDROM.select(),
@@ -522,5 +554,6 @@
     machine = validation.testMachineId(user, fields.getfirst('machine_id'))
     d = infoDict(user, machine)
-    return Template(file='info.tmpl', searchList=[d])
+    checkpoint.checkpoint('Got infodict')
+    return templates.info(searchList=[d])
 
 mapping = dict(list=listVms,
@@ -547,4 +580,5 @@
 
 def main(operation, user, fields):    
+    start_time = time.time()
     fun = mapping.get(operation, badOperation)
 
@@ -552,5 +586,7 @@
         connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen')
     try:
+        checkpoint.checkpoint('Before')
         output = fun(u, fields)
+        checkpoint.checkpoint('After')
 
         headers = dict(DEFAULT_HEADERS)
@@ -558,10 +594,12 @@
             new_headers, output = output
             headers.update(new_headers)
-
         e = revertStandardError()
         if e:
             output.addError(e)
         printHeaders(headers)
-        print output
+        output_string =  str(output)
+        checkpoint.checkpoint('output as a string')
+        print output_string
+        print '<pre>%s</pre>' % checkpoint
     except Exception, err:
         if not fields.has_key('js'):
@@ -586,5 +624,4 @@
 
 if __name__ == '__main__':
-    start_time = time.time()
     fields = cgi.FieldStorage()
     u = getUser()
@@ -601,4 +638,6 @@
         operation = 'list'
 
-    main(operation, u, fields)
-
+    #main(operation, u, fields)
+    import profile
+    profile.run('main(operation, u, fields)', 'log-'+operation)
+
