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=59af6b86393f387b8957bd6d0a04be829b030d38;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;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 new file mode 100644 index 000000000..59af6b863 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/internal/timing/Timing.java @@ -0,0 +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(); + } + } + } + } + +}