11import csv
22import logging
3+ import re
34import shlex
5+ import subprocess
46
7+ from redisbench_admin .utils .remote import executeRemoteCommands
58
6- def redis_benchmark_from_stdout_csv_to_json (stdout , start_time , start_time_str ):
9+
10+ def redis_benchmark_from_stdout_csv_to_json (csv_data , start_time , start_time_str , overloadTestName = None ):
711 results_dict = {"Tests" : {}, "StartTime" : int (start_time .strftime ("%s" )),
812 "StartTimeHuman" : start_time_str }
9- csv_data = list (csv .reader (stdout . decode ( 'ascii' ) .splitlines (), delimiter = "," ))
13+ csv_data = list (csv .reader (csv_data .splitlines (), delimiter = "," ))
1014 header = csv_data [0 ]
1115 for row in csv_data [1 :]:
1216 test_name = row [0 ]
17+ if overloadTestName is not None :
18+ test_name = overloadTestName
1319 results_dict ["Tests" ][test_name ] = {}
1420 for pos , value in enumerate (row [1 :]):
1521 results_dict ["Tests" ][test_name ][header [pos + 1 ]] = value
@@ -21,38 +27,89 @@ def prepareRedisBenchmarkCommand(
2127 server_private_ip : object ,
2228 server_plaintext_port : object ,
2329 benchmark_config : object ,
24- ) -> str :
30+ ):
2531 """
2632 Prepares redis-benchmark command parameters
2733 :param server_private_ip:
2834 :param server_plaintext_port:
2935 :param benchmark_config:
30- :return: string containing the required command to run the benchmark given the configurations
36+ :return: [ string] containing the required command to run the benchmark given the configurations
3137 """
32- queries_str = [executable_path ]
33- queries_str .extend (["-h" , "{}" .format (server_private_ip )])
34- queries_str .extend (["-p" , "{}" .format (server_plaintext_port )])
38+ command_str = ""
39+ command_arr = [executable_path ]
40+ command_arr .extend (["-h" , "{}" .format (server_private_ip )])
41+ command_arr .extend (["-p" , "{}" .format (server_plaintext_port )])
3542
3643 # we need the csv output
37- queries_str .extend (["--csv" , "-e" ])
44+ command_arr .extend (["--csv" , "-e" ])
3845 last_append = None
3946 for k in benchmark_config ["parameters" ]:
4047 if "clients" in k :
41- queries_str .extend (["-c" , "{}" .format (k ["clients" ])])
48+ command_arr .extend (["-c" , "{}" .format (k ["clients" ])])
4249 if "requests" in k :
43- queries_str .extend (["-n" , "{}" .format (k ["requests" ])])
50+ command_arr .extend (["-n" , "{}" .format (k ["requests" ])])
4451 if "threads" in k :
45- queries_str .extend (["--threads" , "{}" .format (k ["threads" ])])
52+ command_arr .extend (["--threads" , "{}" .format (k ["threads" ])])
4653 if "pipeline" in k :
47- queries_str .extend (["-P" , "{}" .format (k ["pipeline" ])])
54+ command_arr .extend (["-P" , "{}" .format (k ["pipeline" ])])
4855 # if we have the command keywork then it needs to be at the end of args
4956 if "command" in k :
57+ last_str = k ["command" ]
5058 last_append = shlex .split (k ["command" ])
59+ command_str = " " .join (command_arr )
5160 if last_append is not None :
52- queries_str .extend (last_append )
61+ command_arr .extend (last_append )
62+ command_str = command_str + " " + last_str
5363 logging .info (
54- "Running the benchmark with the following parameters: {}" .format (
55- " " . join ( queries_str )
64+ "Running the benchmark with the following parameters:\n \t Args array: {} \n \t Args str: {}" .format (
65+ command_arr , command_str
5666 )
5767 )
58- return queries_str
68+ return command_arr , command_str
69+
70+
71+ def ensure_redis_benchmark_version_from_input (benchmark_min_tool_version , benchmark_min_tool_version_major ,
72+ benchmark_min_tool_version_minor , benchmark_min_tool_version_patch ,
73+ benchmark_tool , stdout ):
74+ version_output = stdout .decode ('ascii' ).split ("\n " )[0 ]
75+ logging .info (
76+ "Detected benchmark config tool {} with version {}" .format (benchmark_tool , version_output ))
77+ p = re .compile ("redis-benchmark (\d+)\.(\d+)\.(\d+) " )
78+ m = p .match (version_output )
79+ if m is None :
80+ raise Exception (
81+ "Unable to detect benchmark tool version, and the benchmark requires a min version: {}" .format (
82+ benchmark_min_tool_version ))
83+ major = m .group (1 )
84+ minor = m .group (2 )
85+ patch = m .group (3 )
86+ if major < benchmark_min_tool_version_major or (
87+ major == benchmark_min_tool_version_major and minor < benchmark_min_tool_version_minor ) or (
88+ major == benchmark_min_tool_version_major and minor == benchmark_min_tool_version_minor and patch < benchmark_min_tool_version_patch ):
89+ raise Exception (
90+ "Detected benchmark version that is inferior than the minimum required. {} < {}" .format (
91+ version_output , benchmark_min_tool_version ))
92+
93+
94+ def redis_benchmark_ensure_min_version_local (benchmark_tool , benchmark_min_tool_version ,
95+ benchmark_min_tool_version_major ,
96+ benchmark_min_tool_version_minor , benchmark_min_tool_version_patch ):
97+ benchmark_client_process = subprocess .Popen (args = [benchmark_tool , "--version" ],
98+ stdout = subprocess .PIPE ,
99+ stderr = subprocess .STDOUT )
100+ (stdout , sterr ) = benchmark_client_process .communicate ()
101+ ensure_redis_benchmark_version_from_input (benchmark_min_tool_version , benchmark_min_tool_version_major ,
102+ benchmark_min_tool_version_minor , benchmark_min_tool_version_patch ,
103+ benchmark_tool , stdout )
104+
105+
106+ def redis_benchmark_ensure_min_version_remote (benchmark_tool , benchmark_min_tool_version ,
107+ benchmark_min_tool_version_major ,
108+ benchmark_min_tool_version_minor , benchmark_min_tool_version_patch ):
109+ benchmark_client_process = subprocess .Popen (args = [benchmark_tool , "--version" ],
110+ stdout = subprocess .PIPE ,
111+ stderr = subprocess .STDOUT )
112+ (stdout , sterr ) = benchmark_client_process .communicate ()
113+ ensure_redis_benchmark_version_from_input (benchmark_min_tool_version , benchmark_min_tool_version_major ,
114+ benchmark_min_tool_version_minor , benchmark_min_tool_version_patch ,
115+ benchmark_tool , stdout )
0 commit comments