github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/testhelpers_test.go (about)

     1  /**
     2   * Copyright 2016 Confluent Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package kafka
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  // ratepdisp tracks and prints message & byte rates
    25  type ratedisp struct {
    26  	name      string
    27  	start     time.Time
    28  	lastPrint time.Time
    29  	every     float64
    30  	cnt       int64
    31  	size      int64
    32  	b         *testing.B
    33  }
    34  
    35  // ratedisp_start sets up a new rate displayer
    36  func ratedispStart(b *testing.B, name string, every float64) (pf ratedisp) {
    37  	now := time.Now()
    38  	return ratedisp{name: name, start: now, lastPrint: now, b: b, every: every}
    39  }
    40  
    41  // reset start time and counters
    42  func (rd *ratedisp) reset() {
    43  	rd.start = time.Now()
    44  	rd.cnt = 0
    45  	rd.size = 0
    46  }
    47  
    48  // print the current (accumulated) rate
    49  func (rd *ratedisp) print(pfx string) {
    50  	elapsed := time.Since(rd.start).Seconds()
    51  
    52  	rd.b.Logf("%s: %s%d messages in %fs (%.0f msgs/s), %d bytes (%.3fMb/s)",
    53  		rd.name, pfx, rd.cnt, elapsed, float64(rd.cnt)/elapsed,
    54  		rd.size, (float64(rd.size)/elapsed)/(1024*1024))
    55  }
    56  
    57  // tick adds cnt of total size size to the rate displayer and also prints
    58  // running stats every 1s.
    59  func (rd *ratedisp) tick(cnt, size int64) {
    60  	rd.cnt += cnt
    61  	rd.size += size
    62  
    63  	if time.Since(rd.lastPrint).Seconds() >= rd.every {
    64  		rd.print("")
    65  		rd.lastPrint = time.Now()
    66  	}
    67  }