test_fruit_machine.go 2.02 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package main

import (
	"fmt"
hujiebin's avatar
hujiebin committed
5 6
	"git.hilo.cn/hilo-common/script/model"
	"git.hilo.cn/hilo-common/script/mysql"
hujiebin's avatar
hujiebin committed
7
	"git.hilo.cn/hilo-common/script/utils/jwt"
hujiebin's avatar
hujiebin committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
	"io/ioutil"
	"math/rand"
	"net/http"
	"strings"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

var fruitUserCodes = []string{"1000653", "1000516", "1000675", "1000890", "1000755", "1000593", "1000629", "1000634", "1000765",
	"1000591", "1000633", "1000224", "1000611", "1000689", "1000467", "1000384", "1000367", "1000623"}
var fruitAmounts = []int{10, 100, 1000, 3000}

func main() {
	var users []model.User
	if err := mysql.TestDB.Model(model.User{}).Where("code in ?", fruitUserCodes).Find(&users).Error; err != nil {
		panic(err)
	}
hujiebin's avatar
hujiebin committed
28 29 30 31 32
	for i := 0; i < 10; i++ {
		go func() {
			for {
				times := 20 + rand.Intn(30)
				for i := 0; i < times; i++ {
hujiebin's avatar
hujiebin committed
33

hujiebin's avatar
hujiebin committed
34 35 36 37 38 39 40 41 42 43 44 45
					arr := []int{1, 2, 3, 4, 5, 6, 7, 8}
					rand.Shuffle(len(arr), func(i, j int) {
						arr[i], arr[j] = arr[j], arr[i]
					})
					for _, id := range arr {
						if rand.Intn(100) < 30 {
							break
						}
						url := "https://test.apiv1.faceline.live/v1/fruitMachine/"
						method := "POST"
						amount := fruitAmounts[rand.Intn(len(fruitAmounts))]
						payload := strings.NewReader(fmt.Sprintf("fruitId=%d&amount=%d", id, amount))
hujiebin's avatar
hujiebin committed
46

hujiebin's avatar
hujiebin committed
47 48
						client := &http.Client{}
						req, err := http.NewRequest(method, url, payload)
hujiebin's avatar
hujiebin committed
49

hujiebin's avatar
hujiebin committed
50 51 52 53 54 55 56 57 58
						if err != nil {
							fmt.Println(err)
							return
						}
						u := users[rand.Intn(len(users))]
						token, _ := jwt.GenerateToken(u.Id, u.ExternalId, "hiloApi")
						req.Header.Add("nonce", "hilo")
						req.Header.Add("token", token)
						req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
hujiebin's avatar
hujiebin committed
59

hujiebin's avatar
hujiebin committed
60 61 62 63 64 65
						res, err := client.Do(req)
						if err != nil {
							fmt.Println(err)
							return
						}
						defer res.Body.Close()
hujiebin's avatar
hujiebin committed
66

hujiebin's avatar
hujiebin committed
67 68 69 70 71 72 73
						body, err := ioutil.ReadAll(res.Body)
						if err != nil {
							fmt.Println(err)
							return
						}
						fmt.Printf("body:%v,uid:%v\n", string(body), u.Id)
					}
hujiebin's avatar
hujiebin committed
74
				}
hujiebin's avatar
hujiebin committed
75
				//time.Sleep(time.Second * 55)
hujiebin's avatar
hujiebin committed
76
			}
hujiebin's avatar
hujiebin committed
77
		}()
hujiebin's avatar
hujiebin committed
78
	}
hujiebin's avatar
hujiebin committed
79
	time.Sleep(time.Hour)
hujiebin's avatar
hujiebin committed
80
}