Exemple #1
0
	def wrapper(*args, **kwargs):
		if not request.query.do_log:
			return callback(*args, **kwargs)

		import os

		fname = 'logs/{}.html'.format(request.path
			.replace('/', '.')
			.replace('<', '')
			.replace('>', '')
			.lstrip('.')
		)
		try:
			os.remove(fname)
		except:
			pass

		import sqltap

		profiler = sqltap.start()

		try:
			return callback(*args, **kwargs)
		finally:
			try:
				statistics = profiler.collect()
				profiler.stop()
				sqltap.report(statistics, fname)
			except Exception:
				raise
Exemple #2
0
	def wrapper(*args, **kwargs):
		if not request.query.do_log:
			return callback(*args, **kwargs)

		import os

		fname = 'logs/{}.html'.format(request.path
			.replace('/', '.')
			.replace('<', '')
			.replace('>', '')
			.lstrip('.')
		)
		try:
			os.remove(fname)
		except:
			pass

		import sqltap

		profiler = sqltap.start()

		try:
			return callback(*args, **kwargs)
		finally:
			try:
				statistics = profiler.collect()
				profiler.stop()
				sqltap.report(statistics, fname)
			except Exception:
				raise
Exemple #3
0
 def after_request(self, resp):
     if self.processing:
         stats = self.profiler.collect()
         tap.report(
             stats,
             os.environ.get('SQLTAP_PATH'))
     return resp
Exemple #4
0
 def on_finish(self):
     # NOTE: if client closes connection, it's possible on_finish() won't trigger.
     # see https://github.com/facebook/tornado/issues/473
     if self.dbsession is not None:
         self.dbsession.close()
         if SQLTAP:
             statistics = sqltap.collect()
             print len(statistics)," queries"
             sqltap.report(statistics, "/tmp/report.html")
Exemple #5
0
 def on_finish(self):
     # NOTE: if client closes connection, it's possible on_finish() won't trigger.
     # see https://github.com/facebook/tornado/issues/473
     if self.dbsession is not None:
         self.dbsession.close()
         if SQLTAP:
             statistics = sqltap.collect()
             print len(statistics), " queries"
             sqltap.report(statistics, "/tmp/report.html")
Exemple #6
0
    def test_report(self):
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A)
        qtext = sqltap.format_sql(str(q))
        q.all()

        stats = profiler.collect()
        report = sqltap.report(stats, report_format="html")
        assert REPORT_TITLE in report
        assert qtext in report
        report = sqltap.report(stats, report_format="text")
        assert REPORT_TITLE in report
        assert sqlparse.format(qtext, reindent=True) in report
        profiler.stop()
Exemple #7
0
    def test_report(self):
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A)
        qtext = sqltap.format_sql(str(q))
        q.all()

        stats = profiler.collect()
        report = sqltap.report(stats, report_format="html")
        assert REPORT_TITLE in report
        assert qtext in report
        report = sqltap.report(stats, report_format="text")
        assert REPORT_TITLE in report
        assert sqlparse.format(qtext, reindent=True) in report
        profiler.stop()
    def test_report_aggregation_w_different_param_sets(self):
        """
        Test that report rendering works with groups of queries
        containing different parameter sets
        """

        sess = self.Session()

        a1 = self.A(name=uuid.uuid4().hex, description='')
        a2 = self.A(name=uuid.uuid4().hex, description='')
        sess.add_all([a1, a2])
        sess.commit()

        a1 = sess.query(self.A).get(a1.id)
        a2 = sess.query(self.A).get(a2.id)

        profiler = sqltap.start(self.engine)
        # this will create queries with the same text, but different param sets
        # (different query.params.keys() collections)
        a1.name = uuid.uuid4().hex
        a2.description = uuid.uuid4().hex
        sess.flush()

        report = sqltap.report(profiler.collect())
        print(report)
        profiler.stop()
        self.check_report(report)
Exemple #9
0
    def test_report_aggregation_w_different_param_sets(self):
        """
        Test that report rendering works with groups of queries
        containing different parameter sets
        """

        sess = self.Session()

        a1 = self.A(name=uuid.uuid4().hex, description="")
        a2 = self.A(name=uuid.uuid4().hex, description="")
        sess.add_all([a1, a2])
        sess.commit()

        a1 = sess.query(self.A).get(a1.id)
        a2 = sess.query(self.A).get(a2.id)

        profiler = sqltap.start(self.engine)
        # this will create queries with the same text, but different param sets
        # (different query.params.keys() collections)
        a1.name = uuid.uuid4().hex
        a2.description = uuid.uuid4().hex
        sess.flush()

        report = sqltap.report(profiler.collect())
        print(report)
        profiler.stop()
        self.check_report(report)
