Create a Simple Groovy TimeWatch for Accurate Performance Timing
This article introduces a lightweight Groovy TimeWatch utility that lets developers record execution times with customizable markers, offering both millisecond and nanosecond precision, and explains its implementation, usage, and future multithreading considerations.
Overview
TimeWatch is a lightweight Groovy utility designed to record execution times of code blocks in Java‑based projects. It offers a simple API for starting a timer, creating named or default markers, and retrieving elapsed durations in both milliseconds and nanoseconds.
Key Features (v1.0)
Static factory methods create() and create(name) that instantiate a timer and start it immediately.
Automatic capture of the start moment in nanoseconds ( startTime) and milliseconds ( startTimeMillis).
Marker management via mark(name) and mark(), storing each marker in a HashMap<String, Mark>.
Retrieval of marker elapsed time:
Total elapsed time since timer creation via getTime() (ms) and getNanoTime() (ns).
Clone support to duplicate a timer’s state.
Typical Usage
import com.fun.utils.TimeWatch
// Create a timer with the default name
TimeWatch watch = TimeWatch.create()
// Perform some work
doSomething()
// Record a named marker
watch.mark('step1')
// More work
doAnotherThing()
// Retrieve elapsed times
watch.getMarkTime('step1') // logs milliseconds for 'step1'
watch.getMarkNanoTime('step1') // logs nanoseconds for 'step1'
watch.getTime() // total elapsed time in ms
watch.getNanoTime() // total elapsed time in nsImplementation Details
The class extends SourceCode (a project‑specific base) and uses org.slf4j.LoggerFactory for logging. Each Mark instance records its own start time in both nanoseconds and milliseconds, allowing independent measurement of multiple points within the same execution flow.
Formatting of nanosecond values uses java.text.DecimalFormat to insert thousand separators for readability.
Future Enhancements
Thread‑safety: current implementation stores markers in a plain HashMap, which is not safe for concurrent access. A concurrent map or synchronization will be added.
Compatibility checks for environments where System.nanoTime() may behave differently.
Groovy Compatibility Note
Because Groovy runs on the JVM, the utility can be compiled and used directly in any Java project. Most Groovy source files can be renamed to .java and compiled without modification, and vice‑versa, making integration straightforward. Modern IDEs such as IntelliJ IDEA provide full Groovy support, and build tools like Gradle can compile the class without additional configuration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
