]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/internal/timing/Timing.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/internal/timing/Timing.java
new file mode 100644 (file)
index 0000000..59af6b8
--- /dev/null
@@ -0,0 +1,117 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.internal.timing;\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.lang.reflect.Method;\r
+\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.internal.DebugPolicy;\r
+import org.simantics.diagram.synchronization.ErrorHandler;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class Timing {\r
+\r
+    private static Method BEGIN_METHOD;\r
+    private static Method END_METHOD;\r
+\r
+    static {\r
+        // Try to get ThreadLog, if possible.\r
+        try {\r
+            Class<?> logClass = Class.forName("org.simantics.threadlog.ThreadLog");\r
+            Class<?> taskClass = Class.forName("org.simantics.threadlog.Task");\r
+            Method begin = logClass.getMethod("BEGIN", String.class);\r
+            Method end = taskClass.getMethod("end");\r
+\r
+            BEGIN_METHOD = begin;\r
+            END_METHOD = end;\r
+        } catch (ClassNotFoundException e) {\r
+        } catch (NoSuchMethodException e) {\r
+        } catch (SecurityException e) {\r
+            // No dice, just don't do timing then.\r
+        }\r
+    }\r
+\r
+    public static void timed(String label, GTask r) throws DatabaseException {\r
+        Object t = Timing.BEGIN(label);\r
+        try {\r
+            long start = 0, end = 0;\r
+            if (DebugPolicy.PERFORM_TIMING)\r
+                start = System.nanoTime();\r
+            r.run();\r
+            if (DebugPolicy.PERFORM_TIMING) {\r
+                end = System.nanoTime();\r
+                System.out.println("TIMED " + ((end-start)*1e-6) + "ms for " + label);\r
+            }\r
+        } finally {\r
+            Timing.END(t);\r
+        }\r
+    }\r
+\r
+    public static void safeTimed(ErrorHandler errorHandler, String label, GTask r) {\r
+        Object t = Timing.BEGIN(label);\r
+        try {\r
+            long start = 0, end = 0;\r
+            if (DebugPolicy.PERFORM_TIMING)\r
+                start = System.nanoTime();\r
+            try {\r
+                r.run();\r
+            } catch (DatabaseException e) {\r
+                errorHandler.error(e.getMessage(), e);\r
+            }\r
+            if (DebugPolicy.PERFORM_TIMING) {\r
+                end = System.nanoTime();\r
+                System.out.println("TIMED " + ((end-start)*1e-6) + "ms for " + label);\r
+            }\r
+        } finally {\r
+            Timing.END(t);\r
+        }\r
+    }\r
+\r
+    public static Object BEGIN(String name) {\r
+        if (DebugPolicy.PERFORM_TIMING) {\r
+            //return ThreadLog.BEGIN(name);\r
+            if (BEGIN_METHOD != null) {\r
+                try {\r
+                    return BEGIN_METHOD.invoke(null, name);\r
+                } catch (IllegalArgumentException e) {\r
+                    e.printStackTrace();\r
+                } catch (IllegalAccessException e) {\r
+                    e.printStackTrace();\r
+                } catch (InvocationTargetException e) {\r
+                    e.printStackTrace();\r
+                }\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+    public static void END(Object task) {\r
+        if (DebugPolicy.PERFORM_TIMING) {\r
+            //((Task) task).end();\r
+            if (task != null) {\r
+                try {\r
+                    END_METHOD.invoke(task);\r
+                } catch (IllegalArgumentException e) {\r
+                    e.printStackTrace();\r
+                } catch (IllegalAccessException e) {\r
+                    e.printStackTrace();\r
+                } catch (InvocationTargetException e) {\r
+                    e.printStackTrace();\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+}\r