pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Pyhalov <alp@rsu.ru>
To: Craig Ringer <craig@postnewspapers.com.au>
Cc: dmp <danap@ttc-cmc.net>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Get bytes sent to client
Date: Mon, 21 Dec 2009 12:19:52 +0300
Message-ID: <4B2F3DB8.5080404@rsu.ru> (raw)
In-Reply-To: <4B2CA14F.6030305@rsu.ru>
References: <4B2894E7.80407@rsu.ru>
	<4B28A86A.3060506@rsu.ru>
	<4B290928.5080004@ttc-cmc.net>
	<4B291141.90503@rsu.ru>
	<4B29D2A3.6050504@postnewspapers.com.au>
	<4B2B228C.8070008@rsu.ru>
	<4B2B366C.7010304@postnewspapers.com.au>
	<4B2CA14F.6030305@rsu.ru>

Hello.
Extending VisibleBufferedInputStream was not a good idea - work with 
buffer (like ensureBytes() calls ) can't be accounted. So, I modified 
it. Results are more precise now.

Alexander Pyhalov wrote:
> Good day..
> I've just made new patch - extended BufferedOutputStream and 
> VisibleBufferedInputStream, as you suggested. It works, and results are 
> comparable with tcpdump data (for received traffic). For sent traffic 
> they are incorrect, because I can't account all ACK, but only PGSQL 
> traffic.
> It's suprising, that received traffic is more then tcpdump shows ( 2%, 
> butt more). I thought it would be a bit smaller then actual data.
> However, this data gives good approximation (2% for 100MB traffic is not 
> a lot).
> 
> Craig Ringer wrote:
>> On 18/12/2009 2:34 PM, Alexander Pyhalov wrote:
>>>
>>> I've made patch for Postgres JDBC driver. Now driver accounts bytes
>>> sent/received from client ing PGStream.
>>
>> Can't you extend or wrap the pg_input / pg_output streams to do the 
>> accounting in their read(...) / write(...) method instead? The way 
>> you've done it is rather fragile and is very prone to bitrot as other 
>> parts of the driver are changed.
>>
>> -- 
>> Craig Ringer
> 
> 
> 
> ------------------------------------------------------------------------
> 
> 


-- 
С уважением,
Александр Пыхалов,
системный администратор ЮГИНФО ЮФУ.

Attachments:

patch.pgsql.3 (text/plain, 8.3K) download diff -urN postgresql-jdbc-8.4-701.src/org/postgresql/core/BaseConnection.java postgresql-jdbc-8.4-701.src_mod2/org/postgresql/core/BaseConnection.java --- postgresql-jdbc-8.4-701.src/org/postgresql/core/BaseConnection.java 2008-04-15 08:23:54.000000000 +0400 +++ postgresql-jdbc-8.4-701.src_mod2/org/postgresql/core/BaseConnection.java 2009-12-19 12:22:19.707859272 +0300 @@ -139,4 +139,8 @@ // Get the bind-string-as-varchar config flag public boolean getStringVarcharFlag(); + + public int getSentBytes(); + + public int getRecvdBytes(); } diff -Nru postgresql-jdbc-8.4-701.src/org/postgresql/core/CalculatingBOutputStream.java postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/CalculatingBOutputStream.java --- postgresql-jdbc-8.4-701.src/org/postgresql/core/CalculatingBOutputStream.java 1970-01-01 03:00:00.000000000 +0300 +++ postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/CalculatingBOutputStream.java 2009-12-21 11:46:45.075261312 +0300 @@ -0,0 +1,58 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.postgresql.core; + +import java.io.BufferedOutputStream; +import java.io.OutputStream; +import java.io.IOException; +/** + * + * @author alp + */ +public class CalculatingBOutputStream extends BufferedOutputStream{ + + protected int sentBytes; + + public CalculatingBOutputStream(OutputStream out){ + super(out); + sentBytes=0; + } + + public CalculatingBOutputStream(OutputStream out, int size){ + super(out,size); + sentBytes=0; + } + + /** + * {@inheritDoc} + */ + public void write(int b) throws IOException{ + super.write(b); + sentBytes++; + } + + /** + * {@inheritDoc} + */ + public void write(byte[] b) throws IOException{ + super.write(b); + sentBytes+=b.length; + } + + public void write(byte[] b, int off, int len) throws IOException{ + super.write(b,off,len); + sentBytes+=len; + } + + /** + * Get number of bytes sent to this stream + * @return - number of bytes sent + */ + public int getSentBytes() + { + return sentBytes; + } +} diff -Nru postgresql-jdbc-8.4-701.src/org/postgresql/core/PGStream.java postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/PGStream.java --- postgresql-jdbc-8.4-701.src/org/postgresql/core/PGStream.java 2008-01-08 09:56:27.000000000 +0300 +++ postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/PGStream.java 2009-12-21 11:46:45.067081118 +0300 @@ -109,7 +109,7 @@ // Buffer sizes submitted by Sverre H Huseby <sverrehu@online.no> pg_input = new VisibleBufferedInputStream(connection.getInputStream(), 8192); - pg_output = new BufferedOutputStream(connection.getOutputStream(), 8192); + pg_output = new CalculatingBOutputStream(connection.getOutputStream(), 8192); if (encoding != null) setEncoding(encoding); @@ -534,4 +534,23 @@ pg_input.close(); connection.close(); } + + public int getRecvdBytes() { + if(pg_input!=null){ + return pg_input.getRecvdBytes(); + } + else { + return 0; + } + } + + public int getSentBytes() { + if(pg_output!=null){ + return ((CalculatingBOutputStream)pg_output).getSentBytes(); + } + else { + return 0; + } + } + } diff -Nru postgresql-jdbc-8.4-701.src/org/postgresql/core/ProtocolConnection.java postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/ProtocolConnection.java --- postgresql-jdbc-8.4-701.src/org/postgresql/core/ProtocolConnection.java 2008-01-08 09:56:27.000000000 +0300 +++ postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/ProtocolConnection.java 2009-12-21 11:46:45.075261312 +0300 @@ -132,4 +132,8 @@ * @return the version of the implementation */ public int getProtocolVersion(); + + public int getRecvdBytes(); + + public int getSentBytes(); } diff -Nru postgresql-jdbc-8.4-701.src/org/postgresql/core/VisibleBufferedInputStream.java postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/VisibleBufferedInputStream.java --- postgresql-jdbc-8.4-701.src/org/postgresql/core/VisibleBufferedInputStream.java 2008-01-08 09:56:27.000000000 +0300 +++ postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/VisibleBufferedInputStream.java 2009-12-21 11:46:45.074235505 +0300 @@ -55,6 +55,11 @@ private int endIndex; /** + * How many bytes was read from wrapped stream + */ + protected int rcvdBytes; + + /** * Creates a new buffer around the given stream. * * @param in The stream to buffer. @@ -64,6 +69,7 @@ wrapped = in; buffer = new byte[bufferSize < MINIMUM_READ ? MINIMUM_READ : bufferSize]; + rcvdBytes=0; } /** @@ -135,7 +141,8 @@ int read = wrapped.read(buffer, endIndex, canFit); if (read < 0) { return false; - } + } + rcvdBytes +=read; endIndex += read; return true; } @@ -210,6 +217,7 @@ if (r <= 0) { return (read == 0) ? r : read; } + rcvdBytes+=r; read += r; off += r; len -= r; @@ -222,6 +230,7 @@ * {@inheritDoc} */ public long skip(long n) throws IOException { + long skipped; int avail = endIndex - index; if (avail >= n) { index += n; @@ -230,7 +239,9 @@ n -= avail; index = 0; endIndex = 0; - return avail + wrapped.skip(n); + skipped=wrapped.skip(n); + rcvdBytes+=skipped; + return avail + skipped; } /** @@ -290,4 +301,10 @@ pos = index; } } + + public int getRecvdBytes() + { + return rcvdBytes; + } + } diff -Nru postgresql-jdbc-8.4-701.src/org/postgresql/core/v2/ProtocolConnectionImpl.java postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/v2/ProtocolConnectionImpl.java --- postgresql-jdbc-8.4-701.src/org/postgresql/core/v2/ProtocolConnectionImpl.java 2008-04-01 11:19:20.000000000 +0400 +++ postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/v2/ProtocolConnectionImpl.java 2009-12-21 11:46:45.050828561 +0300 @@ -200,6 +200,20 @@ { return 2; } + + public int getRecvdBytes() { + if(pgStream==null) + return 0; + else + return pgStream.getRecvdBytes(); + } + + public int getSentBytes() { + if(pgStream==null) + return 0; + else + return pgStream.getSentBytes(); + } private String serverVersion; private int cancelPid; diff -Nru postgresql-jdbc-8.4-701.src/org/postgresql/core/v3/ProtocolConnectionImpl.java postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/v3/ProtocolConnectionImpl.java --- postgresql-jdbc-8.4-701.src/org/postgresql/core/v3/ProtocolConnectionImpl.java 2008-04-01 11:19:20.000000000 +0400 +++ postgresql-jdbc-8.4-701.src_mod3/org/postgresql/core/v3/ProtocolConnectionImpl.java 2009-12-21 11:46:45.051828667 +0300 @@ -200,6 +200,20 @@ return 3; } + public int getRecvdBytes() { + if(pgStream==null) + return 0; + else + return pgStream.getRecvdBytes(); + } + + public int getSentBytes() { + if(pgStream==null) + return 0; + else + return pgStream.getSentBytes(); + } + private String serverVersion; private int cancelPid; private int cancelKey; diff -Nru postgresql-jdbc-8.4-701.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java postgresql-jdbc-8.4-701.src_mod3/org/postgresql/jdbc2/AbstractJdbc2Connection.java --- postgresql-jdbc-8.4-701.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2009-07-01 09:00:40.000000000 +0400 +++ postgresql-jdbc-8.4-701.src_mod3/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2009-12-21 11:46:45.091416093 +0300 @@ -1068,4 +1068,19 @@ copyManager = new CopyManager(this); return copyManager; } + + public int getRecvdBytes() { + if(protoConnection==null) + return 0; + else + return protoConnection.getRecvdBytes(); + } + + public int getSentBytes() { + if(protoConnection==null) + return 0; + else + return protoConnection.getSentBytes(); + } + }

view thread (10+ messages)

Message-ID: <4B2F3DB8.5080404@rsu.ru>
Permalink:  ../4B2F3DB8.5080404@rsu.ru/
Also on:    postgresql.org/message-id/4B2F3DB8.5080404@rsu.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: alp@rsu.ru, craig@postnewspapers.com.au, danap@ttc-cmc.net
  Subject: Re: Get bytes sent to client
  In-Reply-To: <4B2F3DB8.5080404@rsu.ru>
  References: <4B2894E7.80407@rsu.ru> <4B28A86A.3060506@rsu.ru> <4B290928.5080004@ttc-cmc.net> <4B291141.90503@rsu.ru> <4B29D2A3.6050504@postnewspapers.com.au> <4B2B228C.8070008@rsu.ru> <4B2B366C.7010304@postnewspapers.com.au> <4B2CA14F.6030305@rsu.ru> <4B2F3DB8.5080404@rsu.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox