22import random
33import json
44from time import perf_counter
5+ import argparse
56
67def synthesis (text :str ,address = "127.0.0.1" ,port = 50021 ,speaker = 0 ,pitch = 0.0 ,speed = 1.0 ):
78 """音声生成
@@ -14,7 +15,7 @@ def synthesis(text:str,address="127.0.0.1",port=50021,speaker=0,pitch=0.0,speed=
1415 speed : スピード
1516
1617 Returns:
17-
18+ 生成時間(秒), レイテンシ(秒)
1819 """
1920 address = "http://" + address
2021 query_payload = {"text" : text ,"speaker" :speaker }
@@ -26,11 +27,15 @@ def synthesis(text:str,address="127.0.0.1",port=50021,speaker=0,pitch=0.0,speed=
2627 query_data ["speedScale" ] = speed
2728 query_data ["pitchScale" ] = pitch
2829 before = perf_counter ()
30+ requests .get (f"{ address } :{ port } /version" )
31+ after = perf_counter ()
32+ latency = after - before
33+ before = perf_counter ()
2934 resp = requests .post (f"{ address } :{ port } /synthesis" ,params = synth_payload ,data = json .dumps (query_data ))
3035 if not resp .status_code == 200 :
3136 raise ConnectionError ("Status code: %d" % resp .status_code )
3237 after = perf_counter ()
33- return after - before
38+ return after - before - latency , latency
3439
3540def gen_text (count :int ):
3641 return "" .join ([chr (random .randint (ord ("あ" ),ord ("ん" ))) for i in range (count )])
@@ -51,15 +56,38 @@ def bench(length:int,count=10,address="127.0.0.1",port=50021):
5156 tmp = 0
5257 for i in range (count ):
5358 text = gen_text (length )
54- elapsed_time = synthesis (text ,address = address ,port = port )
59+ elapsed_time , latency = synthesis (text ,address = address ,port = port )
5560 tmp += elapsed_time
56- print (i + 1 ,elapsed_time )
61+ print (i + 1 ,elapsed_time , latency )
5762 result = round (tmp / count ,4 )
5863 return result
5964
60- score_10 = bench (length = 10 ,address = "127.0.0.1" )
61- score_50 = bench (length = 50 ,address = "127.0.0.1" )
62- score_100 = bench (length = 100 ,address = "127.0.0.1" )
63- print ("10:" ,score_10 )
64- print ("50:" ,score_50 )
65- print ("100:" ,score_100 )
65+ if __name__ == "__main__" :
66+ parser = argparse .ArgumentParser ()
67+ parser .add_argument ("-s" ,help = "VOICEVOX API Server Address" ,default = "127.0.0.1" )
68+ parser .add_argument ("-p" ,help = "VOICEVOX API Server Port" ,default = 50021 )
69+ args = parser .parse_args ()
70+ score_10 = bench (length = 10 ,address = args .s ,port = args .p )
71+ score_50 = bench (length = 50 ,address = args .s ,port = args .p )
72+ score_100 = bench (length = 100 ,address = args .s ,port = args .p )
73+ score_avg = round ((score_10 + score_50 + score_100 ) / 3 ,4 )
74+ resp = requests .get (f"http://{ args .s } :{ args .p } /version" )
75+ info_engine = resp .text .replace ("\" " ,"" )
76+ resp = requests .get (f"http://{ args .s } :{ args .p } /supported_devices" )
77+ info_devices = resp .json ()
78+ if info_devices ["cuda" ]:
79+ info_device = "CUDA"
80+ elif info_devices ["dml" ]:
81+ info_device = "DirectML"
82+ else :
83+ info_device = "CPU"
84+
85+ print ("=========== Info ===========" )
86+ print (" Engine:" ,info_engine )
87+ print (" Device:" ,info_device )
88+ print ("========== Result ==========" )
89+ print (" 10: " ,score_10 )
90+ print (" 50: " ,score_50 )
91+ print (" 100:" ,score_100 )
92+ print (" Avg:" ,score_avg )
93+ print ("============================" )
0 commit comments