예제 #1
0
func profile(clk clock.Clock, w io.Writer, r io.Reader) (io.Writer, io.Reader, func() TimeProfile) {

	preciseWriter := &preciseTimedWriter{clk: clk, w: w}
	preciseReader := &preciseTimedReader{clk: clk, r: r}

	start := clk.Now()
	return preciseWriter, preciseReader, func() TimeProfile {
		return TimeProfile{
			Total:     clk.Now().Sub(start),
			WaitRead:  preciseReader.WaitRead(),
			WaitWrite: preciseWriter.WaitWrite(),
		}
	}
}
예제 #2
0
func profileSample(clk clock.Clock, w io.Writer, r io.Reader, res time.Duration) (io.Writer, io.Reader, func() SamplingProfile) {
	samplingWriter := &samplingTimeWriter{w: w}
	samplingReader := &samplingTimeReader{r: r}

	start := clk.Now()
	done := make(chan struct{})

	samples := SamplingProfile{}
	go func() {
		ticker := clk.Ticker(res)
		defer ticker.Stop()
		for {
			select {
			case <-ticker.C:

				isWriting := atomic.LoadUint32(&samplingWriter.state) == stateBlocked
				isReading := atomic.LoadUint32(&samplingReader.state) == stateBlocked

				if isWriting {
					samples.Writing++
				} else {
					samples.NotWriting++
				}
				if isReading {
					samples.Reading++
				} else {
					samples.NotReading++
				}
			case <-done:
				return
			}
		}
	}()

	return samplingWriter, samplingReader, func() SamplingProfile {
		close(done)
		total := clk.Now().Sub(start)
		samples.TimeProfile = TimeProfile{
			Total:     total,
			WaitRead:  time.Duration(float64(samples.Reading) / float64(samples.Reading+samples.NotReading) * float64(total)),
			WaitWrite: time.Duration(float64(samples.Writing) / float64(samples.Writing+samples.NotWriting) * float64(total)),
		}
		return samples
	}
}
예제 #3
0
// nowInMillis is a utility function that call Now on the clock and converts it
// to milliseconds.
func nowInMillis(c clock.Clock) int64 {
	return c.Now().UnixNano() / int64(time.Millisecond)
}
예제 #4
0
파일: ticker.go 프로젝트: vimeo/statsdaemon
// GetAlignedTicker returns a ticker so that, let's say interval is a second
// then it will tick at every whole second, or if it's 60s than it's every whole
// minute. Note that in my testing this is about .0001 to 0.0002 seconds off due
// to scheduling etc.
func GetAlignedTicker(c clock.Clock, period time.Duration) *clock.Ticker {
	unix := c.Now().UnixNano()
	diff := time.Duration(period - (time.Duration(unix) % period))
	return c.Ticker(diff)
}