Created
June 6, 2017 16:38
-
-
Save AdamBrouwersHarries/f375684b11460c2f3629aa13a06f4fd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by adam on 31/05/17. | |
*/ | |
package profiler | |
import scala.collection.mutable | |
import com.github.nscala_time.time.Imports._ | |
class CheckPoint(val checkpointName: String, val startTime: org.joda.time.DateTime) {} | |
// NOT THREADSAFE | |
class StackProfiler(val name: String) { | |
val tStack : mutable.Stack[CheckPoint] = new mutable.Stack[CheckPoint]() | |
def pushCheckpoint(checkpointName: String) : Unit ={ | |
// get the time of pushing | |
val now = org.joda.time.DateTime.now | |
// push that time and the checkpoint name onto the stack | |
tStack.push(new CheckPoint(checkpointName, now)) | |
println(s"Starting task: ${name}-${checkpointName}") | |
} | |
def logProgress() : Unit = { | |
// get the top of the stack, and log a "progress report" | |
val currentCP = tStack.top | |
val progress = (currentCP.startTime to DateTime.now).millis | |
println(s"PROGRESS: ('${name}-${currentCP.checkpointName}', ${progress}, 'progress')") | |
} | |
def popCheckpoint() : Unit = { | |
val currentCP = tStack.pop() | |
val progress = (currentCP.startTime to DateTime.now).millis | |
println(s"TASK_RESULT: ('${name}-${currentCP.checkpointName}', ${progress}, 'result')") | |
} | |
def profile[T](name: String,f: () => T) : T = { | |
pushCheckpoint(name) | |
val r: T = f() | |
popCheckpoint() | |
r | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment