Re: Get bytes sent to client - Mailing list pgsql-jdbc

From Alexander Pyhalov
Subject Re: Get bytes sent to client
Date
Msg-id 4B2B228C.8070008@rsu.ru
Whole thread Raw
In response to Re: Get bytes sent to client  (Craig Ringer <craig@postnewspapers.com.au>)
Responses Re: Get bytes sent to client
List pgsql-jdbc
I've made patch for Postgres JDBC driver. Now driver accounts bytes
sent/received from client ing PGStream. I hope I didn't miss anything. I
made several tests. tcpdump shows about 10% overhead, but results are
quite similar. I think that overhead is caused by auxiliary data
(packets' headers and so on).

Craig Ringer wrote:
> On 17/12/2009 12:56 AM, Alexander Pyhalov wrote:
>> Hello.
>> Thanks for answer. I used dumpster in my tests. However, executing
>> system tools from lava is not a brilliant idea. And I have to collect
>> this statistics dynamically at runtime :(
>
> It sounds like you'll need to modify the JDBC driver to add the hooks
> you need, then.
>
> Perhaps if you can raise it with the JDBC folks (wherever they hang out)
> and talk it over first, you might be able to produce a patch that
> doesn't affect people who don't use the feature, so they might accept
> into the main JDBC driver.
>
> --
> Craig Ringer


--
С уважением,
Александр Пыхалов,
системный администратор ЮГИНФО ЮФУ.
diff -ur postgresql-jdbc-8.4-701.src/org/postgresql/core/BaseConnection.java
postgresql-jdbc-8.4-701.src_modified/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_modified/org/postgresql/core/BaseConnection.java    2009-12-17 21:47:47.000000000 +0300
@@ -139,4 +139,8 @@

     // Get the bind-string-as-varchar config flag
     public boolean getStringVarcharFlag();
+
+    public int getRecvdBytes();
+
+    public int getSentBytes();
 }
diff -ur postgresql-jdbc-8.4-701.src/org/postgresql/core/PGStream.java
postgresql-jdbc-8.4-701.src_modified/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_modified/org/postgresql/core/PGStream.java    2009-12-17 21:31:00.000000000 +0300
@@ -46,6 +46,8 @@
     private Encoding encoding;
     private Writer encodingWriter;

+    protected int sentBytes;
+    protected int recvdBytes;
     /**
      * Constructor:  Connect to the PostgreSQL back end and return
      * a stream connection.
@@ -56,6 +58,7 @@
      */
     public PGStream(String host, int port) throws IOException
     {
+        sentBytes=recvdBytes=0;
         this.host = host;
         this.port = port;

@@ -78,6 +81,15 @@
         return connection;
     }

+    public int getSentBytes()
+    {
+        return sentBytes;
+    }
+
+    public int getRecvdBytes()
+    {
+        return recvdBytes;
+    }
     /**
      * Check for pending backend messages without blocking.
      * Might return false when there actually are messages
@@ -172,6 +184,7 @@
     public void SendChar(int val) throws IOException
     {
         pg_output.write(val);
+        sentBytes+=1;
     }

     /**
@@ -187,6 +200,7 @@
         _int4buf[2] = (byte)(val >>> 8);
         _int4buf[3] = (byte)(val);
         pg_output.write(_int4buf);
+        sentBytes+=4;
     }

     /**
@@ -203,6 +217,7 @@
         _int2buf[0] = (byte)(val >>> 8);
         _int2buf[1] = (byte)val;
         pg_output.write(_int2buf);
+        sentBytes+=2;
     }

     /**
@@ -214,6 +229,7 @@
     public void Send(byte buf[]) throws IOException
     {
         pg_output.write(buf);
+        sentBytes+=buf.length;
     }

     /**
@@ -246,6 +262,7 @@
         {
             pg_output.write(0);
         }
+        sentBytes+= siz;
     }

     /**
@@ -259,6 +276,7 @@
         int c = pg_input.read();
         if (c < 0)
             throw new EOFException();
+        recvdBytes++;
         return c;
     }

@@ -272,7 +290,7 @@
     {
         if (pg_input.read(_int4buf) != 4)
             throw new EOFException();
-
+        recvdBytes+=4;
         return (_int4buf[0] & 0xFF) << 24 | (_int4buf[1] & 0xFF) << 16 | (_int4buf[2] & 0xFF) << 8 | _int4buf[3] &
0xFF;
     }

@@ -286,7 +304,7 @@
     {
         if (pg_input.read(_int2buf) != 2)
             throw new EOFException();
-
+        recvdBytes+=2;
         return (_int2buf[0] & 0xFF) << 8 | _int2buf[1] & 0xFF;
     }

@@ -304,6 +322,7 @@
         String res = encoding.decode(pg_input.getBuffer(), pg_input.getIndex(),
                                      len);
         pg_input.skip(len);
+        recvdBytes+=len;
         return res;
     }

@@ -320,6 +339,7 @@
         String res = encoding.decode(pg_input.getBuffer(), pg_input.getIndex(),
                                      len - 1);
         pg_input.skip(len);
+        recvdBytes+=len;
         return res;
     }

@@ -447,6 +467,7 @@
                 throw new EOFException();
             s += w;
         }
+        recvdBytes+=siz;
     }

     public void Skip(int size) throws IOException {
@@ -454,6 +475,7 @@
         while (s < size) {
             s += pg_input.skip(size - s);
         }
+        recvdBytes+=size;
     }


diff -ur postgresql-jdbc-8.4-701.src/org/postgresql/core/ProtocolConnection.java
postgresql-jdbc-8.4-701.src_modified/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_modified/org/postgresql/core/ProtocolConnection.java    2009-12-17 21:48:01.000000000
+0300
@@ -132,4 +132,8 @@
      * @return the version of the implementation
      */
     public int getProtocolVersion();
+
+    public int getRecvdBytes();
+
+    public int getSentBytes();
 }
diff -ur postgresql-jdbc-8.4-701.src/org/postgresql/core/v2/ProtocolConnectionImpl.java
postgresql-jdbc-8.4-701.src_modified/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_modified/org/postgresql/core/v2/ProtocolConnectionImpl.java    2009-12-17
21:49:51.000000000+0300 
@@ -218,4 +218,18 @@
     private final String database;
     private final QueryExecutorImpl executor;
     private final Logger logger;
+
+    public int getRecvdBytes() {
+        if(pgStream==null)
+            return 0;
+        else
+            return pgStream.getRecvdBytes();
+    }
+
+    public int getSentBytes() {
+        if(pgStream==null)
+            return 0;
+        else
+            return pgStream.getSentBytes();
+    }
 }
diff -ur postgresql-jdbc-8.4-701.src/org/postgresql/core/v3/ProtocolConnectionImpl.java
postgresql-jdbc-8.4-701.src_modified/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_modified/org/postgresql/core/v3/ProtocolConnectionImpl.java    2009-12-17
21:50:21.000000000+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 -ur postgresql-jdbc-8.4-701.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java
postgresql-jdbc-8.4-701.src_modified/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_modified/org/postgresql/jdbc2/AbstractJdbc2Connection.java    2009-12-17
21:52:26.000000000+0300 
@@ -1068,4 +1068,18 @@
             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();
+    }
 }

pgsql-jdbc by date:

Previous
From: Craig Ringer
Date:
Subject: Re: Get bytes sent to client
Next
From: Craig Ringer
Date:
Subject: Re: Get bytes sent to client