github.com/kaydxh/golang@v0.0.131/go/time/time_counter.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package time
    23  
    24  import (
    25  	"fmt"
    26  	"strings"
    27  	"time"
    28  )
    29  
    30  type TimeCounter struct {
    31  	starts  []time.Time
    32  	message []string
    33  	effect  bool
    34  }
    35  
    36  func New(effect bool) *TimeCounter {
    37  	t := &TimeCounter{
    38  		effect: effect,
    39  	}
    40  	if effect {
    41  		t.starts = append(t.starts, time.Now())
    42  		t.message = append(t.message, "start")
    43  	}
    44  
    45  	return t
    46  }
    47  
    48  func (t *TimeCounter) Tick(msg string) {
    49  	if t.effect {
    50  		t.starts = append(t.starts, time.Now())
    51  		t.message = append(t.message, msg)
    52  	}
    53  }
    54  
    55  func (t *TimeCounter) Elapse() time.Duration {
    56  	if !t.effect {
    57  		return time.Duration(0)
    58  	}
    59  
    60  	if len(t.starts) == 0 {
    61  		return time.Duration(0)
    62  	}
    63  
    64  	return time.Now().Sub(t.starts[0])
    65  }
    66  
    67  func (t *TimeCounter) String() string {
    68  	if !t.effect {
    69  		return ""
    70  	}
    71  
    72  	var buf strings.Builder
    73  	t.Summary(func(idx int, msg string, cost time.Duration, at time.Time) {
    74  		buf.WriteString(fmt.Sprintf("#%d, msg: %s, cost: %s, at %s ", idx, msg, cost, at.Format(time.RFC3339)))
    75  	})
    76  
    77  	return buf.String()
    78  }
    79  
    80  func (t *TimeCounter) Summary(f func(idx int, msg string, cost time.Duration, at time.Time)) {
    81  	if !t.effect {
    82  		return
    83  	}
    84  	if f == nil || t == nil {
    85  		return
    86  	}
    87  
    88  	if len(t.message) < len(t.starts) {
    89  		return
    90  	}
    91  
    92  	for i := 1; i < len(t.starts); i++ {
    93  		f(i, t.message[i], t.starts[i].Sub(t.starts[i-1]), t.starts[i])
    94  	}
    95  }
    96  
    97  func (t *TimeCounter) Reset() {
    98  	t.starts = nil
    99  	t.message = nil
   100  }