Debian Codegraph @

minidns-repl/src/main/java/org/minidns/minidnsrepl/MiniDnsStats.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.minidnsrepl;
12
13 import static java.lang.System.out;
14
15 import java.io.IOException;
16 import java.util.Arrays;
17
18 import org.minidns.AbstractDnsClient;
19 import org.minidns.DnsCache;
20 import org.minidns.dnsqueryresult.DnsQueryResult;
21 import org.minidns.dnssec.DnssecClient;
22 import org.minidns.integrationtest.IntegrationTestTools;
23 import org.minidns.integrationtest.IntegrationTestTools.CacheConfig;
24 import org.minidns.record.Record.TYPE;
25 import org.minidns.source.NetworkDataSourceWithAccounting;
26
27 public class MiniDnsStats {
28
29 public static void main(String[] args) throws IOException {
30 showDnssecStats();
31 }
32
33 public static void showDnssecStats() throws IOException {
34 showDnssecStats("siccegge.de", TYPE.A);
35 }
36
37 public static void showDnssecStats(String name, TYPE type) throws IOException {
38 DnssecClient client;
39
40 client = getClient(CacheConfig.without);
41 // CHECKSTYLE:OFF
42 out.println(gatherStatsFor(client, "Without Cache", name, type));
43 // CHECKSTYLE:ON
44
45 client = getClient(CacheConfig.normal);
46 // CHECKSTYLE:OFF
47 out.println(gatherStatsFor(client, "With Cache", name, type));
48 // CHECKSTYLE:ON
49
50 client = getClient(CacheConfig.extended);
51 // CHECKSTYLE:OFF
52 out.println(gatherStatsFor(client, "With Extended Cache", name, type));
53 // CHECKSTYLE:ON
54
55 client = getClient(CacheConfig.full);
56 // CHECKSTYLE:OFF
57 out.println(gatherStatsFor(client, "With Full Cache", name, type));
58 // CHECKSTYLE:ON
59 }
60
61 public static StringBuilder gatherStatsFor(DnssecClient client, String testName, String name, TYPE type) throws IOException {
62 DnsQueryResult result;
63 long start, stop;
64
65 start = System.currentTimeMillis();
66 result = client.query(name, type);
67 stop = System.currentTimeMillis();
68
69 StringBuilder sb = new StringBuilder();
70 sb.append(testName).append('\n');
71 char[] headline = new char[testName.length()];
72 Arrays.fill(headline, '#');
73 sb.append(headline).append('\n');
74 sb.append(result).append('\n');
75 sb.append("Took ").append(stop - start).append("ms").append('\n');
76 sb.append(getStats(client)).append('\n');
77 sb.append('\n');
78
79 return sb;
80 }
81
82 public static DnssecClient getClient(CacheConfig cacheConfig) {
83 return IntegrationTestTools.getClient(cacheConfig);
84 }
85
86 public static StringBuilder getStats(AbstractDnsClient client) {
87 StringBuilder sb = new StringBuilder();
88
89 NetworkDataSourceWithAccounting ndswa = NetworkDataSourceWithAccounting.from(client);
90 if (ndswa != null) {
91 sb.append(ndswa.getStats().toString());
92 } else {
93 sb.append("Client is not using " + NetworkDataSourceWithAccounting.class.getSimpleName());
94 }
95
96 DnsCache dnsCache = client.getCache();
97 if (dnsCache != null) {
98 sb.append(dnsCache);
99 } else {
100 sb.append("Client is not using a Cache");
101 }
102
103 return sb;
104 }
105 }