metric_n.py 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
import time
import taos
from util.log import tdLog
from util.cases import tdCases
from util.sql import tdSql


class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)

    def run(self):
        tbNum = 10
        rowNum = 20
        totalNum = tbNum * rowNum

        tdSql.prepare()

        tdLog.info("===== preparing data =====")
        tdSql.execute(
            "create table stb(ts timestamp, tbcol int, tbcol2 float) tags(tgcol int)")
        for i in range(tbNum):
            tdSql.execute("create table tb%d using stb tags(%d)" % (i, i))
            for j in range(rowNum):
                tdSql.execute(
                    "insert into tb%d values (now - %dm, %d, %d)" %
                    (i, 1440 - j, j, j))
        time.sleep(0.1)

        tdLog.info("===== step 1 =====")
        tdSql.query("select count(*), count(tbcol), count(tbcol2) from stb interval(1d)")
        tdSql.checkData(0, 1, totalNum)
        tdSql.checkData(0, 2, totalNum)
        tdSql.checkData(0, 3, totalNum)

        tdLog.info("===== step 2 =====")
        tdSql.execute("create table strm_c3 as select count(*), count(tbcol), count(tbcol2) from stb interval(1d)")

        tdLog.info("===== step 3 =====")
        tdSql.execute("create table strm_c32 as select count(*), count(tbcol) as c1, count(tbcol2) as c2, count(tbcol) as c3, count(tbcol) as c4, count(tbcol) as c5, count(tbcol) as c6, count(tbcol) as c7, count(tbcol) as c8, count(tbcol) as c9, count(tbcol) as c10, count(tbcol) as c11, count(tbcol) as c12, count(tbcol) as c13, count(tbcol) as c14, count(tbcol) as c15, count(tbcol) as c16, count(tbcol) as c17, count(tbcol) as c18, count(tbcol) as c19, count(tbcol) as c20, count(tbcol) as c21, count(tbcol) as c22, count(tbcol) as c23, count(tbcol) as c24, count(tbcol) as c25, count(tbcol) as c26, count(tbcol) as c27, count(tbcol) as c28, count(tbcol) as c29, count(tbcol) as c30 from stb interval(1d)")

        tdLog.info("===== step 4 =====")
        tdSql.query("select count(*), count(tbcol) as c1, count(tbcol2) as c2, count(tbcol) as c3, count(tbcol) as c4, count(tbcol) as c5, count(tbcol) as c6, count(tbcol) as c7, count(tbcol) as c8, count(tbcol) as c9, count(tbcol) as c10, count(tbcol) as c11, count(tbcol) as c12, count(tbcol) as c13, count(tbcol) as c14, count(tbcol) as c15, count(tbcol) as c16, count(tbcol) as c17, count(tbcol) as c18, count(tbcol) as c19, count(tbcol) as c20, count(tbcol) as c21, count(tbcol) as c22, count(tbcol) as c23, count(tbcol) as c24, count(tbcol) as c25, count(tbcol) as c26, count(tbcol) as c27, count(tbcol) as c28, count(tbcol) as c29, count(tbcol) as c30  from stb interval(1d)")
        tdSql.checkData(0, 1, totalNum)
        tdSql.checkData(0, 2, totalNum)
        tdSql.checkData(0, 3, totalNum)

        tdLog.info("===== step 5 =====")
        tdSql.execute("create table strm_c31 as select count(*), count(tbcol) as c1, count(tbcol2) as c2, count(tbcol) as c3, count(tbcol) as c4, count(tbcol) as c5, count(tbcol) as c6, count(tbcol) as c7, count(tbcol) as c8, count(tbcol) as c9, count(tbcol) as c10, count(tbcol) as c11, count(tbcol) as c12, count(tbcol) as c13, count(tbcol) as c14, count(tbcol) as c15, count(tbcol) as c16, count(tbcol) as c17, count(tbcol) as c18, count(tbcol) as c19, count(tbcol) as c20, count(tbcol) as c21, count(tbcol) as c22, count(tbcol) as c23, count(tbcol) as c24, count(tbcol) as c25, count(tbcol) as c26, count(tbcol) as c27, count(tbcol) as c28, count(tbcol) as c29, count(tbcol) as c30 from stb interval(1d)")

        tdLog.info("===== step 6 =====")
        tdSql.query("select avg(tbcol), sum(tbcol), min(tbcol), max(tbcol),  first(tbcol), last(tbcol) from stb interval(1d)")
        tdSql.checkData(0, 1, 9.5)
        tdSql.checkData(0, 2, 1900)
        tdSql.checkData(0, 3, 0)
        tdSql.checkData(0, 4, 19)
        tdSql.checkData(0, 5, 0)
        tdSql.checkData(0, 6, 19)
        tdSql.execute("create table strm_avg as select avg(tbcol), sum(tbcol), min(tbcol), max(tbcol),  first(tbcol), last(tbcol) from stb interval(1d)")

        tdLog.info("===== step 7 =====")
        tdSql.query("select avg(tbcol), sum(tbcol), min(tbcol), max(tbcol),  first(tbcol), last(tbcol), count(tbcol) from stb where ts < now + 4m interval(1d)")
        tdSql.checkData(0, 1, 9.5)
        tdSql.checkData(0, 2, 1900)
        tdSql.checkData(0, 3, 0)
        tdSql.checkData(0, 4, 19)
        tdSql.checkData(0, 5, 0)
        tdSql.checkData(0, 6, 19)
        tdSql.checkData(0, 7, totalNum)

        tdLog.info("===== step 8 =====")
        tdSql.query("select avg(tbcol), sum(tbcol), min(tbcol), max(tbcol),  first(tbcol), last(tbcol), count(tbcol)  from stb where ts < now + 4m interval(1d)")
        tdSql.checkData(0, 1, 9.5)
        tdSql.checkData(0, 2, 1900)
        tdSql.checkData(0, 3, 0)
        tdSql.checkData(0, 4, 19)
        tdSql.checkData(0, 5, 0)
        tdSql.checkData(0, 6, 19)
        tdSql.checkData(0, 7, totalNum)

        tdLog.info("===== step 9 =====")
        tdSql.waitedQuery("select * from strm_c3", 1, 120)
        tdSql.checkData(0, 1, totalNum)
        tdSql.checkData(0, 2, totalNum)
        tdSql.checkData(0, 3, totalNum)

        tdLog.info("===== step 10 =====")
        tdSql.waitedQuery("select * from strm_c31", 1, 30)
        for i in range(1, 10):
            tdSql.checkData(0, i, totalNum)

        tdLog.info("===== step 11 =====")
        tdSql.waitedQuery("select * from strm_avg", 1, 20)
        tdSql.checkData(0, 1, 9.5)
        tdSql.checkData(0, 2, 1900)
        tdSql.checkData(0, 3, 0)
        tdSql.checkData(0, 4, 19)
        tdSql.checkData(0, 5, 0)
        tdSql.checkData(0, 6, 19)


    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())