public Value(String payload) { this.payload = payload; if (!EMPTY_PAYLOAD.equals(payload)) { Stopwatch tracer = compressPayloadTimer.start(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream out = new GZIPOutputStream(bos); byte[] rawBytes = payload.getBytes(); out.write(rawBytes); // Finish creation of gzip file out.finish(); out.close(); bos.close(); gzipped = bos.toByteArray(); } catch (IOException e) { gzipped = null; } finally { if (tracer != null) { tracer.stop(); } } } else { gzipped = null; } }
/** * Record a duration to the dynamic timer indicated by the provided config. * The units in which the timer is reported and the duration unit are the same. * * @deprecated Use {@link DynamicTimer#record(MonitorConfig, java.util.concurrent.TimeUnit, * long, java.util.concurrent.TimeUnit)} instead. * The new method allows you to be specific about the units used for reporting the timer and * the units in which the duration is measured. */ public static void record(MonitorConfig config, long duration, TimeUnit unit) { INSTANCE.get(config, unit).record(duration, unit); }
@Test public void testGetValue() throws Exception { Stopwatch s = DynamicTimer.start("test1", tagList); Timer c = getByName("test1"); s.stop(); // we don't call s.stop(), so we only have one recorded value assert c != null; assertEquals(c.getValue().longValue(), s.getDuration(TimeUnit.MILLISECONDS)); c.record(13, TimeUnit.MILLISECONDS); long expected = (13 + s.getDuration(TimeUnit.MILLISECONDS)) / 2; assertEquals(c.getValue().longValue(), expected); }
/** * {@inheritDoc} */ @Override @Deprecated public void record(long duration) { Timer monitor = getMonitorForCurrentContext(); monitor.record(duration, monitor.getTimeUnit()); }
/** Process the batch. */ public void run() { try { if (batch == null) { return; } int inProcess = stream.concurrentBatches.incrementAndGet(); try { avgConcurrentBatches.record(inProcess); Stopwatch s = processMessagesTracer.start(); stream.target.process(batch); s.stop(); } finally { stream.concurrentBatches.decrementAndGet(); } } catch (Throwable e) { e.printStackTrace(); } } }
/** * {@inheritDoc} */ @Override public Long getValue(int pollerIndex) { return getMonitorForCurrentContext().getValue(); } }
/** * {@inheritDoc} */ @Override public TimeUnit getTimeUnit() { return getMonitorForCurrentContext().getTimeUnit(); }
/** * {@inheritDoc} */ @Override @Deprecated public void record(long duration) { Timer monitor = getMonitorForCurrentContext(); monitor.record(duration, monitor.getTimeUnit()); }
@Test public void testByStrings() throws Exception { Stopwatch s = DynamicTimer.start("byName"); Stopwatch s2 = DynamicTimer.start("byName2", "key", "value"); Thread.sleep(100L); s.stop(); s2.stop(); Timer c1 = getByName("byName"); assert c1 != null; assertEquals(c1.getValue().longValue(), s.getDuration(TimeUnit.MILLISECONDS)); Timer c2 = getByName("byName2"); assert c2 != null; assertEquals(c2.getValue().longValue(), s2.getDuration(TimeUnit.MILLISECONDS)); } }
/** * {@inheritDoc} */ @Override public TimeUnit getTimeUnit() { return getMonitorForCurrentContext().getTimeUnit(); }
/** * Returns a stopwatch that has been started and will automatically * record its result to the dynamic timer specified by the given config. The timer * will report the times in milliseconds to observers. * * @see #start(MonitorConfig, TimeUnit) */ public static Stopwatch start(MonitorConfig config) { return INSTANCE.get(config, TimeUnit.MILLISECONDS).start(); }
/** * {@inheritDoc} */ @Override public void record(long duration, TimeUnit timeUnit) { getMonitorForCurrentContext().record(duration, timeUnit); }
@Test public void testExpiration() throws Exception { clock.set(0L); DynamicTimer.start("test1", tagList); DynamicTimer.start("test2", tagList); clock.set(500L); DynamicTimer.start("test1", tagList); clock.set(1000L); Stopwatch s = DynamicTimer.start("test1", tagList); clock.set(1200L); s.stop(); Timer c1 = getByName("test1"); assert c1 != null; assertEquals(c1.getValue().longValue(), s.getDuration(TimeUnit.MILLISECONDS)); Thread.sleep(200L); Timer c2 = getByName("test2"); assertNull(c2, "Timers not used in a while should expire"); }
/** * Returns a stopwatch that has been started and will automatically * record its result to the dynamic timer specified by the given config. * * @param config Config to identify a particular timer instance to update. * @param unit The unit to use when reporting values to observers. For example if sent to * a typical time series graphing system this would be the unit for the y-axis. * It is generally recommended to use base units for reporting, so * {@link TimeUnit#SECONDS} is the preferred value. */ public static Stopwatch start(MonitorConfig config, TimeUnit unit) { return INSTANCE.get(config, unit).start(); }
/** * Record result to the dynamic timer indicated by the provided config * with a TimeUnit of milliseconds. */ public static void record(MonitorConfig config, long duration) { INSTANCE.get(config, TimeUnit.MILLISECONDS).record(duration, TimeUnit.MILLISECONDS); }
/** * {@inheritDoc} */ @Override public Long getValue(int pollerIndex) { return getMonitorForCurrentContext().getValue(); } }
@Override protected BasicPoolEntry getEntryBlocking(HttpRoute route, Object state, long timeout, TimeUnit tunit, WaitingThreadAborter aborter) throws ConnectionPoolTimeoutException, InterruptedException { Stopwatch stopWatch = requestTimer.start(); try { return super.getEntryBlocking(route, state, timeout, tunit, aborter); } finally { stopWatch.stop(); } }
/** * Record a duration to the dynamic timer indicated by the provided config/reportUnit. * * @param config Config to identify a particular timer instance to update. * @param reportUnit The unit to use when reporting values to observers. For example if sent to * a typical time series graphing system this would be the unit * for the y-axis. * It is generally recommended to use base units for reporting, so * {@link TimeUnit#SECONDS} is the preferred value. * @param duration Measured duration to record. * @param durationUnit Unit for the measured duration. This should typically be the unit used for * timing source. For example if using {@link System#nanoTime()} * the unit would be nanoseconds. */ public static void record(MonitorConfig config, TimeUnit reportUnit, long duration, TimeUnit durationUnit) { INSTANCE.get(config, reportUnit).record(duration, durationUnit); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // if the method is one of the CompositeMonitor interface final Class<?> declaringClass = method.getDeclaringClass(); if (declaringClass.isAssignableFrom(CompositeMonitor.class)) { return method.invoke(this, args); } final String methodName = method.getName(); final Timer timer = timers.get(methodName); final Stopwatch stopwatch = timer.start(); try { return method.invoke(concrete, args); } finally { stopwatch.stop(); } } }
/** * {@inheritDoc} */ @Override public void stop() { super.stop(); timer.record(getDuration(), TimeUnit.NANOSECONDS); } }