From da6c86ccb96e7e2f9a1d1102142f214b1b38a203 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Sat, 1 Apr 2023 20:06:01 +0200 Subject: Refactor metric prints --- main.go | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 784638d..827eeb2 100644 --- a/main.go +++ b/main.go @@ -119,17 +119,18 @@ func work(ctx context.Context, opts *options.Options, cli *http.Client, question answer := qna.Answer{Domain: question.Domain} req, err := http.NewRequestWithContext(cctx, http.MethodGet, "https://"+question.Domain, nil) if err != nil { + answer.ReqErr = err answerCh <- answer return } rsp, err := cli.Do(req) if err != nil { + answer.DoErr = err answerCh <- answer return } defer rsp.Body.Close() - answer.OK = true onion, ok := onionloc.HTTP(rsp) if ok { @@ -143,29 +144,16 @@ func work(ctx context.Context, opts *options.Options, cli *http.Client, question } func workAggregator(ctx context.Context, opts options.Options, answerCh chan qna.Answer) { - numConnect := 0 - numOnions := 0 - numAll := 0 - output := func(prefix string) { - log.Printf("%s: %d/%d connected, %d sites configured Onion-Location\n", prefix, numConnect, numAll, numOnions) - } + p := qna.Progress{} handleAnswer := func(a qna.Answer) { - numAll += 1 - if !a.OK { - return - } - - numConnect += 1 + p.AddAnswer(a) if a.HTTP != "" || a.HTML != "" { - numOnions += 1 - fmt.Printf("%s header=%s attribute=%s\n", a.Domain, a.HTTP, a.HTML) + fmt.Printf("%s\n", a.String()) } } metrics := time.NewTicker(opts.MetricsInterval) defer metrics.Stop() - - defer output("SUMMARY") for { select { case <-ctx.Done(): @@ -175,13 +163,14 @@ func workAggregator(ctx context.Context, opts options.Options, answerCh chan qna case a := <-answerCh: handleAnswer(a) case <-time.After(opts.Timeout + time.Second): + log.Printf("INFO: metrics@aggregator: summary: \n\n%s\n\n", p.String()) return } } case a := <-answerCh: handleAnswer(a) case <-metrics.C: - output("INFO") + log.Printf("INFO: metrics@aggregator: \n\n%s\n\n", p.String()) } } } @@ -242,11 +231,13 @@ func workGenerator(ctx context.Context, opts options.Options, fp *os.File, quest readCount++ case <-metrics.C: now := time.Now().Unix() - log.Printf("INFO: currently %.1f sites/s, %.1f sites/s since start, at line %d\n", - float64(nextLine-latestCount)/float64(now-latestTime), - float64(nextLine-opts.StartLineInclusive)/float64(now-startTime), - nextLine, - ) + currRate := float64(nextLine-latestCount) / float64(now-latestTime) + avgRate := float64(nextLine-opts.StartLineInclusive) / float64(now-startTime) + + str := fmt.Sprintf(" Current rate: %.1f sites/s\n", currRate) + str += fmt.Sprintf(" Average rate: %.1f sites/s\n", avgRate) + str += fmt.Sprintf(" Next line: %d\n", nextLine) + log.Printf("INFO: metrics@generator:\n\n%s\n\n", str) latestCount = nextLine latestTime = now -- cgit v1.2.3