1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.internal.timing;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.diagram.internal.DebugPolicy;
19 import org.simantics.diagram.synchronization.ErrorHandler;
22 * @author Tuukka Lehtonen
24 public final class Timing {
26 private static Method BEGIN_METHOD;
27 private static Method END_METHOD;
30 // Try to get ThreadLog, if possible.
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");
39 } catch (ClassNotFoundException e) {
40 } catch (NoSuchMethodException e) {
41 } catch (SecurityException e) {
42 // No dice, just don't do timing then.
46 public static void timed(String label, GTask r) throws DatabaseException {
47 Object t = Timing.BEGIN(label);
49 long start = 0, end = 0;
50 if (DebugPolicy.PERFORM_TIMING)
51 start = System.nanoTime();
53 if (DebugPolicy.PERFORM_TIMING) {
54 end = System.nanoTime();
55 System.out.println("TIMED " + ((end-start)*1e-6) + "ms for " + label);
62 public static void safeTimed(ErrorHandler errorHandler, String label, GTask r) {
63 Object t = Timing.BEGIN(label);
65 long start = 0, end = 0;
66 if (DebugPolicy.PERFORM_TIMING)
67 start = System.nanoTime();
70 } catch (DatabaseException e) {
71 errorHandler.error(e.getMessage(), e);
73 if (DebugPolicy.PERFORM_TIMING) {
74 end = System.nanoTime();
75 System.out.println("TIMED " + ((end-start)*1e-6) + "ms for " + label);
82 public static Object BEGIN(String name) {
83 if (DebugPolicy.PERFORM_TIMING) {
84 //return ThreadLog.BEGIN(name);
85 if (BEGIN_METHOD != null) {
87 return BEGIN_METHOD.invoke(null, name);
88 } catch (IllegalArgumentException e) {
90 } catch (IllegalAccessException e) {
92 } catch (InvocationTargetException e) {
100 public static void END(Object task) {
101 if (DebugPolicy.PERFORM_TIMING) {
102 //((Task) task).end();
105 END_METHOD.invoke(task);
106 } catch (IllegalArgumentException e) {
108 } catch (IllegalAccessException e) {
110 } catch (InvocationTargetException e) {