@@ -21,7 +21,7 @@ test.data.table = function(script="tests.Rraw", verbose=FALSE, pkg=".", silent=F
2121 if (identical(script ," *.Rraw" )) {
2222 # nocov start
2323 scripts = dir(fulldir , " *.Rraw.*" )
24- scripts = scripts [! grepl(" bench|other" , scripts )]
24+ scripts = scripts [! grepl(" bench|other|manual " , scripts )]
2525 scripts = gsub(" [.]bz2$" ," " ,scripts )
2626 return (sapply(scripts , function (fn ) {
2727 err = try(test.data.table(script = fn , verbose = verbose , pkg = pkg , silent = silent , showProgress = showProgress ))
@@ -459,3 +459,89 @@ test = function(num,x,y=TRUE,error=NULL,warning=NULL,message=NULL,output=NULL,no
459459 invisible (! fail )
460460}
461461
462+ is.AsIs = function (x ) inherits(x , " AsIs" )
463+
464+ # nocov start
465+ benchmark = function (num , expr , limit , tolerance = 0.025 , verbose = FALSE ) {
466+
467+ .test.data.table = exists(" nfail" , parent.frame()) # test() can be used inside functions defined in tests.Rraw, so inherits=TRUE (default) here
468+ numStr = sprintf(" %.8g" , num )
469+ if (.test.data.table ) {
470+ prevtest = get(" prevtest" , parent.frame())
471+ nfail = get(" nfail" , parent.frame())
472+ whichfail = get(" whichfail" , parent.frame())
473+ assign(" ntest" , get(" ntest" , parent.frame()) + 1L , parent.frame(), inherits = TRUE ) # bump number of tests run
474+ lasttime = get(" lasttime" , parent.frame())
475+ timings = get(" timings" , parent.frame())
476+ # memtest = get("memtest", parent.frame())
477+ inittime = get(" inittime" , parent.frame())
478+ filename = get(" filename" , parent.frame())
479+ foreign = get(" foreign" , parent.frame())
480+ showProgress = get(" showProgress" , parent.frame())
481+ time = nTest = NULL # to avoid 'no visible binding' note
482+ on.exit( {
483+ now = proc.time()[3L ]
484+ took = now - lasttime # so that prep time between tests is attributed to the following test
485+ assign(" lasttime" , now , parent.frame(), inherits = TRUE )
486+ timings [ as.integer(num ), `:=`(time = time + took , nTest = nTest + 1L ), verbose = FALSE ]
487+ } )
488+ if (showProgress )
489+ cat(" \r Running benchmark id" , numStr , " " )
490+ } else {
491+ # not `test.data.table` but developer running tests manually; i.e. `cc(F); test(...)`
492+ memtest = FALSE
493+ filename = NA_character_
494+ foreign = FALSE
495+ showProgress = FALSE
496+ }
497+
498+ sub.expr = substitute(expr )
499+ stopifnot(is.call(sub.expr ))
500+ l = if (limit.call <- is.call(sub.limit <- substitute(limit ))) system.time(limit )[[" elapsed" ]]
501+ else if (is.numeric(limit )) limit
502+ else stop(" limit must be constant numeric or a call to time it" )
503+
504+ t = system.time(expr )[[" elapsed" ]]
505+
506+ fail = FALSE
507+ if (.test.data.table ) {
508+ if (num < prevtest + 0.0000005 ) {
509+ cat(" Test id" , numStr , " is not in increasing order\n " )
510+ fail = TRUE
511+ }
512+ assign(" prevtest" , num , parent.frame(), inherits = TRUE )
513+ }
514+ if (! fail ) {
515+ if (length(tolerance )== 1L ) {
516+ fail = if (is.AsIs(tolerance )) {
517+ t > l + tolerance
518+ } else {
519+ t > l * (1 + tolerance )
520+ }
521+ } else if (length(tolerance )== 2L ) { # # absolute difference, test 655
522+ if (tolerance [1 ] > = tolerance [2 ])
523+ stop(" invalid use of tolerance argument, first element must be smaller than second one, first usually being negative and second positive" )
524+ fail = if (is.AsIs(tolerance )) {
525+ t < l + tolerance [1 ] || t > l + tolerance [2 ]
526+ } else {
527+ t < l * (1 + tolerance [1L ]) || t > l * (1 + tolerance [2 ])
528+ }
529+ } else stop(" tolerance must be length 1 or 2" )
530+ if (fail || verbose ) {
531+ cat(sprintf(" Benchmark %s %scheck that expression:\n > " ,
532+ numStr , if (fail ) " failed to " else " " ))
533+ print(sub.expr )
534+ cat(" # elapsed: " , t , " \n " , sep = " " )
535+ cat(sprintf(" computes within given limit and tolerance (%s):\n > " ,
536+ paste(sprintf(" %.3f%s" , tolerance , if (is.AsIs(tolerance )) " s" else " " ), collapse = " ," )))
537+ if (limit.call ) print(sub.limit ) else cat(limit , " \n " , sep = " " )
538+ cat(" # elapsed: " , l , " \n " , sep = " " )
539+ }
540+ }
541+ if (fail && .test.data.table ) {
542+ assign(" nfail" , nfail + 1L , parent.frame(), inherits = TRUE )
543+ assign(" whichfail" , c(whichfail , numStr ), parent.frame(), inherits = TRUE )
544+ }
545+ invisible (! fail )
546+ }
547+ # nocov end
0 commit comments