打点统计——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) + "\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"
文章名称:打点统计——2(批量生成随机日志样本)
当前路径:http://azwzsj.com/article/pesdjo.html