boundary.py 4.9 KB
Newer Older
B
Bomin Zhang 已提交
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
# -*- coding: utf-8 -*-

import random
import string
import subprocess
import sys
from util.log import *
from util.cases import *
from util.sql import *


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


    def getLimitFromSourceCode( self, name ):
        cmd = "grep -w '#define %s' ../../src/inc/taosdef.h|awk '{print $3}'" % name
        return int(subprocess.check_output(cmd, shell=True))


    def generateString( self, length ):
        chars = string.ascii_uppercase + string.ascii_lowercase
        v = ""
        for i in range( length ):
            v += random.choice( chars )
        return v


    def checkTagBoundaries( self ):
        tdLog.debug( "checking tag boundaries" )
        tdSql.prepare()

        maxTags = self.getLimitFromSourceCode( 'TSDB_MAX_TAGS' )
        totalTagsLen = self.getLimitFromSourceCode( 'TSDB_MAX_TAGS_LEN' )
        tdLog.notice( "max tags is %d" % maxTags )
        tdLog.notice( "max total tag length is %d" % totalTagsLen )

        # for binary tags, 2 bytes are used for length
        tagLen = (totalTagsLen - maxTags * 2) // maxTags
        firstTagLen = totalTagsLen - 2 * maxTags - tagLen * (maxTags - 1)

        sql = "create table cars(ts timestamp, f int) tags(t0 binary(%d)" % firstTagLen
        for i in range( 1, maxTags ):
            sql += ", t%d binary(%d)" % (i, tagLen)
        sql += ");"

        tdLog.debug( "creating super table: " + sql )
        tdSql.execute( sql )
        tdSql.query( 'show stables' )
        tdSql.checkRows( 1 )

        for i in range( 10 ):
            sql = "create table car%d using cars tags('%d'" % (i, i)
            sql += ", '0'" * (maxTags - 1) + ");"
            tdLog.debug( "creating table: " + sql )
            tdSql.execute( sql )

            sql = "insert into car%d values(now, 0);" % i
            tdLog.debug( "inserting data: " + sql )
            tdSql.execute( sql )

        tdSql.query( 'show tables' )
        tdLog.info( 'tdSql.checkRow(10)' )
        tdSql.checkRows( 10 )

        tdSql.query( 'select * from cars;' )
        tdSql.checkRows( 10 )


    def checkColumnBoundaries( self ):
        tdLog.debug( "checking column boundaries" )
        tdSql.prepare()

        # one column is for timestamp
        maxCols = self.getLimitFromSourceCode( 'TSDB_MAX_COLUMNS' ) - 1

        sql = "create table cars (ts timestamp"
        for i in range( maxCols ):
            sql += ", c%d int" % i
        sql += ");"
        tdSql.execute( sql )
        tdSql.query( 'show tables' )
        tdSql.checkRows( 1 )

        sql = "insert into cars values (now"
        for i in range( maxCols ):
            sql += ", %d" % i
        sql += ");"
        tdSql.execute( sql )
        tdSql.query( 'select * from cars' )
        tdSql.checkRows( 1 )


    def checkTableNameBoundaries( self ):
        tdLog.debug( "checking table name boundaries" )
        tdSql.prepare()

        maxTableNameLen = self.getLimitFromSourceCode( 'TSDB_TABLE_NAME_LEN' )
        tdLog.notice( "table name max length is %d" % maxTableNameLen )

        name = self.generateString( maxTableNameLen - 1)
        tdLog.info( "table name is '%s'" % name )

        tdSql.execute( "create table %s (ts timestamp, value int)" % name )
        tdSql.execute( "insert into %s values(now, 0)" % name )

        tdSql.query( 'show tables' )
        tdSql.checkRows( 1 )

        tdSql.query( 'select * from %s' % name )
        tdSql.checkRows( 1 )


    def checkRowBoundaries( self ):
        tdLog.debug( "checking row boundaries" )
        tdSql.prepare()

        # 8 bytes for timestamp
        maxRowSize = 65536 - 8
        maxCols = self.getLimitFromSourceCode( 'TSDB_MAX_COLUMNS' ) - 1

        # for binary cols, 2 bytes are used for length
        colLen = (maxRowSize - maxCols * 2) // maxCols
        firstColLen = maxRowSize - 2 * maxCols - colLen * (maxCols - 1)

        sql = "create table cars (ts timestamp, c0 binary(%d)" % firstColLen
        for i in range( 1, maxCols ):
            sql += ", c%d binary(%d)" % (i, colLen)
        sql += ");"
        tdSql.execute( sql )
        tdSql.query( 'show tables' )
        tdSql.checkRows( 1 )

        col = self.generateString( firstColLen )
        sql = "insert into cars values (now, '%s'" % col
        col = self.generateString( colLen )
        for i in range( 1, maxCols ):
            sql += ", '%s'" % col 
        sql += ");"
        tdLog.info( sql );
        tdSql.execute( sql )
        tdSql.query( "select * from cars" )
        tdSql.checkRows( 1 )


    def run(self):
        self.checkTagBoundaries()
        self.checkColumnBoundaries()
        self.checkTableNameBoundaries()
        self.checkRowBoundaries()


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


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