/usr/share/pyshared/epsilon/scripts/benchmark.py is in python-epsilon 0.7.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | # -*- test-case-name: epsilon.test.test_benchmark -*-
"""
Functions for running a Python file in a child process and recording resource
usage information and other statistics about it.
"""
import os, time, sys, socket, StringIO, pprint, errno
import twisted
from twisted.python import log, filepath, failure, util
from twisted.internet import reactor, protocol, error, defer
from twisted.protocols import policies
import epsilon
from epsilon import structlike
from epsilon import juice
from epsilon.test import utils
class diskstat(structlike.record(
'readCount mergedReadCount readSectorCount readMilliseconds '
'writeCount mergedWriteCount writeSectorCount writeMilliseconds '
'outstandingIOCount ioMilliseconds weightedIOMilliseconds')):
"""
Represent the I/O stats of a single device, as reported by Linux's disk
stats.
"""
class partitionstat(structlike.record(
'readCount readSectorCount writeCount writeSectorCount')):
"""
Like diskstat, but for a partition. Less information is made available by
Linux for partitions, so this has fewer attributes.
"""
def parseDiskStatLine(L):
"""
Parse a single line from C{/proc/diskstats} into a two-tuple of the name of
the device to which it corresponds (ie 'hda') and an instance of the
appropriate record type (either L{partitionstat} or L{diskstat}).
"""
parts = L.split()
device = parts[2]
if len(parts) == 7:
factory = partitionstat
else:
factory = diskstat
return device, factory(*map(int, parts[3:]))
def parseDiskStats(fObj):
"""
Parse a file-like object containing lines formatted like those in
C{/proc/diskstats}. Yield two-tuples of information for each line.
"""
for L in fObj:
yield parseDiskStatLine(L)
def captureStats():
"""
Parse the current contents of C{/proc/diskstats} into a dict mapping device
names to instances of the appropriate stat record.
"""
return dict(parseDiskStats(file('/proc/diskstats')))
class ResourceSnapshot(structlike.record('time disk partition size')):
"""
Represents the state of some resources on the system at a particular moment
in time.
@ivar time: The time at which the stats associated with this instance were
recorded.
@ivar disk: A C{diskstat} instance created from stats available that the
given time.
@ivar partition: A C{diskstat} instance created from stats available that
the given time.
@ivar size: Total size of all files beneath a particular directory.
"""
class ProcessDied(Exception):
"""
Encapsulates process state and failure mode.
"""
def __init__(self, exitCode, signal, status, output):
self.exitCode = exitCode
self.signal = signal
self.status = status
self.output = output
Exception.__init__(self)
class BasicProcess(protocol.ProcessProtocol, policies.TimeoutMixin):
"""
The simplest possible process protocol. It doesn't do anything except what
is absolutely mandatory of any conceivable ProcessProtocol.
"""
timedOut = False
BACKCHANNEL_OUT = 3
BACKCHANNEL_IN = 4
def __init__(self, whenFinished, path):
self.whenFinished = whenFinished
self.path = path
self.output = []
def connectionMade(self):
self.setTimeout(900.0)
def timeoutConnection(self):
self.timedOut = True
self.transport.signalProcess('KILL')
def childDataReceived(self, childFD, data):
self.resetTimeout()
self.output.append((childFD, data))
def childConnectionLost(self, childFD):
self.resetTimeout()
self.output.append((childFD, None))
def processEnded(self, reason):
# XXX Okay, I'm a liar. This doesn't do everything. Strictly speaking
# we shouldn't fire completion notification until the process has
# terminated *and* the file descriptors have all been closed. We're
# not supporting passing file descriptors from the child to a
# grandchild here, though. Don't Do It.
d, self.whenFinished = self.whenFinished, None
o, self.output = self.output, None
if reason.check(error.ProcessDone):
d.callback((self, reason.value.status, o))
elif self.timedOut:
d.errback(error.TimeoutError())
elif reason.check(error.ProcessTerminated):
d.errback(failure.Failure(ProcessDied(
reason.value.exitCode,
reason.value.signal,
reason.value.status, o)))
else:
d.errback(reason.value)
self.setTimeout(None)
def spawn(cls, executable, args, path, env, spawnProcess=None):
"""
Run an executable with some arguments in the given working directory with
the given environment variables.
Returns a Deferred which fires with a two-tuple of (exit status, output
list) if the process terminates without timing out or being killed by a
signal. Otherwise, the Deferred errbacks with either L{error.TimeoutError}
if any 10 minute period passes with no events or L{ProcessDied} if it is
killed by a signal.
On success, the output list is of two-tuples of (file descriptor, bytes).
"""
d = defer.Deferred()
proto = cls(d, filepath.FilePath(path))
if spawnProcess is None:
spawnProcess = reactor.spawnProcess
spawnProcess(
proto,
executable,
[executable] + args,
path=path,
env=env,
childFDs={0: 'w', 1: 'r', 2: 'r',
cls.BACKCHANNEL_OUT: 'r',
cls.BACKCHANNEL_IN: 'w'})
return d
spawn = classmethod(spawn)
class Change(object):
"""
Stores two ResourceSnapshots taken at two different times.
"""
def start(self, path, disk, partition):
# Do these three things as explicit, separate statments to make sure
# gathering disk stats isn't accidentally included in the duration.
startSize = getSize(path)
beforeDiskStats = captureStats()
startTime = time.time()
self.before = ResourceSnapshot(
time=startTime,
disk=beforeDiskStats.get(disk, None),
partition=beforeDiskStats.get(partition, None),
size=startSize)
def stop(self, path, disk, partition):
# Do these three things as explicit, separate statments to make sure
# gathering disk stats isn't accidentally included in the duration.
endTime = time.time()
afterDiskStats = captureStats()
endSize = getSize(path)
self.after = ResourceSnapshot(
time=endTime,
disk=afterDiskStats.get(disk, None),
partition=afterDiskStats.get(partition, None),
size=endSize)
class BenchmarkProcess(BasicProcess):
START = '\0'
STOP = '\1'
def __init__(self, *a, **kw):
BasicProcess.__init__(self, *a, **kw)
# Figure out where the process is running.
self.partition = discoverCurrentWorkingDevice().split('/')[-1]
self.disk = self.partition.rstrip('0123456789')
# Keep track of stats for the entire process run.
self.overallChange = Change()
self.overallChange.start(self.path, self.disk, self.partition)
# Just keep track of stats between START and STOP events.
self.benchmarkChange = Change()
def connectionMade(self):
return BasicProcess.connectionMade(self)
def startTiming(self):
self.benchmarkChange.start(self.path, self.disk, self.partition)
self.transport.writeToChild(self.BACKCHANNEL_IN, self.START)
def stopTiming(self):
self.benchmarkChange.stop(self.path, self.disk, self.partition)
self.transport.writeToChild(self.BACKCHANNEL_IN, self.STOP)
def childDataReceived(self, childFD, data):
if childFD == self.BACKCHANNEL_OUT:
self.resetTimeout()
for byte in data:
if byte == self.START:
self.startTiming()
elif byte == self.STOP:
self.stopTiming()
else:
self.transport.signalProcess('QUIT')
else:
return BasicProcess.childDataReceived(self, childFD, data)
def processEnded(self, reason):
self.overallChange.stop(self.path, self.disk, self.partition)
return BasicProcess.processEnded(self, reason)
STATS_VERSION = 0
class Results(juice.Command):
commandName = 'Result'
arguments = [
# Stats version - change this whenever the meaning of something changes
# or a field is added or removed.
('version', juice.Integer()),
# If an error occurred while collecting these stats - this probably
# means they're bogus.
('error', juice.Boolean()),
# If a particular timeout (See BasicProcess.connectionMade) elapsed
# with no events whatsoever from the benchmark process.
('timeout', juice.Boolean()),
# A unique name identifying the benchmark for which these are stats.
('name', juice.Unicode()),
# The name of the benchmark associated with these stats.
('host', juice.Unicode()),
# The sector size of the disk on which these stats were collected
# (sectors are a gross lie, this is really the block size, and
# everything else that talks about sectors is really talking about
# blocks).
('sector_size', juice.Integer()),
# Hex version info for the Python which generated these stats.
('python_version', juice.Unicode()),
# Twisted SVN revision number used to generate these stats.
('twisted_version', juice.Unicode()),
# Divmod SVN revision number used to generate these stats.
('divmod_version', juice.Unicode()),
# Number of seconds between process startup and termination.
('elapsed', juice.Float()),
# Size, in bytes, of the directory in which the child process was run.
('filesystem_growth', juice.Integer()),
# Number of reads issued on the partition over the lifetime of the
# child process. This may include reads from other processes, if any
# were active on the same disk when the stats were collected.
('read_count', juice.Integer(optional=True)),
# Number of sectors which were read from the partition over the
# lifetime of the child process. Same caveat as above.
('read_sectors', juice.Integer(optional=True)),
# Number of writes issued to the partition over the lifetime of the
# child process. Same caveat as above.
('write_count', juice.Integer(optional=True)),
# Number of sectors which were written to the partition over the
# lifetime of the child process. Same caveat as above.
('write_sectors', juice.Integer(optional=True)),
# Number of milliseconds spent blocked on reading from the disk over
# the lifetime of the child process. Same caveat as above.
('read_ms', juice.Integer(optional=True)),
# Number of milliseconds spent blocked on writing to the disk over the
# lifetime of the child process. Same caveat as above.
('write_ms', juice.Integer(optional=True)),
]
hostname = socket.gethostname()
assert hostname != 'localhost', "Fix your computro."
def formatResults(name, sectorSize, before, after, error, timeout):
output = StringIO.StringIO()
jj = juice.Juice(issueGreeting=False)
tt = utils.FileWrapper(output)
jj.makeConnection(tt)
if after.partition is not None:
read_count = after.partition.readCount - before.partition.readCount
read_sectors = after.partition.readSectorCount - before.partition.readSectorCount
write_count = after.partition.writeCount - before.partition.writeCount
write_sectors = after.partition.writeSectorCount - before.partition.writeSectorCount
else:
read_count = None
read_sectors = None
write_count = None
write_sectors = None
if after.disk is not None:
read_ms = after.disk.readMilliseconds - before.disk.readMilliseconds
write_ms = after.disk.writeMilliseconds - before.disk.writeMilliseconds
else:
read_ms = None
write_ms = None
twisted_version = twisted.version._getSVNVersion()
if twisted_version is None:
twisted_version = twisted.version.short()
epsilon_version = epsilon.version._getSVNVersion()
if epsilon_version is None:
epsilon_version = epsilon.version.short()
Results(
version=STATS_VERSION,
error=error,
timeout=timeout,
name=name,
host=hostname,
elapsed=after.time - before.time,
sector_size=sectorSize,
read_count=read_count,
read_sectors=read_sectors,
read_ms=read_ms,
write_count=write_count,
write_sectors=write_sectors,
write_ms=write_ms,
filesystem_growth=after.size - before.size,
python_version=unicode(sys.hexversion),
twisted_version=twisted_version,
divmod_version=epsilon_version,
).do(jj, requiresAnswer=False)
return output.getvalue()
def reportResults(results):
print results
print
fObj = file('output', 'ab')
fObj.write(results)
fObj.close()
def discoverCurrentWorkingDevice():
"""
Return a short string naming the device which backs the current working
directory, ie C{/dev/hda1}.
"""
possibilities = []
cwd = os.getcwd()
for L in file('/proc/mounts'):
parts = L.split()
if cwd.startswith(parts[1]):
possibilities.append((len(parts[1]), parts[0]))
possibilities.sort()
return possibilities[-1][-1]
def getSize(p):
"""
@type p: L{twisted.python.filepath.FilePath}
@return: The size, in bytes, of the given path and all its children.
"""
return sum(getOneSize(ch) for ch in p.walk())
def getOneSize(ch):
"""
@type ch: L{twisted.python.filepath.FilePath}
@return: The size, in bytes, of the given path only.
"""
try:
return ch.getsize()
except OSError, e:
if e.errno == errno.ENOENT:
# XXX FilePath is broken
if os.path.islink(ch.path):
return len(os.readlink(ch.path))
else:
raise
else:
raise
def getSectorSize(p):
return os.statvfs(p.path).f_bsize
def _bench(name, workingPath, function):
d = function()
def later(result):
err = timeout = False
if isinstance(result, failure.Failure):
err = True
if result.check(error.TimeoutError):
log.msg("Failing because timeout!")
timeout = True
elif result.check(ProcessDied):
log.msg("Failing because Failure!")
pprint.pprint(result.value.output)
print result.value.exitCode, result.value.signal
else:
log.err(result)
else:
proto, status, output = result
stderr = [bytes for (fd, bytes) in output if fd == 2]
if status or stderr != [None]:
err = True
log.msg("Failing because stderr or bad status")
pprint.pprint(result)
for n, change in [(name + '-overall', proto.overallChange),
(name + '-benchmark', proto.benchmarkChange)]:
reportResults(formatResults(
n,
getSectorSize(workingPath),
change.before,
change.after,
err,
timeout))
return d.addBoth(later)
def bench(name, path, func):
log.startLogging(sys.stdout)
log.msg("Running " + name)
d = _bench(name, path, func)
d.addErrback(log.err)
d.addCallback(lambda ign: reactor.stop())
reactor.run()
def makeBenchmarkRunner(path, args):
"""
Make a function that will run two Python processes serially: first one
which calls the setup function from the given file, then one which calls
the execute function from the given file.
"""
def runner():
return BenchmarkProcess.spawn(
executable=sys.executable,
args=['-Wignore'] + args,
path=path.path,
env=os.environ)
return runner
def start():
"""
Start recording stats. Call this from a benchmark script when your setup
is done. Call this at most once.
@raise RuntimeError: Raised if the parent process responds with anything
other than an acknowledgement of this message.
"""
os.write(BenchmarkProcess.BACKCHANNEL_OUT, BenchmarkProcess.START)
response = util.untilConcludes(os.read, BenchmarkProcess.BACKCHANNEL_IN, 1)
if response != BenchmarkProcess.START:
raise RuntimeError(
"Parent process responded with %r instead of START " % (response,))
def stop():
"""
Stop recording stats. Call this from a benchmark script when the code you
want benchmarked has finished. Call this exactly the same number of times
you call L{start} and only after calling it.
@raise RuntimeError: Raised if the parent process responds with anything
other than an acknowledgement of this message.
"""
os.write(BenchmarkProcess.BACKCHANNEL_OUT, BenchmarkProcess.STOP)
response = util.untilConcludes(os.read, BenchmarkProcess.BACKCHANNEL_IN, 1)
if response != BenchmarkProcess.STOP:
raise RuntimeError(
"Parent process responded with %r instead of STOP" % (response,))
def main():
"""
Run me with the filename of a benchmark script as an argument. I will time
it and append the results to a file named output in the current working
directory.
"""
name = sys.argv[1]
path = filepath.FilePath('.stat').temporarySibling()
path.makedirs()
func = makeBenchmarkRunner(path, sys.argv[1:])
try:
bench(name, path, func)
finally:
path.remove()
if __name__ == '__main__':
main()
|