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