Esempio n. 1
0
 def test_start_twice(self):
     """ Ensure that multiple calls to ProfilingSession.start() raises assertion
     error.
     """
     profiler = sqltap.ProfilingSession(self.engine)
     profiler.start()
     profiler.start()
Esempio n. 2
0
    def test_context_manager(self):
        sess = self.Session()
        q = sess.query(self.A)
        profiled = sqltap.ProfilingSession(self.engine)

        with profiled:
            q.all()
                
        q.all()
        
        stats = profiled.collect()
        assert len(_startswith(stats, 'SELECT')) == 1
Esempio n. 3
0
 def test_start_twice(self):
     """
     Ensure that multiple calls to ProfilingSession.start() raises assertion
     error.
     """
     profiler = sqltap.ProfilingSession(self.engine)
     profiler.start()
     try:
         profiler.start()
         raise ValueError("Second start should have asserted")
     except AssertionError:
         pass
     except:
         assert False, "Got some non-assertion exception"
     profiler.stop()
Esempio n. 4
0
    def test_start_stop(self):
        sess = self.Session()
        q = sess.query(self.A)
        profiled = sqltap.ProfilingSession(self.engine)

        q.all()

        stats = profiled.collect()
        assert len(_startswith(stats, 'SELECT')) == 0

        profiled.start()
        q.all()
        q.all()
        profiled.stop()
        q.all()

        stats2 = profiled.collect()
        assert len(_startswith(stats2, 'SELECT')) == 2
Esempio n. 5
0
 def test_no_before_exec(self):
     """
     If SQLTap is started dynamically on one thread,
     any SQLAlchemy sessions running on other threads start being profiled.
     Their connections did not receive the before_execute event,
     so when they receive the after_execute event, extra care must be taken.
     """
     profiler = sqltap.ProfilingSession(self.engine)
     sqlalchemy.event.listen(self.engine, "after_execute",
                             profiler._after_exec)
     sess = self.Session()
     q = sess.query(self.A)
     q.all()
     stats = profiler.collect()
     assert len(stats) == 1
     assert stats[0].duration == 0.0, str(stats[0].duration)
     sqlalchemy.event.remove(self.engine, "after_execute",
                             profiler._after_exec)
Esempio n. 6
0
    def test_decorator(self):
        """ Test that queries issued in a decorated function are profiled """
        sess = self.Session()
        q = sess.query(self.A)
        profiled = sqltap.ProfilingSession(self.engine)

        @profiled
        def test_function():
            self.Session().query(self.A).all()
            
        q.all()
        
        stats = profiled.collect()
        assert len(_startswith(stats, 'SELECT')) == 0

        test_function()
        test_function()
        q.all()

        stats = profiled.collect()
        assert len(_startswith(stats, 'SELECT')) == 2
    __tablename__ = 'blogs'

    id = sa.Column(sa.Integer, primary_key=True)

    # lazy = True / 'joined' are the same
    comments = sa.orm.relationship(lambda: Comment,
                                   lazy='dynamic',
                                   backref='blog')


class Comment(Base, MethodsMixin):
    __tablename__ = 'comments'

    id = sa.Column(sa.Integer, primary_key=True)
    blog_id = sa.Column(sa.ForeignKey(Blog.id, ondelete='cascade'),
                        nullable=False)


init_entities(Blog, Comment)
enable_logging()

sess = Session()

profiler = sqltap.ProfilingSession()
with profiler:
    for b in sess.query(Blog):
        assert b.comments.order_by('id').all()

query_stats = profiler.collect()
assert len(query_stats) == 3
Esempio n. 8
0
 def test_context_return_self(self):
     with sqltap.ProfilingSession() as profiler:
         assert type(profiler) is sqltap.ProfilingSession