Re: Get bytes sent to client
| От | Alexander Pyhalov |
|---|---|
| Тема | Re: Get bytes sent to client |
| Дата | |
| Msg-id | 4B2CA14F.6030305@rsu.ru обсуждение исходный текст |
| Ответ на | Re: Get bytes sent to client (Craig Ringer <craig@postnewspapers.com.au>) |
| Ответы |
Re: Get bytes sent to client
|
| Список | pgsql-jdbc |
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
--
С уважением,
Александр Пыхалов,
системный администратор ЮГИНФО ЮФУ.
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 -urN postgresql-jdbc-8.4-701.src/org/postgresql/core/CalculatingBOutputStream.java
postgresql-jdbc-8.4-701.src_mod2/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_mod2/org/postgresql/core/CalculatingBOutputStream.java 2009-12-19 12:22:19.707859272
+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 -urN postgresql-jdbc-8.4-701.src/org/postgresql/core/CalculatingVBInputStream.java
postgresql-jdbc-8.4-701.src_mod2/org/postgresql/core/CalculatingVBInputStream.java
--- postgresql-jdbc-8.4-701.src/org/postgresql/core/CalculatingVBInputStream.java 1970-01-01 03:00:00.000000000
+0300
+++ postgresql-jdbc-8.4-701.src_mod2/org/postgresql/core/CalculatingVBInputStream.java 2009-12-19 12:22:19.688492698
+0300
@@ -0,0 +1,83 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.postgresql.core;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ *
+ * @author alp
+ */
+public class CalculatingVBInputStream extends VisibleBufferedInputStream {
+
+ protected int rcvdBytes;
+
+ public CalculatingVBInputStream(InputStream in, int bufferSize) {
+ super(in,bufferSize);
+ rcvdBytes=0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public long skip(long n) throws IOException
+ {
+ long skipped;
+
+ skipped=super.skip(n);
+ if(skipped>0)
+ rcvdBytes+=skipped;
+ return skipped;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int read(byte[] b) throws IOException
+ {
+ int read;
+
+ read=super.read(b);
+ if(read>0)
+ rcvdBytes+=read;
+ return read;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int read() throws IOException
+ {
+ int read;
+
+ read=super.read();
+ if(read>0)
+ rcvdBytes++;
+ return read;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int read(byte to[], int off, int len) throws IOException {
+ int read;
+
+ read=super.read(to,off,len);
+ if(read>0)
+ rcvdBytes+=read;
+ return read;
+ }
+
+ /**
+ * Get number of bytes read from this stream
+ * @return - number of bytes read
+ */
+ public int getRecvdBytes()
+ {
+ return rcvdBytes;
+ }
+}
diff -urN postgresql-jdbc-8.4-701.src/org/postgresql/core/PGStream.java
postgresql-jdbc-8.4-701.src_mod2/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_mod2/org/postgresql/core/PGStream.java 2009-12-19 12:22:19.688492698 +0300
@@ -108,8 +108,8 @@
connection.setTcpNoDelay(true);
// 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_input = new CalculatingVBInputStream(connection.getInputStream(), 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 ((CalculatingVBInputStream)pg_input).getRecvdBytes();
+ }
+ else {
+ return 0;
+ }
+ }
+
+ public int getSentBytes() {
+ if(pg_output!=null){
+ return ((CalculatingBOutputStream)pg_output).getSentBytes();
+ }
+ else {
+ return 0;
+ }
+ }
+
}
diff -urN postgresql-jdbc-8.4-701.src/org/postgresql/core/ProtocolConnection.java
postgresql-jdbc-8.4-701.src_mod2/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_mod2/org/postgresql/core/ProtocolConnection.java 2009-12-19 12:22:19.689492804 +0300
@@ -132,4 +132,8 @@
* @return the version of the implementation
*/
public int getProtocolVersion();
+
+ public int getRecvdBytes();
+
+ public int getSentBytes();
}
diff -urN postgresql-jdbc-8.4-701.src/org/postgresql/core/v2/ProtocolConnectionImpl.java
postgresql-jdbc-8.4-701.src_mod2/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_mod2/org/postgresql/core/v2/ProtocolConnectionImpl.java 2009-12-19
12:22:19.685493219+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 -urN postgresql-jdbc-8.4-701.src/org/postgresql/core/v3/ProtocolConnectionImpl.java
postgresql-jdbc-8.4-701.src_mod2/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_mod2/org/postgresql/core/v3/ProtocolConnectionImpl.java 2009-12-19
12:22:19.686493045+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 -urN postgresql-jdbc-8.4-701.src/org/postgresql/jdbc2/AbstractJdbc2Connection.java
postgresql-jdbc-8.4-701.src_mod2/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_mod2/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2009-12-19 12:22:19.715864030
+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();
+ }
+
}
В списке pgsql-jdbc по дате отправления: