Commits

Sylvain Hellegouarch  committed 9d9d639

basic app for load test recipes

  • Participants
  • Parent commits 58ac04f

Comments (0)

Files changed (4)

File testing/load/myapp/__init__.py

+# -*- coding: utf-8 -*-
+
+if __name__ == '__main__':
+    import os, os.path
+    base_dir = os.path.abspath(os.getcwd())
+    
+    import sys
+    sys.path.append(os.path.normpath(os.path.join(base_dir, "../../../web/database/sql_alchemy")))
+    sys.path.append(os.path.normpath(os.path.join(base_dir, "../../../web/templating/mako_templating")))
+    
+    import cherrypy
+    cherrypy.config.update({'server.socket_port': 8090,
+                            'server.thread_pool': 80})
+    
+    from makoplugin import MakoTemplatePlugin
+    MakoTemplatePlugin(cherrypy.engine, base_dir=os.path.join(base_dir, 'templates')).subscribe()
+
+    from makotool import MakoTool
+    cherrypy.tools.template = MakoTool()
+    
+    from saplugin import SAEnginePlugin
+    saplugin = SAEnginePlugin(cherrypy.engine, 'sqlite:///%s/myapp.db' % base_dir)
+    saplugin.subscribe()
+
+    from satool import SATool
+    cherrypy.tools.db = SATool()
+
+    def create_database():
+        import random
+        cherrypy.log('Creating database')
+        from model import Post, Comment, Base
+        Base.metadata.create_all(saplugin.sa_engine)
+
+        session = saplugin.bind()
+        for i in range(12):
+            post = Post(title=u"Title %d" % i,
+                        author=u"Author %d" % i,
+                        text=u"some text" * i)
+            for j in range(random.randint(1, 6)):
+                comment = Comment(author=u"Commenter %d" % j,
+                                  text=u"blah blah" * j)
+                post.comments.append(comment)
+            session.add(post)
+        session.commit()
+        session.remove()
+        
+    def destroy_database():
+        cherrypy.log('Destroying database')
+        from model import Base
+        Base.metadata.drop_all(saplugin.sa_engine)
+
+    cherrypy.engine.subscribe('start', create_database, priority=60)
+    cherrypy.engine.subscribe('stop', destroy_database, priority=40)
+    
+    from blog import Blog
+    cherrypy.quickstart(
+        Blog(), '/', {'/':
+                      {
+                          'tools.encode.on': False,
+                          'tools.template.on': True,
+                          'tools.db.on': True
+                          },
+                      '/post': {
+                          'tools.template.on': False
+                          },
+                      '/favicon.ico': {
+                          'tools.template.on': False
+                          }
+                      }
+        )

File testing/load/myapp/blog.py

+# -*- coding: utf-8 -*-
+import cherrypy
+
+from model import Post, Comment
+
+__all__ = ['Blog']
+
+class Blog(object):
+    def __init__(self):
+        self.post = BlogPost()
+        
+    @cherrypy.expose
+    @cherrypy.tools.template(template='index.html')
+    def index(self):
+        session = cherrypy.request.db
+        posts = Post.all_(session)
+        return {'posts': posts}
+
+class BlogPost(object):
+    @cherrypy.expose
+    def default(self, id):
+        session = cherrypy.request.db
+        post = Post.get_by_id(session, int(id))
+        
+    @cherrypy.expose
+    def comment(self, id, author, text):
+        session = cherrypy.request.db
+        post = Post.get_by_id(session, int(id))
+        post.comments.append(Comment(author=author, text=text))
+        
+        
+    

File testing/load/myapp/model.py

+# -*- coding: utf-8 -*-
+import datetime
+
+from sqlalchemy import Column, ForeignKey
+from sqlalchemy.types import Unicode, Integer, DateTime
+from sqlalchemy.orm import relationship, backref
+from sqlalchemy.ext.declarative import declarative_base
+
+Base = declarative_base()
+
+__all__ = ['Post', 'Comment']
+
+class Post(Base):
+    __tablename__ = 'post'
+    id = Column(Integer, nullable=False, primary_key=True)
+    title = Column(Unicode)
+    author = Column(Unicode)
+    date = Column(DateTime, default=datetime.datetime.utcnow)
+    text = Column(Unicode)
+    comments = relationship("Comment", backref="post")
+
+    @staticmethod
+    def all_(session):
+        return session.query(Post).all()
+
+    @staticmethod
+    def get_by_id(session, post_id):
+        return session.query(Post).filter(Post.id==post_id).first()
+    
+class Comment(Base):
+    __tablename__ = 'comment'
+    id = Column(Integer, nullable=False, primary_key=True)
+    author = Column(Unicode)
+    date = Column(DateTime, default=datetime.datetime.utcnow)
+    text = Column(Unicode)
+    post_id = Column(Integer, ForeignKey('post.id'))
+
+    @staticmethod
+    def all_(session):
+        return session.query(Comment).all()

File testing/load/myapp/templates/index.html

+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
+    <title></title>
+  </head>
+  <body>
+    % for post in posts:
+    <div>
+      <div>
+	<h1>${post.title} [${post.id}]</h1>
+	<h5>${post.author} at ${post.date}</h5>
+	<p>${post.text}</p>
+      </div>
+      <div>
+	% for comment in post.comments[:5]:
+	<div>
+	  <h5>${comment.author} at ${comment.date}</h5>
+	  <p>${comment.text}</p>
+	</div>
+	% endfor
+      </div>
+    </div>
+    % endfor
+  </body>
+</html>