X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Finternal%2Ftiming%2FTiming.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Finternal%2Ftiming%2FTiming.java;h=06a1c6df71ce419625489da7272518b2c69a99ff;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=59af6b86393f387b8957bd6d0a04be829b030d38;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git 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 index 59af6b863..06a1c6df7 100644 --- 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 @@ -1,117 +1,117 @@ -/******************************************************************************* - * 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(); - } - } - } - } - -} +/******************************************************************************* + * 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(); + } + } + } + } + +}