mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
feat: get feed video info by funplugin
This commit is contained in:
+53
-29
@@ -3,7 +3,6 @@ package uixt
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
@@ -533,7 +532,8 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
|
||||
return fmt.Errorf("invalid sleep params: %v(%T)", action.Params, action.Params)
|
||||
case ACTION_SleepRandom:
|
||||
if params, ok := action.Params.([]interface{}); ok {
|
||||
return sleepRandom(time.Now(), params)
|
||||
sleepStrict(time.Now(), getSimulationDuration(params))
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid sleep random params: %v(%T)", action.Params, action.Params)
|
||||
case ACTION_ScreenShot:
|
||||
@@ -575,13 +575,20 @@ func convertToFloat64(val interface{}) (float64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// sleepRandom sleeps random time with given params
|
||||
// startTime is used to correct sleep duration caused by process time
|
||||
func sleepRandom(startTime time.Time, params []interface{}) error {
|
||||
// getSimulationDuration returns simulation duration by given params (in seconds)
|
||||
func getSimulationDuration(params []interface{}) (milliseconds int64) {
|
||||
if len(params) == 1 {
|
||||
// constant sleep time
|
||||
params = append(params, params[0], 1.0)
|
||||
} else if len(params) == 2 {
|
||||
// given constant duration time
|
||||
seconds, err := convertToFloat64(params[0])
|
||||
if err != nil {
|
||||
log.Error().Err(err).Interface("params", params).Msg("invalid params")
|
||||
return 0
|
||||
}
|
||||
return int64(seconds * 1000)
|
||||
}
|
||||
|
||||
if len(params) == 2 {
|
||||
// given [min, max], missing weight
|
||||
// append default weight 1
|
||||
params = append(params, 1.0)
|
||||
}
|
||||
@@ -593,15 +600,18 @@ func sleepRandom(startTime time.Time, params []interface{}) error {
|
||||
for i := 0; i+3 <= len(params); i += 3 {
|
||||
min, err := convertToFloat64(params[i])
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "invalid minimum time: %v", params[i])
|
||||
log.Error().Err(err).Interface("min", params[i]).Msg("invalid minimum time")
|
||||
return 0
|
||||
}
|
||||
max, err := convertToFloat64(params[i+1])
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "invalid maximum time: %v", params[i+1])
|
||||
log.Error().Err(err).Interface("max", params[i+1]).Msg("invalid maximum time")
|
||||
return 0
|
||||
}
|
||||
weight, err := convertToFloat64(params[i+2])
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "invalid weight value: %v", params[i+2])
|
||||
log.Error().Err(err).Interface("weight", params[i+2]).Msg("invalid weight value")
|
||||
return 0
|
||||
}
|
||||
totalProb += weight
|
||||
sections = append(sections,
|
||||
@@ -610,8 +620,8 @@ func sleepRandom(startTime time.Time, params []interface{}) error {
|
||||
}
|
||||
|
||||
if totalProb == 0 {
|
||||
log.Warn().Msg("total weight is 0, skip sleep")
|
||||
return nil
|
||||
log.Warn().Msg("total weight is 0, skip simulation")
|
||||
return 0
|
||||
}
|
||||
|
||||
r := rand.Float64()
|
||||
@@ -619,22 +629,36 @@ func sleepRandom(startTime time.Time, params []interface{}) error {
|
||||
for _, s := range sections {
|
||||
accProb += s.weight / totalProb
|
||||
if r < accProb {
|
||||
elapsed := time.Since(startTime).Seconds()
|
||||
randomSeconds := s.min + rand.Float64()*(s.max-s.min)
|
||||
dur := randomSeconds - elapsed
|
||||
|
||||
// if elapsed time is greater than random seconds, skip sleep to reduce deviation caused by process time
|
||||
if dur <= 0 {
|
||||
log.Info().Float64("elapsed", elapsed).Float64("randomSeconds", randomSeconds).
|
||||
Interface("strategy_params", params).Msg("elapsed duration >= random seconds, skip sleep")
|
||||
} else {
|
||||
log.Info().Float64("sleepDuration", dur).Float64("elapsed", elapsed).Float64("randomSeconds", randomSeconds).
|
||||
Interface("strategy_params", params).Msg("sleep remaining random seconds")
|
||||
time.Sleep(time.Duration(math.Ceil(dur*1000)) * time.Millisecond)
|
||||
}
|
||||
|
||||
return nil
|
||||
seconds := s.min + rand.Float64()*(s.max-s.min)
|
||||
log.Info().Float64("randomSeconds", seconds).
|
||||
Interface("strategy_params", params).Msg("get simulation duration")
|
||||
return int64(seconds * 1000)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
log.Warn().Interface("strategy_params", params).
|
||||
Msg("get simulation duration failed, skip simulation")
|
||||
return 0
|
||||
}
|
||||
|
||||
// sleepStrict sleeps strict duration with given params
|
||||
// startTime is used to correct sleep duration caused by process time
|
||||
func sleepStrict(startTime time.Time, strictMilliseconds int64) {
|
||||
elapsed := time.Since(startTime).Milliseconds()
|
||||
dur := strictMilliseconds - elapsed
|
||||
|
||||
// if elapsed time is greater than given duration, skip sleep to reduce deviation caused by process time
|
||||
if dur <= 0 {
|
||||
log.Info().
|
||||
Int64("elapsed(ms)", elapsed).
|
||||
Int64("strictSleep(ms)", strictMilliseconds).
|
||||
Msg("elapsed >= simulation duration, skip sleep")
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Int64("sleepDuration(ms)", dur).
|
||||
Int64("elapsed(ms)", elapsed).
|
||||
Int64("strictSleep(ms)", strictMilliseconds).
|
||||
Msg("sleep remaining duration time")
|
||||
time.Sleep(time.Duration(dur) * time.Millisecond)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user