-1

I'm trying to calculate the runtime by subtracting "start" with "end" in Groovy, but I am not sure how to perform the mathematical calculation as I have derived the start and end time using the bash command (date in milliseconds). Here is the sample:

@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*
import wslite.http.auth.*
import java.util.regex.*
import groovy.xml.*
import groovy.util.*
import java.lang.*

Process start = 'date +%s%N'.execute()

.....
 SOAP request and response
.....

Process end = 'date +%s%N'.execute()

Now I'm looking something like below, but giving me HTTP 500 error :

Process runtime = '$((end-start))'.execute()
println runtime.text
shaa0601
  • 111
  • 1
  • 8
  • This show how to use bash command in Groovy: http://stackoverflow.com/questions/16741065/groovy-process-not-working-with-linux-shell-grep-and-awk-and-ps – shaa0601 Feb 19 '15 at 19:53
  • 1
    i'd rather measure time with the java/groovy functions instead of spawning a call to date, which again adds "time" to your measurement. – cfrick Feb 19 '15 at 20:14
  • can you please give me some example or links? I recently started using Groovy.... – shaa0601 Feb 19 '15 at 20:17
  • hmm maybe one of the many search hits here or on the interwebs would be of help? http://stackoverflow.com/questions/238920/how-do-i-calculate-the-elapsed-time-of-an-event-in-java – cfrick Feb 19 '15 at 21:13

1 Answers1

0

If you did want to do this with built-in stuff (as mentioned in your comment) then all you need is the start instant, the end instant and a Duration.

import java.time.*

LocalDateTime start = LocalDateTime.now()

// Do some stuff

LocalDateTime end = LocalDateTime.now()
Duration d = Duration.between(start, end)

println "Operation took ${d.seconds}.${d.nanos}s"

But I'm afraid no amount of Java's DateTime classes are going to fix your HTTP 500 error.

Poundex
  • 410
  • 4
  • 5
  • Thanks @Poundex. While testing with import java.time* ,I got these error messages "unable to resolve class LocalDateTime" and "unable to resolve class Duration". – shaa0601 Feb 19 '15 at 21:10
  • Hm, there's either something wrong with your import statement or you're not using JDK 1.8 (and as I'm presuming you're not an Oracle partner, you should be as 1.7 is being EOL'd in a month or so). That fact notwithstanding, if you don't have DateTime available (there's backports, btw) you can just grab the current instant as a Long with `long start/end = System.nanoTime()` and then subtract the longs. – Poundex Feb 19 '15 at 21:14
  • @shaa0601 this is for java 8 – cfrick Feb 19 '15 at 21:14
  • Thanks guys!! we are using 1.6 :( – shaa0601 Feb 19 '15 at 21:43