99from datetime import datetime
1010import statistics
1111import resource
12+ import hashlib
13+
1214
1315num_cpus = str (max (1 , os .cpu_count () - 1 ))
1416changes_while_running = False
@@ -35,6 +37,12 @@ def regular_cmd(repo):
3537 "--max-target-bytes" , "200000" ,
3638 "--quiet" ]
3739
40+
41+ def hash_output (s ):
42+ s = s .strip ()
43+ return hashlib .sha256 (s .encode ("utf-8" )).hexdigest ()
44+
45+
3846# Not used ATM
3947def _python_cmd (repo ):
4048 return ["pipenv" , "run" , "opengrep" ,
@@ -66,7 +74,8 @@ def run(cmd, cwd=None):
6674 # my_env["PIPENV_PIPFILE"] = "../opengrep/cli/Pipfile"
6775 print (f"Running: { ' ' .join (cmd )} " )
6876 sys .stdout .flush ()
69- subprocess .run (cmd , cwd = cwd , check = True )
77+ result = subprocess .run (cmd , cwd = cwd , check = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
78+ return hash_output (result .stdout )
7079
7180def clone_specific_commit (repo_url , commit_hash , name ):
7281 if os .path .exists (name ):
@@ -110,21 +119,22 @@ def single_run(repo):
110119 #return t2 - t1
111120 time .sleep (5 )
112121 usage_start = resource .getrusage (resource .RUSAGE_CHILDREN )
113- run (regular_cmd (repo ))
122+ results = run (regular_cmd (repo ))
114123 usage_end = resource .getrusage (resource .RUSAGE_CHILDREN )
115124 time .sleep (5 )
116125 user_time = usage_end .ru_utime - usage_start .ru_utime
117126 system_time = usage_end .ru_stime - usage_start .ru_stime
118127 total_cpu_time = user_time + system_time
119- log_to_file (f"- run completed: { repo } . user: { show_num (user_time )} , system: { show_num (system_time )} , total: { show_num (total_cpu_time )} \n " )
120- return total_cpu_time
128+ log_to_file (f"- run completed: { repo } . user: { show_num (user_time )} , system: { show_num (system_time )} , total: { show_num (total_cpu_time )} , result_sha: { results } \n " )
129+ return ( total_cpu_time , results )
121130
122131def run_opengrep (repo ):
123132 durs = [single_run (repo ) for x in range (0 , repeat_each_test_n_times )]
124133 #if repeat_each_test_n_times >= 5:
125134 # durs.remove(max(durs))
126135 # durs.remove(max(durs))
127- return statistics .mean (durs )
136+ hash = set ([y for (x ,y ) in durs ])
137+ return (statistics .mean ([x for (x , _ ) in durs ]),hash )
128138
129139def has_changes ():
130140 result = subprocess .run (
@@ -146,11 +156,11 @@ def make_opengrep():
146156 run (["make" ], cwd = "../.." )
147157
148158def run_bench ():
149- return [{"name" : r ["name" ], "duration" : run_opengrep ( "repos/" + r [ "name" ]) }
150- for r in repos ]
159+ return [{"name" : r ["name" ], "duration" : duration , "sha" : sha }
160+ for r in repos for ( duration , sha ) in [ run_opengrep ( "repos/" + r [ "name" ])] ]
151161
152162def combine_results (res1 , res2 ):
153- return [{"name" : r1 ["name" ], "d1" : r1 ["duration" ], "d2" : r2 ["duration" ]}
163+ return [{"name" : r1 ["name" ], "d1" : r1 ["duration" ], "d2" : r2 ["duration" ], "same-results" : r1 [ "sha" ] == r2 [ "sha" ] }
154164 for (r1 , r2 ) in zip (res1 , res2 )]
155165
156166def report_results (sha1 , sha2 , res1 , res2 ):
@@ -159,7 +169,8 @@ def report_results(sha1, sha2, res1, res2):
159169 sha1 : f"{ r ["d1" ]:.2f} " ,
160170 sha2 : f"{ r ["d2" ]:.2f} " ,
161171 "diff(s)" : f"{ (r ["d2" ] - r ["d1" ]):.2f} " ,
162- "diff(%)" : f"{ (100 * (r ["d2" ] - r ["d1" ]) / r ["d1" ]):.2f} " }
172+ "diff(%)" : f"{ (100 * (r ["d2" ] - r ["d1" ]) / r ["d1" ]):.2f} " ,
173+ "same-results" : r ["same-results" ]}
163174 for r in combined ]
164175 # print to screen
165176 print ("--------- BENCHMARK RUN COMPLETED ---------\n " )
0 commit comments