“e79e50ae364da43ec07820e61dfb3d5a130146eb”上不存在“source/libs/git@gitcode.net:taosdata/tdengine.git”
taosdemoTestQueryWithJson.py 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
###################################################################
#           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 os
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *
import time
from datetime import datetime
22
import ast
23
import re
24 25
# from assertpy import assert_that
import subprocess
26

27

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

33 34 35 36 37 38 39 40 41 42 43 44
    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
            if ("taosd" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
45
                    buildPath = root[:len(root) - len("/build/bin")]
46 47
                    break
        return buildPath
48 49

    # 获取taosc接口查询的结果文件中的内容,返回每行数据,并断言数据的第一列内容。
50
    def assertfileDataTaosc(self, filename, expectResult):
51 52
        self.filename = filename
        self.expectResult = expectResult
53
        with open("%s" % filename, 'r+') as f1:
54
            for line in f1.readlines():
55 56
                queryResultTaosc = line.strip().split()[0]
                self.assertCheck(filename, queryResultTaosc, expectResult)
57 58

    # 获取restful接口查询的结果文件中的关键内容,目前的关键内容找到第一个key就跳出循,所以就只有一个数据。后续再修改多个结果文件。
59
    def getfileDataRestful(self, filename):
60
        self.filename = filename
61
        with open("%s" % filename, 'r+') as f1:
62 63 64
            for line in f1.readlines():
                contents = line.strip()
                if contents.find("data") != -1:
65 66
                    pattern = re.compile("{.*}")
                    contents = pattern.search(contents).group()
67
                    contentsDict = ast.literal_eval(contents)   # 字符串转换为字典
68
                    queryResultRest = contentsDict['data'][0][0]
69
                    break
70 71 72 73
                else :
                    queryResultRest = ""
        return queryResultRest
        
74 75

    # 获取taosc接口查询次数
76
    def queryTimesTaosc(self, filename):
77
        self.filename = filename
78 79
        command = 'cat %s |wc -l' % filename
        times = int(subprocess.getstatusoutput(command)[1])
80 81 82
        return times

    # 获取restful接口查询次数
83
    def queryTimesRestful(self, filename):
84
        self.filename = filename
85 86
        command = 'cat %s |grep "200 OK" |wc -l' % filename
        times = int(subprocess.getstatusoutput(command)[1])
87 88 89
        return times

   # 定义断言结果是否正确。不正确返回错误结果,正确即通过。
90
    def assertCheck(self, filename, queryResult, expectResult):
91 92 93 94
        self.filename = filename
        self.queryResult = queryResult
        self.expectResult = expectResult
        args0 = (filename, queryResult, expectResult)
95
        assert queryResult == expectResult, "Queryfile:%s ,result is %s != expect: %s" % args0
96

97 98 99 100 101 102
    def run(self):
        buildPath = self.getBuildPath()
        if (buildPath == ""):
            tdLog.exit("taosd not found!")
        else:
            tdLog.info("taosd found in %s" % buildPath)
103 104
        binPath = buildPath + "/build/bin/"

105
        # delete useless files
106
        os.system("rm -rf ./query_res*")
107
        os.system("rm -rf ./all_query*")
108 109 110

        # taosc query: query specified  table  and query  super table
        os.system(
111
            "%staosBenchmark -f tools/taosdemoAllTest/queryInsertdata.json" %
112 113
            binPath)
        os.system(
114
            "%staosBenchmark -f tools/taosdemoAllTest/queryTaosc.json" %
115
            binPath)
116 117 118
        os.system("cat query_res0.txt* > all_query_res0_taosc.txt")
        os.system("cat query_res1.txt* > all_query_res1_taosc.txt")
        os.system("cat query_res2.txt* > all_query_res2_taosc.txt")
119 120

        # correct Times testcases
121
        queryTimes0Taosc = self.queryTimesTaosc("all_query_res0_taosc.txt")
122
        self.assertCheck("all_query_res0_taosc.txt", queryTimes0Taosc, 6)
123 124

        queryTimes1Taosc = self.queryTimesTaosc("all_query_res1_taosc.txt")
125
        self.assertCheck("all_query_res1_taosc.txt", queryTimes1Taosc, 6)
126 127

        queryTimes2Taosc = self.queryTimesTaosc("all_query_res2_taosc.txt")
128 129
        self.assertCheck("all_query_res2_taosc.txt", queryTimes2Taosc, 20)

130
        # correct data testcase
131 132 133 134
        self.assertfileDataTaosc("all_query_res0_taosc.txt", "1604160000099")
        self.assertfileDataTaosc("all_query_res1_taosc.txt", "100")
        self.assertfileDataTaosc("all_query_res2_taosc.txt", "1604160000199")

135
        # delete useless files
136
        os.system("rm -rf ./query_res*")
137 138 139
        os.system("rm -rf ./all_query*")

        # use restful api to query
140
        os.system(
141
            "%staosBenchmark -f tools/taosdemoAllTest/queryInsertrestdata.json" %
142 143
            binPath)
        os.system(
144
            "%staosBenchmark -f tools/taosdemoAllTest/queryRestful.json" %
145
            binPath)
146 147 148
        os.system("cat query_res0.txt*  > all_query_res0_rest.txt")
        os.system("cat query_res1.txt*  > all_query_res1_rest.txt")
        os.system("cat query_res2.txt*  > all_query_res2_rest.txt")
149 150

        # correct Times testcases
151
        queryTimes0Restful = self.queryTimesRestful("all_query_res0_rest.txt")
152
        self.assertCheck("all_query_res0_rest.txt", queryTimes0Restful, 6)
153 154

        queryTimes1Restful = self.queryTimesRestful("all_query_res1_rest.txt")
155 156
        self.assertCheck("all_query_res1_rest.txt", queryTimes1Restful, 6)

157
        queryTimes2Restful = self.queryTimesRestful("all_query_res2_rest.txt")
158
        self.assertCheck("all_query_res2_rest.txt", queryTimes2Restful, 4)
159 160 161

        # correct data testcase
        data0 = self.getfileDataRestful("all_query_res0_rest.txt")
162 163 164 165
        self.assertCheck(
            'all_query_res0_rest.txt',
            data0,
            "2020-11-01 00:00:00.009")
166 167

        data1 = self.getfileDataRestful("all_query_res1_rest.txt")
168
        self.assertCheck('all_query_res1_rest.txt', data1, 10)
169

170
        data2 = self.getfileDataRestful("all_query_res2_rest.txt")
171 172 173 174 175
        self.assertCheck(
            'all_query_res2_rest.txt',
            data2,
            "2020-11-01 00:00:00.004")

176
        # query times less than or equal to 100
haoranc's avatar
haoranc 已提交
177
        assert os.system(
178
            "%staosBenchmark -f tools/taosdemoAllTest/queryInsertdata.json" %
haoranc's avatar
haoranc 已提交
179 180
            binPath) == 0
        assert os.system(
181
            "%staosBenchmark -f tools/taosdemoAllTest/querySpeciMutisql100.json" %
haoranc's avatar
haoranc 已提交
182 183
            binPath) != 0
        assert os.system(
184
            "%staosBenchmark -f tools/taosdemoAllTest/querySuperMutisql100.json" %
haoranc's avatar
haoranc 已提交
185
            binPath) == 0
186 187 188

        # query result print QPS
        os.system(
189
            "%staosBenchmark -f tools/taosdemoAllTest/queryInsertdata.json" %
190
            binPath)
191
        exceptcode = os.system(
192
            "%staosBenchmark -f tools/taosdemoAllTest/queryQps.json" %
193
            binPath)
194
        assert exceptcode == 0
195

196
        # use illegal or out of range parameters query json file
197
        os.system(
198
            "%staosBenchmark -f tools/taosdemoAllTest/queryInsertdata.json" %
199 200
            binPath)
        exceptcode = os.system(
201
            "%staosBenchmark -f tools/taosdemoAllTest/queryTimes0.json" %
202
            binPath)
203 204
        assert exceptcode != 0

205
        exceptcode0 = os.system(
206
            "%staosBenchmark -f tools/taosdemoAllTest/queryTimesless0.json" %
207
            binPath)
208 209
        assert exceptcode0 != 0

210
        exceptcode1 = os.system(
211
            "%staosBenchmark -f tools/taosdemoAllTest/queryConcurrentless0.json" %
212
            binPath)
213
        assert exceptcode1 != 0
214

215
        exceptcode2 = os.system(
216
            "%staosBenchmark -f tools/taosdemoAllTest/queryConcurrent0.json" %
217
            binPath)
218 219
        assert exceptcode2 != 0

220
        exceptcode3 = os.system(
221
            "%staosBenchmark -f tools/taosdemoAllTest/querrThreadsless0.json" %
222
            binPath)
223 224
        assert exceptcode3 != 0

225
        exceptcode4 = os.system(
226
            "%staosBenchmark -f tools/taosdemoAllTest/querrThreads0.json" %
227
            binPath)
228
        assert exceptcode4 != 0
229

230
        # delete useless files
231
        os.system("rm -rf ./insert_res.txt")
232 233 234
        os.system("rm -rf tools/taosdemoAllTest/*.py.sql")
        os.system("rm -rf ./querySystemInfo*")
        os.system("rm -rf ./query_res*")
235
        os.system("rm -rf ./all_query*")
236
        os.system("rm -rf ./test_query_res0.txt")
237

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

242

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