示例#1
0
    def test_init_4(self):
        from shortening.service.models import db, User, Shortening
        from peewee import IntegrityError

        # Given...
        db = SqliteDatabase(TEST_DB, check_same_thread=False)
        db.connect()

        # remove all records
        Shortening.delete().execute()

        # Test...
        total = 1000
        t0 = time.time()
        for x in range(total):
            try:
                s = Shortening(user=User.get(login="******"), short_url=Shortening.generate_uniqie_shortening(),
                               long_url="http://google.com/")
                s.save()
            except IntegrityError:
                pass
                # Shortening already exists

        t1 = time.time()

        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert records with random name performance test, method 1, set size: %s, time: %s s, per row: %s s" %
              (total, diff, diff_per_row))
        db.close()
示例#2
0
    def test_service_performance(self):
        from shortening.service.models import db, Shortening

        def handle_request(response):
            if response.error:
                # should not be here !!!
                self.assertTrue(False)
            else:
                self.assertTrue(self.check_header(response))
                cookies = self.get_cookies(response)
                # RUN TEST
                url = BASE_URL + "/generate?long_url="
                self.asynchronous_test(cookies, url)

        # Given...
        db = SqliteDatabase(TEST_DB, check_same_thread=False)
        db.connect()

        # remove all records
        Shortening.delete().execute()

        # Test...
        app = Application()
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(options.port)
        print("Step1: /login")
        request = tornado.httpclient.HTTPRequest(BASE_URL + "/login?username=user1&password=pass1",
                                                 follow_redirects=False,
                                                 method='POST',
                                                 body="",
                                                 validate_cert=False)

        http_client = httpclient.AsyncHTTPClient()
        http_client.fetch(request, handle_request)
        self.wait(timeout=90)
        print("Finished")
示例#3
0
    def test_insert_existing_performance(self):
        from shortening.service.models import db, User, Shortening
        from peewee import IntegrityError, DoesNotExist

        # Given...
        db = SqliteDatabase(TEST_DB, check_same_thread=False)
        db.connect()

        # remove all records
        Shortening.delete().execute()

        # Test...
        total = 10000
        t0 = time.time()
        for x in range(total):
            try:
                s = Shortening(user=User.get(login="******"), short_url="0002", long_url="http://long_url/")
                s.save()
            except IntegrityError:
                pass
                # Shortening already exists

        t1 = time.time()
        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert of existing record performance test, method 1, set size: %s, time: %s, per row: %s s" %
              (total, diff, diff_per_row))

        # THIS IS FASTEST METHOD
        # remove all records
        Shortening.delete().execute()
        t0 = time.time()
        for x in range(total):
            query = Shortening.select().where(Shortening.short_url == "80001")
            if not query.exists():
                # short_url does not exists exists
                pass

        t1 = time.time()
        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert of existing record performance test, method 2, set size: %s, time: %s, per row: %s s" %
              (total, diff, diff_per_row))

        # remove all records
        Shortening.delete().execute()
        t0 = time.time()
        for x in range(total):
            try:
                s = Shortening.get(Shortening.short_url == "80001")
                print(":" + str(s))
            except DoesNotExist:
                # Shortening not exists
                pass

        t1 = time.time()
        diff = t1 - t0
        diff_per_row = diff / total
        print("Insert of existing record performance test, method 3, set size: %s, time: %s, per row: %s s" %
              (total, diff, diff_per_row))

        db.close()