Exemple #10
0
    def test_report_raw_sql(self):
        """ Ensure that reporting works when raw SQL queries were emitted. """
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        sql = 'SELECT * FROM %s' % self.A.__tablename__
        sess.connection().execute(sql)

        stats = profiler.collect()
        report = sqltap.report(stats, report_format="html")
        assert REPORT_TITLE in report
        assert sqltap.format_sql(sql) in report
        report = sqltap.report(stats, report_format="text")
        assert REPORT_TITLE in report
        assert sqlparse.format(sql, reindent=True) in report
        profiler.stop()
Exemple #11
0
    def test_report_raw_sql(self):
        """ Ensure that reporting works when raw SQL queries were emitted. """
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        sql = 'SELECT * FROM %s' % self.A.__tablename__
        sess.connection().execute(sql)

        stats = profiler.collect()
        report = sqltap.report(stats, report_format="html")
        assert REPORT_TITLE in report
        assert sqltap.format_sql(sql) in report
        report = sqltap.report(stats, report_format="text")
        assert REPORT_TITLE in report
        assert sqlparse.format(sql, reindent=True) in report
        profiler.stop()
Exemple #12
0
def shutdown_session(exception=None):
    # if database profiling is enabled, save a report
    if app.config.get('DEBUG_DB_PROFILING',
                      False) and not request.path.startswith('/static'):
        # filter out any statistics which aren't for this request
        stats_all = sqltap_sess.collect()
        stats_req = list(
            filter(lambda x: x.user_context == g.req_id,
                   sqltap_sess.collect()))
        sqltap.report(
            stats_all,
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         "static/db_profile_report_all.html"))
        sqltap.report(
            stats_req,
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         "static/db_profile_report_req.html"))
Exemple #13
0
    def test_report_ddl(self):
        """ Ensure that reporting works when DDL were emitted """
        engine2 = create_engine('sqlite:///:memory:', echo=True)
        Base2 = declarative_base(bind=engine2)

        class B(Base2):
            __tablename__ = "b"
            id = Column("id", Integer, primary_key=True)

        profiler = sqltap.start(engine2)
        Base2.metadata.create_all(engine2)

        stats = profiler.collect()
        report = sqltap.report(stats, report_format="html")
        assert REPORT_TITLE in report
        report = sqltap.report(stats, report_format="text")
        assert REPORT_TITLE in report
        profiler.stop()
Exemple #14
0
    def test_report_ddl(self):
        """ Ensure that reporting works when DDL were emitted """
        engine2 = create_engine('sqlite:///:memory:', echo=True)
        Base2 = declarative_base(bind=engine2)

        class B(Base2):
            __tablename__ = "b"
            id = Column("id", Integer, primary_key=True)

        profiler = sqltap.start(engine2)
        Base2.metadata.create_all(engine2)

        stats = profiler.collect()
        report = sqltap.report(stats, report_format="html")
        assert REPORT_TITLE in report
        report = sqltap.report(stats, report_format="text")
        assert REPORT_TITLE in report
        profiler.stop()
Exemple #15
0
    def test_report(self):
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A)
        qtext = str(q)
        q.all()

        report = sqltap.report(profiler.collect())
        assert 'sqltap profile report' in report
        assert qtext in report
Exemple #16
0
 def decorated_get(*args, **kwargs):
     if 'tuning' in kwargs and kwargs['tuning']:
         if not current_app.config['DEBUG']:
             abort(403, message="`DEBUG` must be on for tuning page")
         profiler = sqltap.start()
         result = f(*args, **kwargs)
         profiler.stop()
         stats = profiler.collect()
         return make_response(sqltap.report(stats, 'tuning.html'))
     result = f(*args, **kwargs)
     return result
Exemple #17
0
    def test_report(self):
        sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A)
        qtext = str(q)
        q.all()


        report = sqltap.report(sqltap.collect())
        assert 'SQLTap Report' in report
        assert qtext in report
Exemple #18
0
    def test_report_to_file(self):
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A).filter(self.A.name == u"معاذ")
        q.all()
        stats = profiler.collect()

        fd, temp_path = tempfile.mkstemp()
        os.close(fd)
        report = sqltap.report(stats, filename=temp_path)

        with open(temp_path) as fp:
            self.assertEqual(report, fp.read())
Exemple #19
0
    def test_report_to_file(self):
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A).filter(self.A.name == u"معاذ")
        q.all()
        stats = profiler.collect()

        fd, temp_path = tempfile.mkstemp()
        os.close(fd)
        report = sqltap.report(stats, filename=temp_path)

        with open(temp_path) as fp:
            self.assertEqual(report, fp.read())
