001/*
002 * Copyright 2015-2024 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.minidnsrepl;
012
013import static java.lang.System.out;
014
015import java.io.IOException;
016import java.util.Arrays;
017
018import org.minidns.AbstractDnsClient;
019import org.minidns.DnsCache;
020import org.minidns.dnsqueryresult.DnsQueryResult;
021import org.minidns.dnssec.DnssecClient;
022import org.minidns.integrationtest.IntegrationTestTools;
023import org.minidns.integrationtest.IntegrationTestTools.CacheConfig;
024import org.minidns.record.Record.TYPE;
025import org.minidns.source.NetworkDataSourceWithAccounting;
026
027public class MiniDnsStats {
028
029    public static void main(String[] args) throws IOException {
030        showDnssecStats();
031    }
032
033    public static void showDnssecStats() throws IOException {
034        showDnssecStats("siccegge.de", TYPE.A);
035    }
036
037    public static void showDnssecStats(String name, TYPE type) throws IOException {
038        DnssecClient client;
039
040        client = getClient(CacheConfig.without);
041        // CHECKSTYLE:OFF
042        out.println(gatherStatsFor(client, "Without Cache", name, type));
043        // CHECKSTYLE:ON
044
045        client = getClient(CacheConfig.normal);
046        // CHECKSTYLE:OFF
047        out.println(gatherStatsFor(client, "With Cache", name, type));
048        // CHECKSTYLE:ON
049
050        client = getClient(CacheConfig.extended);
051        // CHECKSTYLE:OFF
052        out.println(gatherStatsFor(client, "With Extended Cache", name, type));
053        // CHECKSTYLE:ON
054
055        client = getClient(CacheConfig.full);
056        // CHECKSTYLE:OFF
057        out.println(gatherStatsFor(client, "With Full Cache", name, type));
058        // CHECKSTYLE:ON
059    }
060
061    public static StringBuilder gatherStatsFor(DnssecClient client, String testName, String name, TYPE type) throws IOException {
062        DnsQueryResult result;
063        long start, stop;
064
065        start = System.currentTimeMillis();
066        result = client.query(name, type);
067        stop = System.currentTimeMillis();
068
069        StringBuilder sb = new StringBuilder();
070        sb.append(testName).append('\n');
071        char[] headline = new char[testName.length()];
072        Arrays.fill(headline, '#');
073        sb.append(headline).append('\n');
074        sb.append(result).append('\n');
075        sb.append("Took ").append(stop - start).append("ms").append('\n');
076        sb.append(getStats(client)).append('\n');
077        sb.append('\n');
078
079        return sb;
080    }
081
082    public static DnssecClient getClient(CacheConfig cacheConfig) {
083        return IntegrationTestTools.getClient(cacheConfig);
084    }
085
086    public static StringBuilder getStats(AbstractDnsClient client) {
087        StringBuilder sb = new StringBuilder();
088
089        NetworkDataSourceWithAccounting ndswa = NetworkDataSourceWithAccounting.from(client);
090        if (ndswa != null) {
091            sb.append(ndswa.getStats().toString());
092        } else {
093            sb.append("Client is not using " + NetworkDataSourceWithAccounting.class.getSimpleName());
094        }
095
096        DnsCache dnsCache = client.getCache();
097        if (dnsCache != null) {
098            sb.append(dnsCache);
099        } else {
100            sb.append("Client is not using a Cache");
101        }
102
103        return sb;
104    }
105}