打点统计——2(批量生成随机日志样本)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打点统计——2(批量生成随机日志样本)相关的知识,希望对你有一定的参考价值。

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) + "
"
        //和随机种子一样的间隔,尽可能保证随机性
        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"

以上是关于打点统计——2(批量生成随机日志样本)的主要内容,如果未能解决你的问题,请参考以下文章

打点统计——3(go日志读取分析写入)

宋欣蓉.2019.11.21.spss

Koa 请求日志打点工具

现代信号处理01 -概率论与随机过程回顾

[概率论与数理统计-5]:一元连续随机变量=>几何图形=>样本空间=>组合样本=>长度/面积/体积=>几何概率

python怎样生成正太分布的随机数