github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/bench.py (about) 1 #!/usr/bin/env python3 2 3 # Copyright 2023 CloudWeGo Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import tempfile 18 import os 19 import subprocess 20 import argparse 21 22 gbench_prefix = "SONIC_NO_ASYNC_GC=1 go test -benchmem -run=none " 23 mainbranch = "main" 24 25 def run(cmd): 26 print(cmd) 27 if os.system(cmd): 28 print ("Failed to run cmd: %s"%(cmd)) 29 exit(1) 30 31 def run_s(cmd): 32 print (cmd) 33 try: 34 res = os.popen(cmd) 35 except subprocess.CalledProcessError as e: 36 if e.returncode: 37 print (e.output) 38 exit(1) 39 return res.read() 40 41 def run_r(cmd): 42 print (cmd) 43 try: 44 cmds = cmd.split(' ') 45 data = subprocess.check_output(cmds, stderr=subprocess.STDOUT) 46 except subprocess.CalledProcessError as e: 47 if e.returncode: 48 print (e.output) 49 exit(1) 50 return data.decode("utf-8") 51 52 def compare(args): 53 # detech current branch. 54 # result = run_r("git branch") 55 current_branch = run_s("git status | head -n1 | sed 's/On branch //'") 56 # for br in result.split('\n'): 57 # if br.startswith("* "): 58 # current_branch = br.lstrip('* ') 59 # break 60 61 if not current_branch: 62 print ("Failed to detech current branch") 63 return None 64 65 # get the current diff 66 (fd, diff) = tempfile.mkstemp() 67 run("git diff > %s"%diff) 68 69 # early return if currrent is main branch. 70 print ("Current branch: %s"%(current_branch)) 71 if current_branch == mainbranch: 72 print ("Cannot compare at the main branch.Please build a new branch") 73 return None 74 75 # benchmark current branch 76 (fd, target) = tempfile.mkstemp(".target.txt") 77 run("%s %s ./... 2>&1 | tee %s" %(gbench_prefix, args, target)) 78 79 # trying to switch to the latest main branch 80 run("git checkout -- .") 81 if current_branch != mainbranch: 82 run("git checkout %s" %(mainbranch)) 83 run("git pull --allow-unrelated-histories origin %s" %(mainbranch)) 84 85 # benchmark main branch 86 (fd, main) = tempfile.mkstemp(".main.txt") 87 run("%s %s ./... 2>&1 | tee %s" %(gbench_prefix, args, main)) 88 89 # diff the result 90 # benchstat = "go get golang.org/x/perf/cmd/benchstat && go install golang.org/x/perf/cmd/benchstat" 91 run( "benchstat -sort=delta %s %s"%(main, target)) 92 run("git checkout -- .") 93 94 # restore branch 95 if current_branch != mainbranch: 96 run("git checkout %s"%(current_branch)) 97 run("patch -p1 < %s" % (diff)) 98 return target 99 100 def main(): 101 argparser = argparse.ArgumentParser(description='Tools to test the performance. Example: ./bench.py -b Decoder_Generic_Sonic -c') 102 argparser.add_argument('-b', '--bench', dest='filter', required=False, 103 help='Specify the filter for golang benchmark') 104 argparser.add_argument('-c', '--compare', dest='compare', action='store_true', required=False, 105 help='Compare with the main benchmarking') 106 argparser.add_argument('-t', '--times', dest='times', required=False, 107 help='benchmark the times') 108 argparser.add_argument('-r', '--repeat_times', dest='count', required=False, 109 help='benchmark the count') 110 args = argparser.parse_args() 111 112 if args.filter: 113 gbench_args = "-bench=%s"%(args.filter) 114 else: 115 gbench_args = "-bench=." 116 117 if args.times: 118 gbench_args += " -benchtime=%s"%(args.times) 119 120 if args.count: 121 gbench_args += " -count=%s"%(args.count) 122 else: 123 gbench_args += " -count=10" 124 125 if args.compare: 126 target = compare(gbench_args) 127 else: 128 target = None 129 130 if not target: 131 (fd, target) = tempfile.mkstemp(".target.txt") 132 run("%s %s ./... 2>&1 | tee %s" %(gbench_prefix, gbench_args, target)) 133 134 if __name__ == "__main__": 135 main()