Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tednaleid/aca90d7ff16ebbb89057407127b30939 to your computer and use it in GitHub Desktop.
Save tednaleid/aca90d7ff16ebbb89057407127b30939 to your computer and use it in GitHub Desktop.
import ratpack.exec.Blocking
import ratpack.exec.ExecInterceptor
import ratpack.exec.Execution
import ratpack.func.Block
import ratpack.handling.Context
import static ratpack.groovy.Groovy.ratpack
@GrabResolver(name = 'jcenter', root = 'http://jcenter.bintray.com/')
@GrabExclude('org.codehaus.groovy:groovy-all')
@Grab('io.ratpack:ratpack-groovy:1.3.0')
@Grab('org.slf4j:slf4j-simple:1.7.12')
public class ExecutionTimer {
private ExecutionTimer() {}
final UUID id = UUID.randomUUID()
final Long executionStart = System.currentTimeMillis()
Long segmentStart
ExecutionTimer startSegment() {
segmentStart = System.currentTimeMillis()
this
}
Long getExecutionTime() { System.currentTimeMillis() - executionStart }
Long getSegmentTime() { System.currentTimeMillis() - segmentStart }
@Override
String toString() {
"$id - segment time: ${segmentTime} execution time: ${executionTime}ms"
}
public static ExecutionTimer startExecutionSegment(Execution execution) {
ExecutionTimer timer = execution.maybeGet(ExecutionTimer).orElse(null)
if (!timer) {
timer = new ExecutionTimer()
execution.add(ExecutionTimer, timer)
}
timer.startSegment()
}
}
public class LoggingExecInterceptor implements ExecInterceptor {
@Override
void intercept(Execution execution, ExecInterceptor.ExecType execType, Block executionSegment) throws Exception {
ExecutionTimer timer = ExecutionTimer.startExecutionSegment(execution)
try {
executionSegment.execute()
} finally {
println "${Thread.currentThread().name} - $timer - ${execType}"
}
}
}
ratpack {
serverConfig {
development false
}
bindings {
bindInstance(new LoggingExecInterceptor())
}
handlers {
all { Context context ->
final String executionId = context.get(ExecutionTimer).id.toString()
println "${Thread.currentThread().name} - $executionId - A. Original compute thread"
Blocking.exec { ->
context.render "hello from blocking" // pretend blocking work
println "${Thread.currentThread().name} - $executionId - B. Blocking thread"
}
println "${Thread.currentThread().name} - $executionId - C. Original compute thread"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment