vegeta压测工具源码修改, 增加 摸高模式(梯度加压)

文章介绍了在pacer.go中定义的HighTouchPacer结构体,它是一个用于控制压力测试速率的接口实现。HighTouchPacer使用梯度加压策略,根据StartAtRate、Slope等参数动态调整速率。Rate函数计算当前速率,Pace函数决定下一次发送请求前应等待的时间,而hits函数则返回指定时间内的预计请求数量。
摘要由CSDN通过智能技术生成

在pacer.go中增加如下代码.
pacer接口实现类为整个压测的定速器. 作用就是控制施压速率, 什么时间速率应该是多少.
摸高模式(梯度加压)效果如下
在这里插入图片描述

type HighTouchPacer struct {
	StartAt              Rate
	Slope                float64
	HighTouchTimes       float64
	PerHighTouchDuration float64
	PerStayDuration      float64
}

func (h HighTouchPacer) Rate(elapsed time.Duration) float64 {
	a := h.Slope
	//x := elapsed.Seconds()
	xp := elapsed.Seconds()
	b := h.StartAt.hitsPerNs() * 1e9
	l := h.PerHighTouchDuration + h.PerStayDuration
	count := float64(0)
	for xp > l {
		xp = xp - l
		count = count + 1
	}
	if xp >= h.PerHighTouchDuration {
		return a*h.PerHighTouchDuration*(count+1) + b
	}
	return a*h.PerHighTouchDuration*count + a*xp + b
}

// Pace determines the length of time to sleep until the next hit is sent.
func (h HighTouchPacer) Pace(elapsed time.Duration, hits uint64) (time.Duration, bool) {
	switch {
	case h.StartAt.Per == 0 || h.StartAt.Freq == 0:
		return 0, false // Zero value = infinite rate
	case h.StartAt.Per < 0 || h.StartAt.Freq < 0:
		return 0, true
	}

	expectedHits := h.hits(elapsed)
	if hits == 0 || hits < uint64(expectedHits) {
		// Running behind, send next hit immediately.
		return 0, false
	}

	rate := h.Rate(elapsed)
	interval := math.Round(1e9 / rate)

	if n := uint64(interval); n != 0 && math.MaxInt64/n < hits {
		// We would overflow wait if we continued, so stop the attack.
		return 0, true
	}

	delta := float64(hits+1) - expectedHits
	wait := time.Duration(interval * delta)

	return wait, false
}

// hits returns the number of hits that have been sent during an attack
// lasting t nanoseconds. It returns a float so we can tell exactly how
// much we've missed our target by when solving numerically in Pace.
func (h HighTouchPacer) hits(t time.Duration) float64 {
	if t < 0 {
		return 0
	}
	a := h.Slope
	x := t.Seconds()
	xp := t.Seconds()
	b := h.StartAt.hitsPerNs() * 1e9
	l := h.PerHighTouchDuration + h.PerStayDuration
	count := float64(0)
	for xp > l {
		xp = xp - l
		count = count + 1
	}
	if xp >= h.PerHighTouchDuration {
		return b*x + a*math.Pow(h.PerHighTouchDuration*(count+1), 2)/2 + h.PerStayDuration*a*h.PerHighTouchDuration*((1+count)*count/2) + (xp-h.PerHighTouchDuration)*a*h.PerHighTouchDuration*(count+1)
	}
	return b*x + a*math.Pow(h.PerHighTouchDuration*count+xp, 2)/2 + h.PerStayDuration*a*h.PerHighTouchDuration*((1+count)*count/2)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值