提交 e66f2dbd 编写于 作者: sangshuduo's avatar sangshuduo

[TD-2771] <feature>: python version taosdemo. add native interface

上级 4b3050d7
...@@ -32,6 +32,12 @@ def v_print(msg: str, arg: str): ...@@ -32,6 +32,12 @@ def v_print(msg: str, arg: str):
print(msg % arg) print(msg % arg)
@dispatch(str, str, str, str, str)
def v_print(msg: str, arg1: str, arg2: str, arg3: str, arg4: str):
if verbose:
print(msg % (arg1, arg2, arg3, arg4))
@dispatch(str, int) @dispatch(str, int)
def v_print(msg: str, arg: int): def v_print(msg: str, arg: int):
if verbose: if verbose:
...@@ -100,10 +106,16 @@ def query_func(process: int, thread: int, cmd: str): ...@@ -100,10 +106,16 @@ def query_func(process: int, thread: int, cmd: str):
if oneMoreHost != "NotSupported" and random.randint( if oneMoreHost != "NotSupported" and random.randint(
0, 1) == 1: 0, 1) == 1:
v_print("%s", "Send to second host") v_print("%s", "Send to second host")
if native:
cursor2.execute(cmd)
else:
restful_execute( restful_execute(
oneMoreHost, port, user, password, cmd) oneMoreHost, port, user, password, cmd)
else: else:
v_print("%s", "Send to first host") v_print("%s", "Send to first host")
if native:
cursor.execute(cmd)
else:
restful_execute( restful_execute(
host, port, user, password, cmd) host, port, user, password, cmd)
...@@ -171,25 +183,56 @@ def insert_data(processes: int): ...@@ -171,25 +183,56 @@ def insert_data(processes: int):
def create_stb(): def create_stb():
for i in range(0, numOfStb): for i in range(0, numOfStb):
if native:
cursor.execute(
"CREATE TABLE IF NOT EXISTS %s%d (ts timestamp, value float) TAGS (uuid binary(50))" %
(stbName, i))
else:
restful_execute( restful_execute(
host, host,
port, port,
user, user,
password, password,
"CREATE TABLE IF NOT EXISTS st%d (ts timestamp, value float) TAGS (uuid binary(50))" % "CREATE TABLE IF NOT EXISTS %s%d (ts timestamp, value float) TAGS (uuid binary(50))" %
i) (stbName, i)
)
def use_database():
current_db = "%s%d" % (dbName, (numOfDb - 1))
if native:
cursor.execute("USE %s" % current_db)
else:
restful_execute(host, port, user, password, "USE %s" % current_db)
def create_databases(): def create_databases():
for i in range(0, numOfDb): for i in range(0, numOfDb):
v_print("will create database db%d", int(i)) v_print("will create database db%d", int(i))
if native:
cursor.execute(
"CREATE DATABASE IF NOT EXISTS %s%d" % (dbName, i))
else:
restful_execute( restful_execute(
host, host,
port, port,
user, user,
password, password,
"CREATE DATABASE IF NOT EXISTS db%d" % "CREATE DATABASE IF NOT EXISTS %s%d" % (dbName, i))
i)
def drop_tables():
# TODO
v_print("TODO: drop tables total %d", numOfTb)
pass
def drop_stable():
# TODO
v_print("TODO: drop stables total %d", numOfStb)
pass
def drop_databases(): def drop_databases():
...@@ -198,13 +241,19 @@ def drop_databases(): ...@@ -198,13 +241,19 @@ def drop_databases():
# drop exist databases first # drop exist databases first
for i in range(0, numOfDb): for i in range(0, numOfDb):
v_print("will drop database db%d", int(i)) v_print("will drop database db%d", int(i))
if native:
cursor.execute(
"DROP DATABASE IF EXISTS %s%d" %
(dbName, i))
else:
restful_execute( restful_execute(
host, host,
port, port,
user, user,
password, password,
"DROP DATABASE IF EXISTS db%d" % "DROP DATABASE IF EXISTS %s%d" %
i) (dbName, i))
def insert_func(process: int, thread: int): def insert_func(process: int, thread: int):
...@@ -252,6 +301,9 @@ def insert_func(process: int, thread: int): ...@@ -252,6 +301,9 @@ def insert_func(process: int, thread: int):
if measure: if measure:
exec_start_time = datetime.datetime.now() exec_start_time = datetime.datetime.now()
if native:
cursor.execute(cmd)
else:
restful_execute( restful_execute(
host, port, user, password, cmd) host, port, user, password, cmd)
...@@ -274,15 +326,26 @@ def create_tb_using_stb(): ...@@ -274,15 +326,26 @@ def create_tb_using_stb():
def create_tb(): def create_tb():
v_print("create_tb() numOfTb: %d", numOfTb) v_print("create_tb() numOfTb: %d", numOfTb)
for i in range(0, numOfDb): for i in range(0, numOfDb):
restful_execute(host, port, user, password, "USE db%d" % i) if native:
cursor.execute("USE %s%d" % (dbName, i))
else:
restful_execute(
host, port, user, password, "USE %s%d" %
(dbName, i))
for j in range(0, numOfTb): for j in range(0, numOfTb):
if native:
cursor.execute(
"CREATE TABLE %s%d (ts timestamp, value float)" %
(tbName, j))
else:
restful_execute( restful_execute(
host, host,
port, port,
user, user,
password, password,
"CREATE TABLE tb%d (ts timestamp, value float)" % "CREATE TABLE %s%d (ts timestamp, value float)" %
j) (tbName, j))
def insert_data_process(i: int, begin: int, end: int): def insert_data_process(i: int, begin: int, end: int):
...@@ -312,38 +375,42 @@ def insert_data_process(i: int, begin: int, end: int): ...@@ -312,38 +375,42 @@ def insert_data_process(i: int, begin: int, end: int):
end)] end)]
wait(workers, return_when=ALL_COMPLETED) wait(workers, return_when=ALL_COMPLETED)
def printConfig(): def printConfig():
print("###################################################################"); print("###################################################################")
print("# Use native interface: %s" % native); print("# Use native interface: %s" % native)
print("# Server IP: %s" % host); print("# Server IP: %s" % host)
if native: if native:
print("# Server port: %s" % port); print("# Server port: %s" % port)
else: else:
print("# Server port: %s" % restPort); print("# Server port: %s" % restPort)
print("# User: %s" % user); print("# Configuration Dir: %s" % configDir)
print("# Password: %s" % password); print("# User: %s" % user)
print("# Number of Columns per record: %s" % colsPerRecord); print("# Password: %s" % password)
print("# Number of Threads: %s" % threads); print("# Number of Columns per record: %s" % colsPerRecord)
print("# Number of Processes: %s" % processes); print("# Number of Threads: %s" % threads)
print("# Number of Tables: %s" % numOfTb); print("# Number of Processes: %s" % processes)
print("# Number of records per Table: %s" % numOfRec); print("# Number of Tables: %s" % numOfTb)
print("# Records/Request: %s" % batch); print("# Number of records per Table: %s" % numOfRec)
print("# Database name: %s" % dbName); print("# Records/Request: %s" % batch)
print("# Replica: %s" % replica); print("# Database name: %s" % dbName)
print("# Use STable: %s" % useStable); print("# Replica: %s" % replica)
print("# Table prefix: %s" % tbNamePrefix); print("# Use STable: %s" % useStable)
print("# Table prefix: %s" % tbNamePrefix)
if useStable: if useStable:
print("# STable prefix: %s" % stbNamePrefix); print("# STable prefix: %s" % stbNamePrefix)
print("# Data order: %s" % outOfOrder);
print("# Data out of order rate: %s" % rateOOOO); print("# Data order: %s" % outOfOrder)
print("# Delete method: %s" % deleteMethod); print("# Data out of order rate: %s" % rateOOOO)
print("# Query command: %s" % queryCmd); print("# Delete method: %s" % deleteMethod)
print("# Insert Only: %s" % insertOnly); print("# Query command: %s" % queryCmd)
print("# Verbose output %s" % verbose); print("# Insert Only: %s" % insertOnly)
print("# Test time: %s" % datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")) print("# Verbose output %s" % verbose)
print("###################################################################"); print("# Test time: %s" %
datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
print("###################################################################")
if __name__ == "__main__": if __name__ == "__main__":
...@@ -388,7 +455,7 @@ if __name__ == "__main__": ...@@ -388,7 +455,7 @@ if __name__ == "__main__":
'native', 'host', 'port', 'user', 'password', 'dbname', 'replica', 'tbname', 'native', 'host', 'port', 'user', 'password', 'dbname', 'replica', 'tbname',
'stable', 'stbname', 'query', 'numOfThreads', 'numOfProcesses', 'stable', 'stbname', 'query', 'numOfThreads', 'numOfProcesses',
'recPerReq', 'colsPerRecord', 'numOfTb', 'numOfRec', 'config', 'recPerReq', 'colsPerRecord', 'numOfTb', 'numOfRec', 'config',
'insertOnly', 'outOfOrder', 'rateOOOO','deleteMethod', 'insertOnly', 'outOfOrder', 'rateOOOO', 'deleteMethod',
'verbose', 'debug', 'skipPrompt', 'help' 'verbose', 'debug', 'skipPrompt', 'help'
]) ])
except getopt.GetoptError as err: except getopt.GetoptError as err:
...@@ -418,16 +485,23 @@ if __name__ == "__main__": ...@@ -418,16 +485,23 @@ if __name__ == "__main__":
print('\t-u, --user <username> user, The user name to use when connecting to the server. Default is \'root\'.') print('\t-u, --user <username> user, The user name to use when connecting to the server. Default is \'root\'.')
print('\t-P, --password <password> password, The password to use when connecting to the server. Default is \'taosdata\'.') print('\t-P, --password <password> password, The password to use when connecting to the server. Default is \'taosdata\'.')
print('\t-l, --colsPerRec <number> num_of_columns_per_record, The number of columns per record. Default is 3.') print('\t-l, --colsPerRec <number> num_of_columns_per_record, The number of columns per record. Default is 3.')
print('\t-d, --dbname <dbname> database, Destination database. Default is \'test\'.') print(
'\t-d, --dbname <dbname> database, Destination database. Default is \'test\'.')
print('\t-a, --replica <replications> replica, Set the replica parameters of the database, Default 1, min: 1, max: 5.') print('\t-a, --replica <replications> replica, Set the replica parameters of the database, Default 1, min: 1, max: 5.')
print('\t-m, --tbname <table prefix> table_prefix, Table prefix name. Default is \'t\'.') print(
print('\t-M, --stable flag, Use super table. Default is no') '\t-m, --tbname <table prefix> table_prefix, Table prefix name. Default is \'t\'.')
print('\t-s, --stbname <stable prefix> stable_prefix, STable prefix name. Default is \'st\'') print(
'\t-M, --stable flag, Use super table. Default is no')
print(
'\t-s, --stbname <stable prefix> stable_prefix, STable prefix name. Default is \'st\'')
print('\t-Q, --query <DEFAULT | command> query, Execute query command. set \'DEFAULT\' means select * from each table') print('\t-Q, --query <DEFAULT | command> query, Execute query command. set \'DEFAULT\' means select * from each table')
print('\t-T, --numOfThreads <number> num_of_threads, The number of threads. Default is 1.') print(
print('\t-P, --numOfProcesses <number> num_of_processes, The number of threads. Default is 1.') '\t-T, --numOfThreads <number> num_of_threads, The number of threads. Default is 1.')
print(
'\t-P, --numOfProcesses <number> num_of_processes, The number of threads. Default is 1.')
print('\t-r, --batch <number> num_of_records_per_req, The number of records per request. Default is 1000.') print('\t-r, --batch <number> num_of_records_per_req, The number of records per request. Default is 1000.')
print('\t-t, --numOfTb <number> num_of_tables, The number of tables. Default is 1.') print(
'\t-t, --numOfTb <number> num_of_tables, The number of tables. Default is 1.')
print('\t-n, --numOfRec <number> num_of_records_per_table, The number of records per table. Default is 1.') print('\t-n, --numOfRec <number> num_of_records_per_table, The number of records per table. Default is 1.')
print('\t-c, --config <path> config_directory, Configuration directory. Default is \'/etc/taos/\'.') print('\t-c, --config <path> config_directory, Configuration directory. Default is \'/etc/taos/\'.')
print('\t-x, --inserOnly flag, Insert only flag.') print('\t-x, --inserOnly flag, Insert only flag.')
...@@ -436,7 +510,8 @@ if __name__ == "__main__": ...@@ -436,7 +510,8 @@ if __name__ == "__main__":
print('\t-D, --deleteMethod <number> Delete data methods 0: don\'t delete, 1: delete by table, 2: delete by stable, 3: delete by database.') print('\t-D, --deleteMethod <number> Delete data methods 0: don\'t delete, 1: delete by table, 2: delete by stable, 3: delete by database.')
print('\t-v, --verbose Print verbose output') print('\t-v, --verbose Print verbose output')
print('\t-g, --debug Print debug output') print('\t-g, --debug Print debug output')
print('\t-y, --skipPrompt Skip read key for continous test, default is not skip') print(
'\t-y, --skipPrompt Skip read key for continous test, default is not skip')
print('') print('')
sys.exit(0) sys.exit(0)
...@@ -510,7 +585,11 @@ if __name__ == "__main__": ...@@ -510,7 +585,11 @@ if __name__ == "__main__":
numOfRec = int(value) numOfRec = int(value)
v_print("numOfRec is %d", numOfRec) v_print("numOfRec is %d", numOfRec)
if key in ['-x', '--insertonLy']: if key in ['-c', '--config']:
configDir = value
v_print("config dir: %s", configDir)
if key in ['-x', '--insertOnly']:
insertOnly = True insertOnly = True
v_print("insert only: %d", insertOnly) v_print("insert only: %d", insertOnly)
...@@ -524,6 +603,11 @@ if __name__ == "__main__": ...@@ -524,6 +603,11 @@ if __name__ == "__main__":
if key in ['-D', '--deleteMethod']: if key in ['-D', '--deleteMethod']:
deleteMethod = int(value) deleteMethod = int(value)
if (deleteMethod < 0) or (deleteMethod > 3):
print(
"inputed delete method is %d, valid value is 0~3, set to default 0" %
deleteMethod)
deleteMethod = 0
v_print("the delete method is %d", deleteMethod) v_print("the delete method is %d", deleteMethod)
if key in ['-v', '--verbose']: if key in ['-v', '--verbose']:
...@@ -538,10 +622,39 @@ if __name__ == "__main__": ...@@ -538,10 +622,39 @@ if __name__ == "__main__":
if verbose: if verbose:
printConfig() printConfig()
if skipPrompt == False: if not skipPrompt:
input("Press any key to continue..") input("Press any key to continue..")
if dropDbOnly: if native:
v_print("host:%s, user:%s passwd:%s configDir:%s ", host, user, password, configDir)
try:
conn = taos.connect(
host=host,
user=user,
password=password,
config=configDir)
print("conn: %p" % conn)
except Exception as e:
print("Error: %s" % e.args[0])
sys.exit(1)
if native:
try:
cursor = conn.cursor()
except Exception as e:
print("Error: %s" % e.args[0])
sys.exit(1)
if deleteMethod > 0:
if deleteMethod == 1:
drop_tables()
print("Drop tables done.")
elif deleteMethod == 2:
drop_stables()
print("Drop super tables done.")
elif deleteMethod == 3:
drop_databases() drop_databases()
print("Drop Database done.") print("Drop Database done.")
sys.exit(0) sys.exit(0)
...@@ -549,14 +662,14 @@ if __name__ == "__main__": ...@@ -549,14 +662,14 @@ if __name__ == "__main__":
# create databases # create databases
if (insertOnly == False): if (insertOnly == False):
drop_databases() drop_databases()
create_databases() create_databases()
if measure: if measure:
start_time = time.time() start_time = time.time()
# use last database # use last database
current_db = "db%d" % (numOfDb - 1) use_database()
restful_execute(host, port, user, password, "USE %s" % current_db)
if numOfStb > 0: if numOfStb > 0:
create_stb() create_stb()
...@@ -568,8 +681,14 @@ if __name__ == "__main__": ...@@ -568,8 +681,14 @@ if __name__ == "__main__":
if verbose: if verbose:
for i in range(0, numOfDb): for i in range(0, numOfDb):
for j in range(0, numOfStb): for j in range(0, numOfStb):
restful_execute(host, port, user, password, if native:
"SELECT COUNT(*) FROM db%d.st%d" % (i, j,)) cursor.execute(
"SELECT COUNT(*) FROM %s%d.%s%d" %
(dbName, i, stbName, j,))
else:
restful_execute(
host, port, user, password, "SELECT COUNT(*) FROM %s%d.%s%d" %
(dbName, i, stbName, j,))
print("done") print("done")
...@@ -587,10 +706,21 @@ if __name__ == "__main__": ...@@ -587,10 +706,21 @@ if __name__ == "__main__":
if verbose: if verbose:
for i in range(0, numOfDb): for i in range(0, numOfDb):
restful_execute(host, port, user, password, "USE db%d" % i) if native:
cursor.execute("USE %s%d" % (dbName, i))
else:
restful_execute(
host, port, user, password, "USE %s%d" %
(dbName, i))
for j in range(0, numOfTb): for j in range(0, numOfTb):
restful_execute(host, port, user, password, if native:
"SELECT COUNT(*) FROM tb%d" % (j,)) cursor.execute(
"SELECT COUNT(*) FROM %s%d" % (tbName, j))
else:
restful_execute(
host, port, user, password, "SELECT COUNT(*) FROM %s%d" %
(tbName, j))
if queryCmd != "": if queryCmd != "":
print("queryCmd: %s" % queryCmd) print("queryCmd: %s" % queryCmd)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册