2023-12-01 08:27:02 +00:00
###################################################################
# 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 -*-
from util . log import *
from util . cases import *
from util . sql import *
from util . common import *
from util . sqlset import *
from util . autogen import *
import random
import time
import traceback
import os
from os import path
2024-11-29 01:56:21 +00:00
import psutil
2023-12-01 08:27:02 +00:00
class TDTestCase :
# init
def init ( self , conn , logSql , replicaVar = 1 ) :
self . replicaVar = int ( replicaVar )
tdLog . debug ( " start to execute %s " % __file__ )
tdSql . init ( conn . cursor ( ) , True )
# autoGen
self . autoGen = AutoGen ( )
def waitTranslation ( self , waitSeconds ) :
# wait end
for i in range ( waitSeconds ) :
sql = " show transactions; "
rows = tdSql . query ( sql )
if rows == 0 :
return True
tdLog . info ( f " i= { i } wait for translation finish ... " )
time . sleep ( 1 )
return False
def getPath ( self , tool = " taosBenchmark " ) :
if ( platform . system ( ) . lower ( ) == ' windows ' ) :
tool = tool + " .exe "
selfPath = os . path . dirname ( os . path . realpath ( __file__ ) )
if ( " community " in selfPath ) :
projPath = selfPath [ : selfPath . find ( " community " ) ]
else :
projPath = selfPath [ : selfPath . find ( " tests " ) ]
paths = [ ]
for root , dirs , files in os . walk ( projPath ) :
if ( ( tool ) in files ) :
rootRealPath = os . path . dirname ( os . path . realpath ( root ) )
if ( " packaging " not in rootRealPath ) :
paths . append ( os . path . join ( root , tool ) )
break
if ( len ( paths ) == 0 ) :
tdLog . exit ( " taosBenchmark not found! " )
return
else :
tdLog . info ( " taosBenchmark found in %s " % paths [ 0 ] )
return paths [ 0 ]
def taosBenchmark ( self , param ) :
binPath = self . getPath ( )
cmd = f " { binPath } { param } "
tdLog . info ( cmd )
os . system ( cmd )
2024-03-28 05:48:46 +00:00
def case1 ( self ) :
tdSql . execute ( f ' create database if not exists d1 vgroups 1 ' )
tdSql . execute ( f ' use d1 ' )
tdSql . execute ( f ' create table st(ts timestamp, i int) tags(t int) ' )
tdSql . execute ( f ' insert into t1 using st tags(1) values(now, 1) (now+1s, 2) ' )
tdSql . execute ( f ' insert into t2 using st tags(2) values(now, 1) (now+1s, 2) ' )
tdSql . execute ( f ' insert into t3 using st tags(3) values(now, 1) (now+1s, 2) ' )
2024-05-15 06:57:50 +00:00
tdSql . execute ( " create stream stream1 fill_history 1 into sta subtable(concat( ' nee.w- ' , tname)) AS SELECT "
2024-03-28 05:48:46 +00:00
" _wstart, count(*), avg(i) FROM st PARTITION BY tbname tname INTERVAL(1m) " , show = True )
2024-03-28 09:54:00 +00:00
tdSql . execute ( " create stream stream2 fill_history 1 into stb subtable(concat( ' new- ' , tname)) AS SELECT "
2024-03-28 05:48:46 +00:00
" _wstart, count(*), avg(i) FROM st PARTITION BY tbname tname INTERVAL(1m) " , show = True )
2024-07-23 06:11:40 +00:00
sql = " select * from sta "
tdSql . check_rows_loop ( 3 , sql , loopCount = 100 , waitTime = 0.5 )
2024-04-01 11:59:30 +00:00
tdSql . query ( " select tbname from sta order by tbname " )
2024-05-15 06:57:50 +00:00
if not tdSql . getData ( 0 , 0 ) . startswith ( ' nee_w-t1_sta_ ' ) :
2024-04-01 11:59:30 +00:00
tdLog . exit ( " error1 " )
2024-05-15 06:57:50 +00:00
if not tdSql . getData ( 1 , 0 ) . startswith ( ' nee_w-t2_sta_ ' ) :
2024-04-01 11:59:30 +00:00
tdLog . exit ( " error2 " )
2024-05-15 06:57:50 +00:00
if not tdSql . getData ( 2 , 0 ) . startswith ( ' nee_w-t3_sta_ ' ) :
2024-04-01 11:59:30 +00:00
tdLog . exit ( " error3 " )
2024-03-28 05:48:46 +00:00
2024-07-23 06:11:40 +00:00
sql = " select * from stb "
tdSql . check_rows_loop ( 3 , sql , loopCount = 100 , waitTime = 0.5 )
2024-04-01 11:59:30 +00:00
tdSql . query ( " select tbname from stb order by tbname " )
2024-05-15 06:57:50 +00:00
if not tdSql . getData ( 0 , 0 ) . startswith ( ' new-t1_stb_ ' ) :
2024-04-01 11:59:30 +00:00
tdLog . exit ( " error4 " )
2024-05-15 06:57:50 +00:00
if not tdSql . getData ( 1 , 0 ) . startswith ( ' new-t2_stb_ ' ) :
2024-04-01 11:59:30 +00:00
tdLog . exit ( " error5 " )
2024-05-15 06:57:50 +00:00
if not tdSql . getData ( 2 , 0 ) . startswith ( ' new-t3_stb_ ' ) :
2024-04-01 11:59:30 +00:00
tdLog . exit ( " error6 " )
2024-11-29 01:56:21 +00:00
def caseDropStream ( self ) :
tdLog . info ( f " start caseDropStream " )
sql = " drop database if exists d1; "
tdSql . query ( sql )
sql = " drop database if exists db; "
tdSql . query ( sql )
sql = " show streams; "
tdSql . query ( sql )
tdSql . check_rows_loop ( 0 , sql , loopCount = 100 , waitTime = 0.5 )
sql = " select * from information_schema.ins_stream_tasks; "
tdSql . query ( sql )
tdSql . check_rows_loop ( 0 , sql , loopCount = 100 , waitTime = 0.5 )
self . taosBenchmark ( " -d db -t 2 -v 2 -n 1000000 -y " )
# create stream
tdSql . execute ( " use db; " )
tdSql . execute ( " create stream stream4 fill_history 1 into sta4 as select _wstart, sum(current),avg(current),last(current),min(voltage),first(voltage),last(phase),max(phase),count(phase), _wend, _wduration from meters partition by tbname, ts interval(10a); " , show = True )
time . sleep ( 10 )
sql = " select * from information_schema.ins_stream_tasks where status == ' ready ' ; "
tdSql . query ( sql , show = True )
tdSql . check_rows_loop ( 4 , sql , loopCount = 100 , waitTime = 0.5 )
pl = psutil . pids ( )
for pid in pl :
try :
if psutil . Process ( pid ) . name ( ) == ' taosd ' :
taosdPid = pid
break
except psutil . NoSuchProcess :
pass
tdLog . info ( " taosd pid: {} " . format ( taosdPid ) )
p = psutil . Process ( taosdPid )
cpuInfo = p . cpu_percent ( interval = 5 )
tdLog . info ( " taosd cpu: {} " . format ( cpuInfo ) )
tdSql . execute ( " drop stream stream4; " , show = True )
sql = " show streams; "
tdSql . query ( sql , show = True )
tdSql . check_rows_loop ( 0 , sql , loopCount = 100 , waitTime = 0.5 )
sql = " select * from information_schema.ins_stream_tasks; "
tdSql . query ( sql , show = True )
tdSql . check_rows_loop ( 0 , sql , loopCount = 100 , waitTime = 0.5 )
for i in range ( 10 ) :
cpuInfo = p . cpu_percent ( interval = 5 )
tdLog . info ( " taosd cpu: {} " . format ( cpuInfo ) )
if cpuInfo < 10 :
return
else :
time . sleep ( 1 )
continue
cpuInfo = p . cpu_percent ( interval = 5 )
tdLog . info ( " taosd cpu: {} " . format ( cpuInfo ) )
if cpuInfo > 10 :
tdLog . exit ( " drop stream failed, stream tasks are still running " )
2023-12-01 08:27:02 +00:00
# run
def run ( self ) :
2024-03-28 05:48:46 +00:00
self . case1 ( )
2023-12-01 08:27:02 +00:00
# gen data
random . seed ( int ( time . time ( ) ) )
self . taosBenchmark ( " -d db -t 2 -v 2 -n 1000000 -y " )
# create stream
tdSql . execute ( " use db " )
2024-03-29 01:07:09 +00:00
tdSql . execute ( " create stream stream3 fill_history 1 into sta as select count(*) as cnt from meters interval(10a); " , show = True )
2023-12-01 08:27:02 +00:00
sql = " select count(*) from sta "
# loop wait max 60s to check count is ok
tdLog . info ( " loop wait result ... " )
2024-04-17 05:51:49 +00:00
time . sleep ( 5 )
2023-12-26 06:40:15 +00:00
tdSql . checkDataLoop ( 0 , 0 , 100000 , sql , loopCount = 120 , waitTime = 0.5 )
2023-12-01 08:27:02 +00:00
2023-12-18 06:26:15 +00:00
time . sleep ( 5 )
2023-12-01 08:27:02 +00:00
# check all data is correct
sql = " select * from sta where cnt != 20; "
tdSql . query ( sql )
tdSql . checkRows ( 0 )
# check ts interval is correct
sql = " select * from ( select diff(_wstart) as tsdif from sta ) where tsdif != 10; "
tdSql . query ( sql )
tdSql . checkRows ( 0 )
2024-11-29 01:56:21 +00:00
self . caseDropStream ( )
2023-12-01 08:27:02 +00:00
# stop
def stop ( self ) :
tdSql . close ( )
tdLog . success ( " %s successfully executed " % __file__ )
tdCases . addLinux ( __file__ , TDTestCase ( ) )