NettyStats.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.tsi.app.common.xnet;
  2. import io.netty.channel.Channel;
  3. import java.net.InetSocketAddress;
  4. public abstract class NettyStats {
  5. protected Channel channel;
  6. protected long recordReceived;
  7. protected long recordSent;
  8. protected long bytesReceived;
  9. protected long bytesSent;
  10. protected long minLatency;
  11. protected long maxLatency;
  12. protected long count;
  13. protected long totalLatency;
  14. public abstract InetSocketAddress getRemoteSocketAddress();
  15. public NettyStats() {
  16. reset();
  17. }
  18. public synchronized void setChannel(Channel chn) {
  19. channel = chn;
  20. }
  21. public synchronized Channel getChannel() {
  22. return channel;
  23. }
  24. public synchronized void reset() {
  25. recordReceived = 0;
  26. recordSent = 0;
  27. bytesReceived = 0;
  28. bytesSent = 0;
  29. minLatency = Long.MAX_VALUE;
  30. maxLatency = 0;
  31. count = 0;
  32. totalLatency = 0;
  33. }
  34. protected synchronized void updateStats(long start, long end, long receivedBytes, long sentBytes) {
  35. long elapsed = end - start;
  36. if (elapsed < minLatency) {
  37. minLatency = elapsed;
  38. }
  39. if (elapsed > maxLatency) {
  40. maxLatency = elapsed;
  41. }
  42. if (receivedBytes > 0) {
  43. recordReceived++;
  44. bytesReceived += receivedBytes;
  45. }
  46. if (sentBytes > 0) {
  47. recordSent++;
  48. bytesSent += sentBytes;
  49. }
  50. count++;
  51. totalLatency += elapsed;
  52. }
  53. public synchronized long getRecordReceived() {
  54. return recordReceived;
  55. }
  56. public synchronized long getRecordSent() {
  57. return recordSent;
  58. }
  59. public synchronized long getBytesReceived() {
  60. return bytesReceived;
  61. }
  62. public synchronized long getBytesSent() {
  63. return bytesSent;
  64. }
  65. public synchronized long getMinLatency(){
  66. return minLatency == Long.MAX_VALUE ? 0 : minLatency;
  67. }
  68. public synchronized long getMaxLatency(){
  69. return maxLatency;
  70. }
  71. public synchronized long getAvgLatency(){
  72. return count == 0 ? 0 : totalLatency / count;
  73. }
  74. public synchronized long getTotalLatency(){
  75. return totalLatency;
  76. }
  77. public synchronized long getCount(){
  78. return count;
  79. }
  80. }