X-Original-To: pgsql-jdbc-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id C32149FA002 for ; Sat, 16 Sep 2006 07:08:38 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 83830-09 for ; Sat, 16 Sep 2006 10:08:33 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp.ini.unizh.ch (dns.ini.unizh.ch [130.60.230.185]) by postgresql.org (Postfix) with ESMTP id 2CBC09F9FB2 for ; Sat, 16 Sep 2006 07:08:32 -0300 (ADT) Received: from smtp.ini.unizh.ch (localhost [127.0.0.1]) by smtp.ini.unizh.ch (Postfix) with ESMTP id 98BC085C2C1 for ; Sat, 16 Sep 2006 12:08:31 +0200 (CEST) Received: by smtp.ini.unizh.ch (Postfix, from userid 499) id 8C9F785C79A; Sat, 16 Sep 2006 12:08:31 +0200 (CEST) Received: from acardona.lan.ini.unizh.ch (unknown [172.16.2.13]) (using TLSv1 with cipher EXP1024-RC4-SHA (56/128 bits)) (No client certificate requested) by smtp.ini.unizh.ch (Postfix) with ESMTP id 0425785C2C1 for ; Sat, 16 Sep 2006 12:08:28 +0200 (CEST) From: Albert Cardona To: pgsql-jdbc@postgresql.org Subject: Re: about monitoring the input stream Date: Sat, 16 Sep 2006 12:08:43 +0200 User-Agent: KMail/1.9.1 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200609161208.43680.acardona@ini.phys.ethz.ch> X-Virus-Scanned: ClamAV using ClamSMTP X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200609/62 X-Sequence-Number: 15780 Mark, The relevant parts of the code for monitoring the PGStream are attached below. This code is part of TrakEM2, an ImageJ/postgresql -based application (GPL applies) for managing an arbitrarily large set of images, segmented profiles and metadata in general, for the purpose of extracting 3D models and a hierarchical structure of objects present in the sample represented by the images. See all details here: http://www.ini.unizh.ch/~acardona/trakem2.html The code below belongs to the current svn snapshot, which won't be available until the end of this month. /** Extract from private inner class Monitor. GPL applies, see the TrakEM2-src.zip file, class ini.trakem2.persistence.DBLoader, at http://www.ini.unizh.ch/~acardona/trakem2.html */ public Monitor(Connection con) { connection = con; LoggingInputStream lis = null; try { AbstractJdbc2Connection a2 = (AbstractJdbc2Connection)connection; Class c2 = connection.getClass().getSuperclass().getSuperclass(); java.lang.reflect.Field f_proto = c2.getDeclaredField("protoConnection"); f_proto.setAccessible(true); // protoConnection is a ProtocolConnection interface, implemented in core.v3.ProtocolConnectionImpl ! //ProtocolConnectionImpl pci = (ProtocolConnectionImpl)m_proto.get(c2); // class is private to the package, can't cast! Object pci = f_proto.get(a2); // finally, get the PGStream java.lang.reflect.Field f_pgstream = pci.getClass().getDeclaredField("pgStream"); f_pgstream.setAccessible(true); PGStream pgstream = (PGStream)f_pgstream.get(pci); // now the InputStream java.lang.reflect.Field f_i = pgstream.getClass().getDeclaredField("pg_input"); f_i.setAccessible(true); InputStream stream = (InputStream)f_i.get(pgstream); lis = new LoggingInputStream(stream); f_i.set(pgstream, lis); // TADA! Many thanks to the PGSQL JDBC mailing list for this last tip on not just monitoring the PGStream as I was doing, but on replacing the inputstream altogether with a logging copy! ("CountingInputStream", they called it). } catch (Exception e) { new IJError(e); } this.lis = lis; makeWindow(); } /** ===================== */ /** The class below exists as ini.trakem2.io.LoggingInputStream in TrakEM2. The GPL applies, see the TrakEM2-src.zip downloadable at http://www.ini.unizh.ch/~acardona/trakem2.html */ import java.io.BufferedInputStream; import java.io.InputStream; import java.io.IOException; /** A class to monitor an input stream for speed and total byte download. */ public class LoggingInputStream extends BufferedInputStream { private long last; private long n = 0; private long accum_time = 0; private long accum_bytes = 0; public LoggingInputStream(InputStream in) { super(in); last = System.currentTimeMillis(); } public int read() throws IOException { int m = super.read(); n += m; return m; } public int read(byte[] b) throws IOException { int m = super.read(b); n += m; return m; } public int read(byte[] b, int off, int len) throws IOException { int m = super.read(b, off, len); n += m; return m; } /** Put the accumulated count to zero. */ public void resetInfo() { // to work perfect, this would need a synchronized clause, but no such perfection is needed, and there are perfomance issues. accum_bytes = n = 0; last = System.currentTimeMillis(); accum_time = 0; } /** Returns info as * [0] = current time in ms * [1] = elapsed time in ms since last call to getInfo(long[]) * [2] = n_bytes_read since last call to getInfo(long[]) * [3] = accumulated time in ms since last call to resetInfo() * [4] = accumulated bytes since last call to resetInfo() * * So current speed = info[2]/info[1] Kb/s */ public void getInfo(long[] info) { long now = System.currentTimeMillis(); accum_time += now - last; accum_bytes += n; info[0] = now; info[1] = now - last; // elapsed time info[2] = n; info[3] = accum_time; // total time since last call to resetInfo() info[4] = accum_bytes; // total bytes since last call to resetInfo() // reset cycle vars: n = 0; last = now; } } -- Albert Cardona Molecular Cell Developmental Biology University of California Los Angeles Tel +1 310 2067376 Programming: http://www.ini.unizh.ch/~acardona/trakem2.html Research: http://www.mcdb.ucla.edu/Research/Hartenstein/ Web design: http://www.pixelets.com