aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-java/src/main/java/com/yahoo/vespafeeder/ProgressPrinter.java
blob: 8b428f83c7d4f015cc2a86cdf9deccd87599ee11 (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
// Copyright Vespa.ai. 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 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;
    final NumberFormat format;

    public ProgressPrinter(Timer timer, PrintStream output) {
        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);
        this.timer = timer;
        this.output = output;

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

    private void printMetrics(PrintStream out, RouteMetricSet metrics) {
        for (MessageTypeMetricSet m : metrics.getMetrics().values()) {
            long timeSinceStart = timer.milliTime() - startTime;
            out.println(m.getMessageName() + ":\t" +
                    "ok: " + m.count +
                    " msgs/sec: " + format.format((double)m.count * 1000 / timeSinceStart) +
                    " failed: " + m.errorCount +
                    " ignored: " + m.ignored +
                    " latency(min, max, avg): " + m.latency_min + ", " + m.latency_max + ", " + m.latency_total/Long.max(1L, m.count));
        }
    }

    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) {
        String headline = "Messages sent to vespa (route " + metrics.getRoute() + ") :";
        stream.println(headline);
        stream.println(getDashes(headline.length()));
        printMetrics(stream, metrics);
    }

    public long getOkMessageCount(RouteMetricSet metrics) {
        long count = 0;
        for (MessageTypeMetricSet m : metrics.getMetrics().values()) {
            count += m.count;
        }

        return count;
    }

    @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();
        }
    }
}