github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/misc/benchcmp (about)

     1  #!/bin/sh
     2  # Copyright 2011 The Go Authors.  All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  case "$1" in
     7  -*)	
     8  	echo 'usage: benchcmp old.txt new.txt' >&2
     9  	echo >&2
    10  	echo 'Each input file should be from:' >&2
    11  	echo '	go test -test.run=NONE -test.bench=. > [old,new].txt' >&2
    12  	echo >&2
    13  	echo 'Benchcmp compares the first and last for each benchmark.' >&2
    14  	echo >&2
    15  	echo 'If -test.benchmem=true is added to the "go test" command' >&2
    16  	echo 'benchcmp will also compare memory allocations.' >&2
    17  	exit 2
    18  esac
    19  
    20  awk '
    21  BEGIN {
    22  	n = 0
    23  }
    24  
    25  $1 ~ /Benchmark/ && $4 == "ns/op" {
    26  	if(old[$1]) {
    27  		if(!saw[$1]++) {
    28  			name[n++] = $1
    29  			if(length($1) > len)
    30  				len = length($1)
    31  		}
    32  		new[$1] = $3
    33  		if($6 == "MB/s")
    34  			newmb[$1] = $5
    35  
    36  		# allocs/op might be at $8 or $10 depending on if
    37  		# SetBytes was used or not.
    38  		# B/op might be at $6 or $8, it should be immediately
    39  		# followed by allocs/op
    40  		if($8 == "allocs/op") {
    41  			newbytes[$1] = $5
    42  			newalloc[$1] = $7
    43  		}
    44  		if($10 == "allocs/op") {
    45  			newbytes[$1] = $7
    46  			newalloc[$1] = $9
    47  		}
    48  	} else {
    49  		old[$1] = $3
    50  		if($6 == "MB/s")
    51  			oldmb[$1] = $5
    52  		if($8 == "allocs/op") {
    53  			oldbytes[$1] = $5
    54  			oldalloc[$1] = $7
    55  		}
    56  		if($10 == "allocs/op") {
    57  			oldbytes[$1] = $7
    58  			oldalloc[$1] = $9
    59  		}
    60  	}
    61  }
    62  
    63  END {
    64  	if(n == 0) {
    65  		print "benchcmp: no repeated benchmarks" >"/dev/stderr"
    66  		exit 1
    67  	}
    68  
    69  	printf("%-*s %12s %12s  %7s\n", len, "benchmark", "old ns/op", "new ns/op", "delta")
    70  
    71  	# print ns/op
    72  	for(i=0; i<n; i++) {
    73  		what = name[i]
    74  		printf("%-*s %12d %12d  %6s%%\n", len, what, old[what], new[what],
    75  			sprintf("%+.2f", 100*new[what]/old[what]-100))
    76  	}
    77  
    78  	# print mb/s
    79  	anymb = 0
    80  	for(i=0; i<n; i++) {
    81  		what = name[i]
    82  		if(!(what in newmb))
    83  			continue
    84  		if(anymb++ == 0)
    85  			printf("\n%-*s %12s %12s  %7s\n", len, "benchmark", "old MB/s", "new MB/s", "speedup")
    86  		printf("%-*s %12s %12s  %6sx\n", len, what,
    87  			sprintf("%.2f", oldmb[what]),
    88  			sprintf("%.2f", newmb[what]),
    89  			sprintf("%.2f", newmb[what]/oldmb[what]))
    90  	}
    91  
    92  	# print allocs
    93  	anyalloc = 0
    94  	for(i=0; i<n; i++) {
    95  		what = name[i]
    96  		if(!(what in newalloc))
    97  			continue
    98  		if(anyalloc++ == 0)
    99  			printf("\n%-*s %12s %12s  %7s\n", len, "benchmark", "old allocs", "new allocs", "delta")
   100  		if(oldalloc[what] == 0)
   101  			delta="n/a"
   102  		else
   103  			delta=sprintf("%.2f", 100*newalloc[what]/oldalloc[what]-100)
   104  		printf("%-*s %12d %12d  %6s%%\n", len, what,
   105  			oldalloc[what], newalloc[what], delta)
   106  	}
   107  
   108  	# print alloc bytes
   109  	anybytes = 0
   110  	for(i=0; i<n; i++) {
   111  		what = name[i]
   112  		if(!(what in newbytes))
   113  			continue
   114  		if(anybytes++ == 0)
   115  			printf("\n%-*s %12s %12s  %7s\n", len, "benchmark", "old bytes", "new bytes", "delta")
   116  		if(oldbytes[what] == 0)
   117  			delta="n/a"
   118  		else
   119  			delta=sprintf("%.2f", 100*newbytes[what]/oldbytes[what]-100)
   120  		printf("%-*s %12d %12d  %6s%%\n", len, what,
   121  			oldbytes[what], newbytes[what], delta)
   122  	}
   123  }
   124  ' "$@"