01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.support;
14:
15: import java.io.PrintStream;
16: import java.util.ArrayList;
17: import java.util.List;
18:
19: /**
20: * Utility class for measuring and dumping timestamps
21: *
22: * @author Ole.Matzura
23: */
24:
25: public class Timestamps {
26: private static ThreadLocal<Timestamps> timestamps = new ThreadLocal<Timestamps>();
27:
28: private List<String> labels = new ArrayList<String>();
29: private List<Long> times = new ArrayList<Long>();
30:
31: private long startTime;
32:
33: private Timestamps() {
34: startTime = System.nanoTime();
35: }
36:
37: public static void init() {
38: timestamps.set(new Timestamps());
39: }
40:
41: public static void addTimestamp(String label) {
42: timestamps.get().add(label);
43: }
44:
45: public synchronized static void dump() {
46: timestamps.get().dumpResult(System.out);
47: }
48:
49: public static void dump(PrintStream out) {
50: timestamps.get().dumpResult(out);
51: }
52:
53: private void add(String label) {
54: times.add(System.nanoTime());
55: labels.add(label);
56: }
57:
58: private void dumpResult(PrintStream out) {
59: out.println("Timestamps result:");
60: long last = startTime;
61: for (int c = 0; c < labels.size(); c++) {
62: out.println(labels.get(c) + "; "
63: + (times.get(c) - startTime) / 1000000);
64: last = times.get(c);
65: }
66: }
67:
68: public static long getTimeTaken() {
69: return timestamps.get().getInternalTimeTaken();
70: }
71:
72: private long getInternalTimeTaken() {
73: if (labels.isEmpty())
74: return 0;
75:
76: return (times.get(times.size() - 1) - startTime) / 1000000;
77: }
78: }
|