Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | #!/usr/bin/env ruby # coding: utf-8 require 'getoptlong' require 'pathname' $benchmarks_all = [ # Single-threaded benchmarks. "churn", "list_allocate", "tree_allocate", "tree_churn", "fragment", "fragment_iterate", "medium", "big", # Benchmarks based on browser recordings. "facebook", "reddit", "flickr", "theverge", "nimlang", # Multi-threaded benchmark variants. "message_one", "message_many", "churn --parallel", "list_allocate --parallel", "tree_allocate --parallel", "tree_churn --parallel", "fragment --parallel", "fragment_iterate --parallel", # These tests often crash TCMalloc: <rdar://problem/13657137>. "medium --parallel", "big --parallel", # Enable these tests to test memory footprint. The way they run is not # really compatible with throughput testing. # "reddit_memory_warning --runs 0", # "flickr_memory_warning --runs 0", # "theverge_memory_warning --runs 0", # Enable this test to test shrinking back down from a large heap while a process remains active. # The way it runs is not really compatible with throughput testing. # "balloon" "facebook --parallel", "reddit --parallel", "flickr --parallel", "theverge --parallel", # "nimlang --use-thread-id", ] $benchmarks_memory = [ "facebook", "reddit", "flickr", "theverge", "nimlang" ] $benchmarks_memory_warning = [ "reddit_memory_warning --runs 0", "flickr_memory_warning --runs 0", "theverge_memory_warning --runs 0", ] $benchmarks = $benchmarks_all $heap = 0 def usage puts "run-malloc-benchmarks [options] /path/to/MallocBench Name:/path/to/libmbmalloc.dylib [ Name:/path/to/libmbmalloc.dylib ]" puts puts " Runs a suite of memory allocation and access benchmarks." puts puts " <Name:/path/to/libmbmalloc.dylib> is a symbolic name followed by a path to libmbmalloc.dylib." puts puts " Specify \"SystemMalloc\" to test the built-in libc malloc." puts " Specify \"NanoMalloc\" to test the built-in libc malloc using the NanoMalloc zone." puts puts " Example usage:" puts puts " run-malloc-benchmarks /BUILD/MallocBench SystemMalloc:/BUILD/libmbmalloc.dylib NanoMalloc:/BUILD/libmbmalloc.dylib" puts " run-malloc-benchmarks /BUILD/MallocBench FastMalloc:/BUILD/FastMalloc/libmbmalloc.dylib" puts " run-malloc-benchmarks --benchmark churn SystemMalloc:/BUILD/libmbmalloc.dylib FastMalloc:/BUILD/FastMalloc/libmbmalloc.dylib" puts puts "Options:" puts puts " --benchmark <benchmark> Select a single benchmark to run instead of the full suite." puts " --heap <heap> Set a baseline heap size." puts end class Dylib attr_reader :name attr_reader :path def initialize(name, path) @name = name @path = path end end class Results attr_reader :executionTime attr_reader :peakMemory attr_reader :memoryAtEnd def initialize(executionTime, peakMemory, memoryAtEnd) @executionTime = executionTime @peakMemory = peakMemory @memoryAtEnd = memoryAtEnd end end class Stat attr_reader :benchmark attr_reader :result def initialize(benchmark, result) @benchmark = benchmark @result = result[/\d+/].to_i end end class TimeStat < Stat def to_s @result + "ms" end end class MemoryStat < Stat def to_s @result + "kB" end end class PeakMemoryStat < Stat def to_s @result + "kB" end end def lpad(str, chars) if str.length > chars str else "%#{chars}s"%(str) end end def rpad(str, chars) while str.length < chars str += " " end str end def computeArithmeticMean(array) sum = 0.0 array.each { | value | sum += value } (sum / array.length) end def computeGeometricMean(array) mult = 1.0 array.each { | value | mult *= value ? value : 1.0 } (mult ** (1.0 / array.length)) end def computeHarmonicMean(array) 1.0 / computeArithmeticMean(array.collect{ | value | 1.0 / value }) end def lowerIsBetter(a, b, better, worse) if b < a return "^ " + (a.to_f / b.to_f).round(2).to_s + "x " + better end if b == a return "" end "! " + (b.to_f / a.to_f).round(2).to_s + "x " + worse end def lowerIsFaster(a, b) lowerIsBetter(a, b, "faster", "slower") end def lowerIsSmaller(a, b) lowerIsBetter(a, b, "smaller", "bigger") end def numberWithDelimiter(number) number.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse end def prettify(number, suffix) numberWithDelimiter(number) + suffix end def parseOptions GetoptLong.new( ['--benchmark', GetoptLong::REQUIRED_ARGUMENT], ['--memory', GetoptLong::NO_ARGUMENT], ['--memory_warning', GetoptLong::NO_ARGUMENT], ['--heap', GetoptLong::REQUIRED_ARGUMENT], ['--help', GetoptLong::NO_ARGUMENT], ).each { | opt, arg | case opt when '--benchmark' $benchmarks = [ arg ] when '--memory' $benchmarks = $benchmarks_memory when '--memory_warning' $benchmarks = $benchmarks_memory_warning when '--heap' $heap = arg when '--help' usage exit 1 else raise "bad option: #{opt}" end } if ARGV.length < 1 puts "Error: No MallocBench specified." exit 1 end if ARGV.length < 2 puts "Error: No dylib specified." exit 1 end $mallocBench = File.absolute_path(ARGV.shift) if !File.exists?($mallocBench) puts "File not found: #{$mallocBench}." exit 1 end $buildDir = Pathname.new($mallocBench).dirname dylibs = [] ARGV.each { | arg | name, path = arg.split(":") if !name || name.length < 1 || !path || path.length < 1 puts "Invalid <Name:/path/to/dylib>: '#{arg}'." exit 1 end dylib = Dylib.new(name, File.expand_path(path)) if !File.exists?(dylib.path) puts "File not found: #{dylib.path}." exit 1 end dylibs.push(dylib) } dylibs end def runBenchmarks(dylibs) executionTime = [] peakMemory = [] memoryAtEnd = [] $benchmarks.each { | benchmark | executionTime.push([]) peakMemory.push([]) memoryAtEnd.push([]) dylibs.each { | dylib | $stderr.print "\rRUNNING #{dylib.name}: #{benchmark}... " env = "DYLD_LIBRARY_PATH='#{Pathname.new(dylib.path).dirname}' " env += "LD_LIBRARY_PATH='#{Pathname.new(dylib.path).dirname}' " if dylib.name == "NanoMalloc" env += "MallocNanoZone=1 " elsif dylib.name == "SystemMalloc" env += "MallocNanoZone=0 " end input = "cd '#{$buildDir}'; #{env} '#{$mallocBench}' --benchmark #{benchmark} --heap #{$heap}}" output =`#{input}` splitOutput = output.split("\n") executionTime[-1].push(TimeStat.new(benchmark, splitOutput[1])) peakMemory[-1].push(PeakMemoryStat.new(benchmark, splitOutput.length > 3 ? splitOutput[2] : "0")) memoryAtEnd[-1].push(MemoryStat.new(benchmark, splitOutput.length > 2 ? splitOutput[3] : "0")) } } $stderr.print "\r \n" Results.new(executionTime, peakMemory, memoryAtEnd) end def printResults(dylibs, results) def printHeader(dylibs, fieldSize) print print lpad("", fieldSize) print lpad(dylibs[0].name, fieldSize) if dylibs.length > 1 print lpad(dylibs[1].name, fieldSize) print lpad("Δ", fieldSize) end print "\n" end def printMetric(name, results, compareFunction, suffix, fieldSize) def printMean(name, results, meanFunction, compareFunction, suffix, fieldSize) means = [] means.push(meanFunction.call(results.collect { | stats | stats[0].result })) print rpad(" " + name, fieldSize) print lpad("#{prettify(means[0].round, suffix)}", fieldSize) if results[0][1] means.push(meanFunction.call(results.collect { | stats | stats[1].result })) print lpad("#{prettify(means[1].round, suffix)}", fieldSize) print lpad(compareFunction.call(means[0], means[1]), fieldSize) end print "\n" end if results[0][0].result == 0 return end print name + ":\n" results.each { | stats | print rpad(" " + stats[0].benchmark, fieldSize) print lpad("#{prettify(stats[0].result, suffix)}", fieldSize) if stats[1] print lpad("#{prettify(stats[1].result, suffix)}", fieldSize) print lpad(compareFunction.call(stats[0].result, stats[1].result), fieldSize) end print "\n" } print "\n" printMean("<geometric mean>", results, method(:computeGeometricMean), compareFunction, suffix, fieldSize) printMean("<arithmetic mean>", results, method(:computeArithmeticMean), compareFunction, suffix, fieldSize) printMean("<harmonic mean>", results, method(:computeHarmonicMean), compareFunction, suffix, fieldSize) print "\n" end fieldSize = ($benchmarks + ["<arithmetic mean>"]).collect { | benchmark | benchmark.size }.max + 4 printHeader(dylibs, fieldSize) printMetric("Execution Time", results.executionTime, method(:lowerIsFaster), "ms", fieldSize) printMetric("Peak Memory", results.peakMemory, method(:lowerIsSmaller), "kB", fieldSize) printMetric("Memory at End", results.memoryAtEnd, method(:lowerIsSmaller), "kB", fieldSize) end def main begin dylibs = parseOptions() results = runBenchmarks(dylibs) printResults(dylibs, results) rescue => exception puts puts puts exception puts exception.backtrace puts end end main() |