Debian Codegraph @

minidns-client/src/main/java/org/minidns/source/NetworkDataSourceWithAccounting.java

raw
1 /*
2 * Copyright 2015-2020 the original author or authors
3 *
4 * This software is licensed under the Apache License, Version 2.0,
5 * the GNU Lesser General Public License version 2 or later ("LGPL")
6 * and the WTFPL.
7 * You may choose either license to govern your use of this software only
8 * upon the condition that you accept all of the terms of either
9 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
10 */
11 package org.minidns.source;
12
13 import java.io.IOException;
14 import java.net.InetAddress;
15 import java.util.Locale;
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 import org.minidns.AbstractDnsClient;
19 import org.minidns.dnsmessage.DnsMessage;
20 import org.minidns.dnsqueryresult.StandardDnsQueryResult;
21
22 public class NetworkDataSourceWithAccounting extends NetworkDataSource {
23
24 private final AtomicInteger successfulQueries = new AtomicInteger();
25 private final AtomicInteger responseSize = new AtomicInteger();
26 private final AtomicInteger failedQueries = new AtomicInteger();
27
28 private final AtomicInteger successfulUdpQueries = new AtomicInteger();
29 private final AtomicInteger udpResponseSize = new AtomicInteger();
30 private final AtomicInteger failedUdpQueries = new AtomicInteger();
31
32 private final AtomicInteger successfulTcpQueries = new AtomicInteger();
33 private final AtomicInteger tcpResponseSize = new AtomicInteger();
34 private final AtomicInteger failedTcpQueries = new AtomicInteger();
35
36 @Override
37 public StandardDnsQueryResult query(DnsMessage message, InetAddress address, int port) throws IOException {
38 StandardDnsQueryResult response;
39 try {
40 response = super.query(message, address, port);
41 } catch (IOException e) {
42 failedQueries.incrementAndGet();
43 throw e;
44 }
45
46 successfulQueries.incrementAndGet();
47 responseSize.addAndGet(response.response.toArray().length);
48
49 return response;
50 }
51
52 @Override
53 protected DnsMessage queryUdp(DnsMessage message, InetAddress address, int port) throws IOException {
54 DnsMessage response;
55 try {
56 response = super.queryUdp(message, address, port);
57 } catch (IOException e) {
58 failedUdpQueries.incrementAndGet();
59 throw e;
60 }
61
62 successfulUdpQueries.incrementAndGet();
63 udpResponseSize.addAndGet(response.toArray().length);
64
65 return response;
66 }
67
68 @Override
69 protected DnsMessage queryTcp(DnsMessage message, InetAddress address, int port) throws IOException {
70 DnsMessage response;
71 try {
72 response = super.queryTcp(message, address, port);
73 } catch (IOException e) {
74 failedTcpQueries.incrementAndGet();
75 throw e;
76 }
77
78 successfulTcpQueries.incrementAndGet();
79 tcpResponseSize.addAndGet(response.toArray().length);
80
81 return response;
82 }
83
84 public Stats getStats() {
85 return new Stats(this);
86 }
87
88 public static NetworkDataSourceWithAccounting from(AbstractDnsClient client) {
89 DnsDataSource ds = client.getDataSource();
90 if (ds instanceof NetworkDataSourceWithAccounting) {
91 return (NetworkDataSourceWithAccounting) ds;
92 }
93 return null;
94 }
95
96 public static final class Stats {
97 public final int successfulQueries;
98 public final int responseSize;
99 public final int averageResponseSize;
100 public final int failedQueries;
101
102 public final int successfulUdpQueries;
103 public final int udpResponseSize;
104 public final int averageUdpResponseSize;
105 public final int failedUdpQueries;
106
107 public final int successfulTcpQueries;
108 public final int tcpResponseSize;
109 public final int averageTcpResponseSize;
110 public final int failedTcpQueries;
111
112 private String stringCache;
113
114 private Stats(NetworkDataSourceWithAccounting ndswa) {
115 successfulQueries = ndswa.successfulQueries.get();
116 responseSize = ndswa.responseSize.get();
117 failedQueries = ndswa.failedQueries.get();
118
119 successfulUdpQueries = ndswa.successfulUdpQueries.get();
120 udpResponseSize = ndswa.udpResponseSize.get();
121 failedUdpQueries = ndswa.failedUdpQueries.get();
122
123 successfulTcpQueries = ndswa.successfulTcpQueries.get();
124 tcpResponseSize = ndswa.tcpResponseSize.get();
125 failedTcpQueries = ndswa.failedTcpQueries.get();
126
127 // Calculated stats section
128 averageResponseSize = successfulQueries > 0 ? responseSize / successfulQueries : 0;
129 averageUdpResponseSize = successfulUdpQueries > 0 ? udpResponseSize / successfulUdpQueries : 0;
130 averageTcpResponseSize = successfulTcpQueries > 0 ? tcpResponseSize / successfulTcpQueries : 0;
131 }
132
133 @Override
134 public String toString() {
135 if (stringCache != null)
136 return stringCache;
137
138 StringBuilder sb = new StringBuilder();
139
140 sb.append("Stats\t").append("# Successful").append('\t').append("# Failed").append('\t')
141 .append("Resp. Size").append('\t').append("Avg. Resp. Size").append('\n');
142 sb.append("Total\t").append(toString(successfulQueries)).append('\t').append(toString(failedQueries))
143 .append('\t').append(toString(responseSize)).append('\t').append(toString(averageResponseSize))
144 .append('\n');
145 sb.append("UDP\t").append(toString(successfulUdpQueries)).append('\t').append(toString(failedUdpQueries))
146 .append('\t').append(toString(udpResponseSize)).append('\t')
147 .append(toString(averageUdpResponseSize)).append('\n');
148 sb.append("TCP\t").append(toString(successfulTcpQueries)).append('\t').append(toString(failedTcpQueries))
149 .append('\t').append(toString(tcpResponseSize)).append('\t')
150 .append(toString(averageTcpResponseSize)).append('\n');
151
152 stringCache = sb.toString();
153 return stringCache;
154 }
155
156 private static String toString(int i) {
157 return String.format(Locale.US, "%,09d", i);
158 }
159 }
160 }