func TestQuery(t *testing.T) { rules0, _ := randutil.IntRange(0, 999999) sucks0, _ := randutil.IntRange(0, 999999) rules1, _ := randutil.IntRange(0, 999999) sucks1, _ := randutil.IntRange(0, 999999) dse0 := dummySearchEngine{ Rules: int(rules0), Sucks: int(sucks0), } dse1 := dummySearchEngine{ Rules: int(rules1), Sucks: int(sucks1), } engines := []SearchEngine{ &dse0, &dse1, } sr := New(engines, &NilOutput{}) sr.Run() ratio, err := sr.Query("foobar") if err != nil { t.Fatal(err) } r0 := float64(sucks0) / float64(rules0) r1 := float64(sucks1) / float64(rules1) expected := (r0 + r1) / 2.0 assert.Equal(t, expected, ratio) }
// generateMetricNames generates a specified number of random metric types. func generateMetricNames(numMetrics int, store []Metric, metricToken string) []Metric { metricTypes := []string{ "c", "g", "ms", "s", } rand.Seed(time.Now().UnixNano()) r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numMetrics; i++ { newMetricName, _ := randutil.String(20, randutil.Alphabet) newMetricNS := fmt.Sprintf("%sstatsgod.test.%s", metricToken, newMetricName) newMetricCT := 1 if *connType > 0 && *connType < 6 { newMetricCT = *connType } else { newMetricCT, _ = randutil.IntRange(1, 6) } metricType := metricTypes[r.Intn(len(metricTypes))] store = append(store, Metric{ key: newMetricNS, metricType: metricType, metricValue: 0, connectionType: newMetricCT, }) } return store }
func pick(pats []*Pattern) (*Pattern, error) { var winner *Pattern length := len(pats) i, err := R.IntRange(0, length) winner = pats[i] return winner, err }
// Generate returns a new value for a token according to the definition. func (t URLToken) Generate() (r string) { if t.Choices != "" { r, _ = randutil.ChoiceString(strings.Split(t.Choices, "|")) } switch t.Pattern { case "alpha": r, _ = randutil.StringRange(t.Min, t.Max, randutil.Alphabet) case "alphanum": r, _ = randutil.AlphaStringRange(t.Min, t.Max) case "num": rInt, _ := randutil.IntRange(t.Min, t.Max) r = strconv.FormatInt(int64(rInt), 10) } return }
// FetchRand will grab a random machine // to spin up the requests lease on // In reality images should likely have tags // so that you can better optimize how the // hardware would be shared among a large // amount of users func (m *Machine) FetchRand() error { // Goqu doesn't support order by random // So i'm faking it in order to take // advantage of the ScanStructs function still var ms []Machine if err := db.From("machines"). ScanStructs(&ms); err != nil { return err } // Get "random" row total := len(ms) randInt, _ := randutil.IntRange(0, 1000) row := randInt % total *m = ms[row] return nil }
func GetBrandFollowers(brandIdHex string) (apiUsers []*duoerlapi.User, err error) { brandId, err := utils.ToObjectId(brandIdHex) if err != nil { utils.PrintStackAndError(err) return } followbrandz, err := followbrands.FindByBrandId(brandId) if err != nil { utils.PrintStackAndError(err) return } maxNum := len(followbrandz) // Get random number users if maxNum > configs.BRAND_SHOW_FOLLOWER_NUM { randIndex, err := randutil.IntRange(0, maxNum) if err != nil { utils.PrintStackAndError(err) randIndex = 0 } leftIndex := randIndex - configs.BRAND_SHOW_FOLLOWER_NUM if leftIndex < 0 { followbrandz = followbrandz[0:configs.BRAND_SHOW_FOLLOWER_NUM] } else { followbrandz = followbrandz[leftIndex:randIndex] } } followerIds := []bson.ObjectId{} for _, followBrand := range followbrandz { followerIds = append(followerIds, followBrand.UserId) } followers, err := users.FindByIds(followerIds) if err != nil { utils.PrintStackAndError(err) return } apiUsers = toApiUsers(followers) return }
func generateMetricNames(numMetrics int, store []Metric) []Metric { metricTypes := []string{ "c", "g", "ms", } r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numMetrics; i++ { newMetricName, _ := randutil.String(20, randutil.Alphabet) newMetricNS := fmt.Sprintf("statsgod.test.%s", newMetricName) newMetricCT, _ := randutil.IntRange(1, 4) store = append(store, Metric{ key: newMetricNS, metricType: metricTypes[r.Intn(len(metricTypes))], connectionType: newMetricCT, }) } return store }