aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-java/src/main/java/com/yahoo/vespafeeder/ProgressPrinter.java
blob: 3b1ab38737d17a5710a904c66e53c26f7f71fe7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespafeeder;

import com.yahoo.clientmetrics.MessageTypeMetricSet;
import com.yahoo.clientmetrics.RouteMetricSet;
import com.yahoo.concurrent.Timer;
import com.yahoo.metrics.Metric;
import com.yahoo.metrics.MetricSet;
import com.yahoo.metrics.MetricVisitor;
import com.yahoo.metrics.SumMetric;

import java.io.IOException;
import java.io.PrintStream;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * Class that takes progress from the feed and prints to a stream.
 */
public class ProgressPrinter implements RouteMetricSet.ProgressCallback {
    private long startTime = 0;
    private long lastProgressTime = 0;
    private long lastVerboseProgress = 0;
    final Timer timer;
    final PrintStream output;

    public ProgressPrinter(Timer timer, PrintStream output) {
        this.timer = timer;
        this.output = output;

        startTime = timer.milliTime();
        lastProgressTime = startTime;
        lastVerboseProgress = startTime;
    }

    class PrintVisitor extends MetricVisitor {
        final PrintStream out;
        final NumberFormat format;

        PrintVisitor(PrintStream out) {
            this.out = out;
            format = NumberFormat.getNumberInstance(Locale.US);
            format.setMaximumFractionDigits(2);
            format.setMinimumFractionDigits(2);
            format.setMinimumIntegerDigits(1);
            format.setParseIntegerOnly(false);
            format.setRoundingMode(RoundingMode.HALF_UP);
            format.setGroupingUsed(false);
        }

        @Override
        public boolean visitMetricSet(MetricSet set, boolean autoGenerated) {
            if (set instanceof MessageTypeMetricSet && !set.getName().equals("total")) {
                Metric m = set.getMetric("latency");
                Metric count = set.getMetric("count");
                Metric err = set.getMetric("errors.total");

                long okCount = 0, errCount = 0, ignored = 0;
                long minLatency = 0, maxLatency = 0, avgLatency = 0;

                if (m != null) {
                    minLatency = m.getLongValue("min");
                    maxLatency = m.getLongValue("max");
                    avgLatency = m.getLongValue("average");
                }
                if (count != null) {
                    okCount = count.getLongValue("count");
                }
                Metric ignoredMetric = set.getMetric("ignored");
                if (ignoredMetric != null) {
                    ignored = ignoredMetric.getLongValue("count");
                }

                if (err != null) {
                    errCount = err.getLongValue("count");
                }

                long timeSinceStart = timer.milliTime() - startTime;

                out.println(((MessageTypeMetricSet)set).getMessageName() + ":\t" +
                        "ok: " + okCount +
                        " msgs/sec: " + format.format((double)okCount * 1000 / timeSinceStart) +
                        " failed: " + errCount +
                        " ignored: " + ignored +
                        " latency(min, max, avg): " + minLatency + ", " + maxLatency + ", " + avgLatency);
            }
            return true;
        }
    }

    public static String getDashes(int count) {
        String dashes = "";
        for (int i = 0; i < count; i++) {
            dashes += "-";
        }

        return dashes;
    }

    public synchronized void renderStatusText(RouteMetricSet metrics, PrintStream stream) throws IOException {
        String headline = "Messages sent to vespa (route " + metrics.getName() + ") :";
        stream.println(headline);
        stream.println(getDashes(headline.length()));
        metrics.visit(new PrintVisitor(stream), false);
    }

    public long getOkMessageCount(RouteMetricSet metrics) {
        SumMetric sum = (SumMetric)metrics.getMetric("total");

        MetricSet ms = (MetricSet)sum.generateSum();
        if (ms != null) {
            Metric latency = ms.getMetric("latency");
            if (latency != null) {
                return latency.getLongValue("count");
            }
        }

        return 0;
    }

    @Override
    public void onProgress(RouteMetricSet metrics) {
        try {
            long timeNow = timer.milliTime();

            if (timeNow - lastVerboseProgress > 30000) {
                output.println("\n");
                renderStatusText(metrics, output);
                lastVerboseProgress = timeNow;
            } else if (timeNow - lastProgressTime > 1000) {
                output.print("\rSuccessfully sent " + getOkMessageCount(metrics) + " messages so far");
                lastProgressTime = timeNow;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void done(RouteMetricSet metrics) {
        try {
            output.println("\n");
            renderStatusText(metrics, output);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}