Exemple #20
0
    def test_report_escaped(self):
        """ Test that . """
        engine2 = create_engine('sqlite:///:memory:', echo=True)

        Base = declarative_base(bind = engine2)

        class B(Base):
            __tablename__ = "b"
            id = Column("id", Unicode, primary_key = True)

        Base.metadata.create_all(engine2)
        Session = sessionmaker(bind=engine2)
        profiler = sqltap.start(engine2)

        sess = Session()
        sess.query(B).filter(B.id == u"<blockquote class='test'>").all()

        report = sqltap.report(profiler.collect())
        assert "<blockquote class='test'>" not in report
        assert "&#34;&lt;blockquote class=&#39;test&#39;&gt;&#34;" in report
Exemple #21
0
    def test_report_aggregation(self):
        """
        Test that we aggregate stats for the same query called from
        different locations as well as aggregating queries called
        from the same stack trace.
        """
        sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A)

        q.all()
        q.all()
            
        q2 = sess.query(self.A).filter(self.A.id == 10)
        for i in xrange(10):
            q2.all()

        report = sqltap.report(sqltap.collect())
        assert '2 unique' in report
        assert 'Query count: 10' in report
Exemple #22
0
    def test_report_aggregation(self):
        """
        Test that we aggregate stats for the same query called from
        different locations as well as aggregating queries called
        from the same stack trace.
        """
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A)

        q.all()
        q.all()
            
        q2 = sess.query(self.A).filter(self.A.id == 10)
        for i in range(10):
            q2.all()

        report = sqltap.report(profiler.collect())
        print(report)
        assert '2 unique' in report
        assert '<dd>10</dd>' in report
Exemple #23
0
    def test_report_aggregation(self):
        """
        Test that we aggregate stats for the same query called from
        different locations as well as aggregating queries called
        from the same stack trace.
        """
        profiler = sqltap.start(self.engine)

        sess = self.Session()
        q = sess.query(self.A)

        q.all()
        q.all()

        q2 = sess.query(self.A).filter(self.A.id == 10)
        for i in range(10):
            q2.all()

        report = sqltap.report(profiler.collect())
        print(report)
        assert '2 unique' in report
        assert '<dd>10</dd>' in report
        profiler.stop()
Exemple #24
0
async def add_sql_tap(request: Request, call_next):
    profiler = sqltap.start()
    response = await call_next(request)
    statistics = profiler.collect()
    sqltap.report(statistics, "report.txt", report_format="text")
    return response
Exemple #25
0
from database import dbsess
from models import *
from sqlalchemy.orm import aliased
import sqltap
import logging
logging.basicConfig(filename='log')
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
"""
clause.compile(dialect=conn.engine.dialect, compile_kwargs={'literal_binds': True})
"""

#root = dbsess.query(Node).filter_by(pid = None).one()
#subchild_1 = dbsess.query(Node).filter_by(id = 4).one()

"""
class SessionWithCompiledSQL(sqltap.ProfilingSession):
    def _after_exec(self, conn, clause, multiparams, params, results):
        duration = time.time() - conn._sqltap_query_start_time
        context = (None if not self.user_context_fn else
                   self.user_context_fn(conn, clause, multiparams, params, results))
        try:
            text = clause.compile(dialect=conn.engine.dialect, compile_kwargs={'literal_binds': True})
        except AttributeError:
            text = clause
        self.collect_fn(QueryStats(text, traceback.extract_stack()[:-1], duration, context))

profiler = SessionWithCompiledSQL()
with profiler:
    nodealias = aliased(Node)
    dbsess.query(Node).filter(Node.name == 'subchild_1').\
        join(nodealias, Node.parent).\
Exemple #26
0
import os
import sys

from webcheck.cmd import parser, main

if __name__ == '__main__':
    try:
        args = parser.parse_args()
        if args.profile:
            fname = os.path.join(args.output_dir, 'webcheck.prof')
            try:
                import cProfile
            except ImportError:
                import profile as cProfile
            try:
                import sqltap
                sqltap.start()
            except ImportError:
                pass
            cProfile.run('main(vars(args))', fname)
            if 'sqltap' in locals():
                statistics = sqltap.collect()
                sqltap.report(statistics,
                              os.path.join(args.output_dir, 'sqltap.html'))
        else:
            main(vars(args))
    except KeyboardInterrupt:
        sys.stderr.write('Interrupted\n')
        sys.exit(1)
Exemple #27
0
 def after_request(self, resp):
     if self.processing:
         stats = self.profiler.collect()
         tap.report(stats, conf.SQLTAP_PATH)
     return resp