]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/internal/timing/Timing.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / internal / timing / Timing.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.internal.timing;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.diagram.internal.DebugPolicy;
19 import org.simantics.diagram.synchronization.ErrorHandler;
20
21 /**
22  * @author Tuukka Lehtonen
23  */
24 public final class Timing {
25
26     private static Method BEGIN_METHOD;
27     private static Method END_METHOD;
28
29     static {
30         // Try to get ThreadLog, if possible.
31         try {
32             Class<?> logClass = Class.forName("org.simantics.threadlog.ThreadLog");
33             Class<?> taskClass = Class.forName("org.simantics.threadlog.Task");
34             Method begin = logClass.getMethod("BEGIN", String.class);
35             Method end = taskClass.getMethod("end");
36
37             BEGIN_METHOD = begin;
38             END_METHOD = end;
39         } catch (ClassNotFoundException e) {
40         } catch (NoSuchMethodException e) {
41         } catch (SecurityException e) {
42             // No dice, just don't do timing then.
43         }
44     }
45
46     public static void timed(String label, GTask r) throws DatabaseException {
47         Object t = Timing.BEGIN(label);
48         try {
49             long start = 0, end = 0;
50             if (DebugPolicy.PERFORM_TIMING)
51                 start = System.nanoTime();
52             r.run();
53             if (DebugPolicy.PERFORM_TIMING) {
54                 end = System.nanoTime();
55                 System.out.println("TIMED " + ((end-start)*1e-6) + "ms for " + label);
56             }
57         } finally {
58             Timing.END(t);
59         }
60     }
61
62     public static void safeTimed(ErrorHandler errorHandler, String label, GTask r) {
63         Object t = Timing.BEGIN(label);
64         try {
65             long start = 0, end = 0;
66             if (DebugPolicy.PERFORM_TIMING)
67                 start = System.nanoTime();
68             try {
69                 r.run();
70             } catch (DatabaseException e) {
71                 errorHandler.error(e.getMessage(), e);
72             }
73             if (DebugPolicy.PERFORM_TIMING) {
74                 end = System.nanoTime();
75                 System.out.println("TIMED " + ((end-start)*1e-6) + "ms for " + label);
76             }
77         } finally {
78             Timing.END(t);
79         }
80     }
81
82     public static Object BEGIN(String name) {
83         if (DebugPolicy.PERFORM_TIMING) {
84             //return ThreadLog.BEGIN(name);
85             if (BEGIN_METHOD != null) {
86                 try {
87                     return BEGIN_METHOD.invoke(null, name);
88                 } catch (IllegalArgumentException e) {
89                     e.printStackTrace();
90                 } catch (IllegalAccessException e) {
91                     e.printStackTrace();
92                 } catch (InvocationTargetException e) {
93                     e.printStackTrace();
94                 }
95             }
96         }
97         return null;
98     }
99
100     public static void END(Object task) {
101         if (DebugPolicy.PERFORM_TIMING) {
102             //((Task) task).end();
103             if (task != null) {
104                 try {
105                     END_METHOD.invoke(task);
106                 } catch (IllegalArgumentException e) {
107                     e.printStackTrace();
108                 } catch (IllegalAccessException e) {
109                     e.printStackTrace();
110                 } catch (InvocationTargetException e) {
111                     e.printStackTrace();
112                 }
113             }
114         }
115     }
116
117 }