SQL NetworkCostInfo allows you to obtain network overhead data by using some fine-grained APIs.
Related APIs
All the APIs listed in the following table are at the Connection level and require forced type conversion when they are called to obtain detailed time values.
| API | Description | Parameter | Unit of return value |
|---|---|---|---|
| networkStatistics | Specifies whether to enable statistics collection. | true: specifies to enable statistics collection. false: specifies to disable statistics collection. Default value: false. |
N/A |
| getLastPacketCostTime | Obtains the round-trip time of the last packet. | N/A | ms |
| getLastPacketReponseTimestamp | Obtains the packet return timestamp of the last packet. | N/A | ms |
| getLastPacketSendTimestamp | Obtains the sending timestamp of the last packet. | N/A | ms |
| clearNetworkCostInfo | Clears previously cached data. | N/A | N/A |
Examples
public void testCostTime() {
try {
Connection conn = sharedConnection;
((OceanBaseConnection) conn).networkStatistics(true);
PreparedStatement ps = conn
.prepareStatement("insert into " + tableName2 + " values(?)");
long ms = ((OceanBaseConnection) conn).getLastPacketCostTime();
System.out.println("prepareStatement cost info = " + ms + "ms");
/* getNetWorkCostInfo gets the information of the last network transmission, setInt will not perform network transmission, so it needs to be cleaned up*/
((OceanBaseConnection) conn).clearNetworkCostInfo();
ms = ((OceanBaseConnection) conn).getLastPacketCostTime();
ps.setInt(1, 1);
System.out.println("setInt cost info = " + ms + "ms");
ps.execute();
ms = ((OceanBaseConnection) conn).getLastPacketCostTime();
long sendTs = ((OceanBaseConnection) conn).getLastPacketSendTimestamp();
long responseTs = ((OceanBaseConnection) conn).getLastPacketResponseTimestamp();
System.out
.println(" (responseTs - sendTs) + \"m's\" = " + (responseTs - sendTs) + "ms");
System.out.println("execute cost info = " + ms + "ms");
((OceanBaseConnection) conn).networkStatistics(false);
ms = ((OceanBaseConnection) conn).getLastPacketCostTime();
} catch (SQLException e) {
Assert.assertEquals("Cant get network cost info while setNetworkStatisticsFlag(true)",
e.getMessage());
}
}