中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

打點統計——2(批量生成隨機日志樣本)

發布時間:2020-06-13 10:44:11 來源:網絡 閱讀:350 作者:梁十八 欄目:編程語言

run.go:

package main

import (
    "./uas"
    "flag"
    "math/rand"
    "net/url"
    "os"
    "strconv"
    "strings"
    "time"
)

type resource struct {
    url     string
    target  string
    start   int
    end     int
}

func ruleResource() []resource{
    var res []resource
    //首頁
    r1 := resource{
        url: "http://localhost/",
        target: "",
        start: 0,
        end: 0,
    }
    //列表頁
    r2 := resource{
        url: "http://localhost/list/{$id}.html",
        target: "{$id}",
        start: 1,
        end: 21,
    }
    //詳情頁
    r3 := resource{
        url: "http://localhost/movie/{$id}.html",
        target: "{$id}",
        start: 1,
        end: 12924,
    }
    res = append(res, r1, r2, r3)
    return res
}

func buildUrl(res []resource) []string {
    var list []string

    for _, resItem := range res {
        if len(resItem.target) == 0 {
            list = append(list, resItem.url)
        }else {
            for i := resItem.start; i <= resItem.end; i++ {
                urlStr := strings.Replace(resItem.url, resItem.target, strconv.Itoa(i), -1)
                list = append(list, urlStr)
            }
        }
    }

    return list
}

func makeLog(current, refer, ua string) string {
    u := url.Values{}
    u.Set("time", "1")
    u.Set("url", current)
    u.Set("refer", refer)
    u.Set("ua", ua)
    paramsStr := u.Encode()
    logTemplate := "127.0.0.1 - - [14/Apr/2019:20:07:42 +0800] \"OPTIONS /dig?{$paramsStr} HTTP/1.1\" 499 0 \"-\" \"{$ua}\" \"-\""
    log := strings.Replace(logTemplate, "{$paramsStr}", paramsStr, -1)
    log = strings.Replace(log, "{$ua}", ua, -1)
    return log
}

//隨機
func randInt(min, max int) int {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    if min > max {
        return max
    }
    return r.Intn(max-min) + min
}

func main() {

    total := flag.Int("total", 100, "創建多少行日志")
    filePath := flag.String("filePath", "F:/phpStudy/PHPTutorial/nginx/logs/access.log", "日志文件路徑")
    //想生效還要調用解析方法
    flag.Parse()
    //fmt.Println(*total, *filePath)

    //構造出真實的網站url集合
    res := ruleResource()
    list := buildUrl(res)

    //按照要求,生成$total行日志內容
    logStr := ""
    for i := 1; i <= *total; i++ {
        //隨機選
        currentUrl := list[randInt(0, len(list)-1)]
        referUrl := list[randInt(0, len(list)-1)]
        ua := uas.UaList[randInt(0, len(uas.UaList)-1)]
        logStr = logStr + makeLog(currentUrl, referUrl, ua) + "\n"
        //和隨機種子一樣的間隔,盡可能保證隨機性
        time.Sleep(time.Nanosecond)
    }
    //放到循環外一次寫入
    fd, _ := os.OpenFile(*filePath, os.O_RDWR|os.O_APPEND, 0644 )
    fd.Write([]byte(logStr))
    fd.Close()
}

uas.go(各種瀏覽器信息):

package uas

var UaList = []string {
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
    " Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Mozilla/5.0 (Windows NT 6.1; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
    " Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
}

運行程序(隨機生成10000條日志):

go run run.go --total=10000 --filePath="F:/phpStudy/PHPTutorial/nginx/logs/access.log"

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

芷江| 林西县| 腾冲县| 广宗县| 西安市| 宜都市| 陈巴尔虎旗| 富平县| 三门县| 宜城市| 桑日县| 南投市| 江山市| 松滋市| 舞阳县| 咸丰县| 牟定县| 平凉市| 南溪县| 惠东县| 潢川县| 苍南县| 岳阳县| 嵊泗县| 大城县| 宁南县| 定安县| 揭西县| 锡林郭勒盟| 扎兰屯市| 安宁市| 梅州市| 通道| 临沂市| 精河县| 临颍县| 巍山| 桦川县| 蒙山县| 莎车县| 正镶白旗|