Limiting CPU Time

BitterSuite already has a mechanism for limiting the amount of real time a test takes. However, sometimes a finer grain of control is desired on multiuser systems; this module will track how long an expression takes to evaluate in terms of CPU time. If the expression evaluates quicker, the value it evaluates to is produced; otherwise, an error message string is produced.

  • N.B. 1: This does not actually enforce any kind of timeout; if the code in question runs indefinitely, it still needs to be killed by the wall-time timeout. This needs to be improved if this is actually put into general use.
  • N.B. 2: This seems like something that would be generally useful for any test... see ImprovePit.

Example code for this and a few test cases follow:

#lang scheme/base

(provide cpu-time-eval)

(define DEFAULT_TIMEOUT 100)

(define (cpu-time-do-eval fn #:time-limit (timeout DEFAULT_TIMEOUT) . args)
  (let-values ([(result cpu-time real-time gc-time) (time-apply fn args)])
    (if (> cpu-time timeout)
        (format "Error: Timeout of ~a exceeded; total time ~a" timeout cpu-time)
        result)))

(define-syntax cpu-time-eval
  (syntax-rules ()
    ((_ expr)
     (cpu-time-do-eval (lambda () expr)))
    ((_ expr timeout)
     (cpu-time-do-eval (lambda () expr) #:time-limit timeout))))

#|
; Should produce 7
(cpu-time-eval (+ 4 3))

; Should produce failure messages
(cpu-time-eval (+ 4 3) -1)
(cpu-time-eval (build-list 1000000 values))
|#

-- TerryVaskor - 16 Mar 2009

Topic revision: r4 - 2009-07-30 - TerryVaskor
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback