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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.internal.timing;
\r
14 import java.lang.reflect.InvocationTargetException;
\r
15 import java.lang.reflect.Method;
\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
22 * @author Tuukka Lehtonen
\r
24 public final class Timing {
\r
26 private static Method BEGIN_METHOD;
\r
27 private static Method END_METHOD;
\r
30 // Try to get ThreadLog, if possible.
\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
37 BEGIN_METHOD = begin;
\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
46 public static void timed(String label, GTask r) throws DatabaseException {
\r
47 Object t = Timing.BEGIN(label);
\r
49 long start = 0, end = 0;
\r
50 if (DebugPolicy.PERFORM_TIMING)
\r
51 start = System.nanoTime();
\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
62 public static void safeTimed(ErrorHandler errorHandler, String label, GTask r) {
\r
63 Object t = Timing.BEGIN(label);
\r
65 long start = 0, end = 0;
\r
66 if (DebugPolicy.PERFORM_TIMING)
\r
67 start = System.nanoTime();
\r
70 } catch (DatabaseException e) {
\r
71 errorHandler.error(e.getMessage(), e);
\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
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
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
100 public static void END(Object task) {
\r
101 if (DebugPolicy.PERFORM_TIMING) {
\r
102 //((Task) task).end();
\r
103 if (task != null) {
\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