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