Skip to content

Instantly share code, notes, and snippets.

@superbob
Created June 17, 2013 08:08
Show Gist options
  • Select an option

  • Save superbob/5795355 to your computer and use it in GitHub Desktop.

Select an option

Save superbob/5795355 to your computer and use it in GitHub Desktop.

Revisions

  1. superbob created this gist Jun 17, 2013.
    18 changes: 18 additions & 0 deletions SpyInstanciations.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    TimeSpy<Boolean> rsSpy = new TimeSpy<Boolean>() {
    protected Boolean run() throws Exception {
    return resultSet.next();
    }
    };

    TimeSpy<Boolean> statementSpy = new TimeSpy<Boolean>() {
    protected Boolean run() throws Exception {
    return statement.execute();
    }
    };

    TimeSpy<Boolean> commitSpy = new TimeSpy<Boolean>() {
    protected Boolean run() throws Exception {
    connection.commit();
    return true;
    }
    };
    4 changes: 4 additions & 0 deletions SpyResult.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    long t1 = rsSpy.getTime()/1000000;
    long t2 = statementSpy.getTime()/1000000;
    long t3 = commitSpy.getTime()/1000000;
    log.info("Ellapsed time : ResultSet.next() : " + t1 + "ms, Statement.execute() : " + t2 + "ms, Connextion.commit() : " + t3 + "ms.");
    3 changes: 3 additions & 0 deletions SpyRuns.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    rsSpy.doIt()
    statementSpy.doIt();
    commitSpy.doIt();
    21 changes: 21 additions & 0 deletions TimeSpy.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    private static abstract class TimeSpy<T> {
    private long time = 0;

    protected abstract T run() throws Exception;

    public T doIt() throws InvocationTargetException {
    time -= System.nanoTime();
    T retVal;
    try {
    retVal = run();
    } catch (Exception e) {
    throw new InvocationTargetException(e);
    }
    time += System.nanoTime();
    return retVal;
    }

    public long getTime() {
    return time;
    }
    }