How to add some execution information to the console

71 views
Skip to first unread message

etorreborre

unread,
Feb 3, 2013, 1:37:27 AM2/3/13
to specs2...@googlegroups.com
Hi,

Here's a not-well-known tip on how to print (for example) execution times to the console. From the SNAPSHOT version of the UserGuide:

===========

Knowing that an example passed is fine but sometimes you want to display more information, like the time spent executing the example for instance.

This can be done by creating a Context object which will update the Result of the example execution with whatever you want to display:

  import org.specs2._
 
import time._
 
import specification._
 
import execute._

  trait
Timed extends Around {
   
def around[T : AsResult](t: =>T): Result = {
     
// use `ResultExecution.execute` to catch possible exceptions
      val
(result, timer) = withTimer(ResultExecution.execute(AsResult(t)))

     
// update the result with a piece of text which will be displayed in the console
      result
.updateExpected("Execution time: "+timer.time)
   
}

   
/** mesure the execution time of a piece of code */
   
def withTimer[T](t: =>T): (T, SimpleTimer) = {
      val timer
= (new SimpleTimer).start
      val result
= t
     
(result, timer.stop)
   
}

 
}

When you execute a specification where each example uses this Around context (by implementing the AroundExampleContext trait for example) you should see the timing of each example displayed in the console:

  [info] TimedExecutionSpecification
 
[info]
 
[info] + example 1
 
[info] Execution time: 94 ms
 
[info] + example 2
 
[info] Execution time: 11 ms

============

Cheers,

Eric.

Diego Medina

unread,
Feb 3, 2013, 8:24:23 AM2/3/13
to specs2...@googlegroups.com

Nice one Eric!

Diego
Sent from my android cell

--
You received this message because you are subscribed to the Google Groups "specs2-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specs2-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Oliver Schrenk

unread,
Dec 23, 2016, 11:53:52 AM12/23/16
to specs2-users
How would you use this?

Instead of Around I extend AroundEach and then have my Spec extend that trait, so...

````
trait Timed extends AroundEach {
  def around[T : AsResult](t: =>T): Result = {
    // use `ResultExecution.execute` to catch possible exceptions
    val (result, timer) = withTimer(ResultExecution.execute(AsResult(t)))

    // update the result with a piece of text which will be displayed in the console
    result.updateExpected("Execution time: "+timer.time)
  }

  /** mesure the execution time of a piece of code */
  def withTimer[T](t: =>T): (T, SimpleTimer) = {
    val timer = (new SimpleTimer).start
    val result = t
    (result, timer.stop)
  }
}
```

and

```
class AdminApiSpecs extends Specification with Specs2RouteTest with JsonMatchers with Timed ....
```

I may understand contexts wrong but I don't see any outout with timings>

etorreborre

unread,
Dec 26, 2016, 3:57:38 PM12/26/16
to specs2-users
If you just want to display execution times, there is a `showtimes` arguments which you can use:

sbt> testOnly -- showtimes

Eric.

Oliver Schrenk

unread,
Jan 3, 2017, 11:03:31 AM1/3/17
to specs2-users
That's actually good enough for me, Didn't know about this

Cheers,
Oliver
Reply all
Reply to author
Forward
0 new messages