The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
zarte

go 爬虫 cpu 占用 99%内存占用还好要怎么改?

  •  
  •   zarte · Aug 2, 2018 · 3464 views
  •   You need to sign in to view this topic
    This topic created in 2865 days ago, the information mentioned may be changed or developed.

    用的是 surfer 下载正则循环匹配关键词等规则匹配连接,抓取文章。 一百多个网站,词有几千个。

    10 replies    2018-08-03 16:11:46 +08:00
    owenliang
        1
    owenliang  
       Aug 2, 2018
    几个 cpu,什么型号。
    fcten
        2
    fcten  
       Aug 2, 2018
    这种情况 99%是正则写的有问题

    参考 https://blog.csdn.net/ityouknow/article/details/80851338
    gamexg
        3
    gamexg  
       Aug 2, 2018 via Android   ❤️ 1
    不先看看哪些函数占用 cpu 高?
    google go pprof

    大量关键字匹配
    google 大量 关键字 匹配 算法
    FanWall
        4
    FanWall  
       Aug 2, 2018 via Android   ❤️ 1
    同怀疑正则的问题
    regicide
        5
    regicide  
       Aug 2, 2018 via iPhone
    ac 自动机?
    zarte
        6
    zarte  
    OP
       Aug 3, 2018
    @fcten 谢谢我看下
    zarte
        7
    zarte  
    OP
       Aug 3, 2018
    @gamexg 谢谢
    wkc
        8
    wkc  
       Aug 3, 2018
    golang 的正则实现是线性时间复杂度的

    参见文档 https://golang.org/pkg/regexp/
    > The regexp implementation provided by this package is guaranteed to run in time linear in the size of the input.
    zarte
        9
    zarte  
    OP
       Aug 3, 2018
    @gamexg
    pprof 这个生成的文件是空的呀,没有错误提示,咋整?

    main{
    。。。。
    f, err := os.Create("mains")
    if err != nil {
    log.Fatal(err)
    }
    fmt.Println(f)
    // 开启 CPU profiling
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()
    gamexg
        10
    gamexg  
       Aug 3, 2018
    @zarte #9

    我平常是这么做的,代码里面加上下面的内容:

    import _ "net/http/pprof"

    go func() {
    log.Println( http.ListenAndServe("localhost:6060", nil))
    }()

    然后需要分析性能时直接运行下面的命令:

    分析 cpu 占用:go tool pprof --web 编译出来的文件.exe http://127.0.0.1:6060/debug/pprof/profile
    分析内存占用:go tool pprof -inuse_space --web 编译出来的文件.exe http://127.0.0.1:6060/debug/pprof/heap

    可以直接生成 SVG 调用图,很清晰。图是使用 graphviz 生成的,需要在本机安装 graphviz。
    如果没有 graphviz,可以去掉 --web 参数,是 cmd 版本。

    线上环境推荐这种方式,另外可以浏览器访问 http://127.0.0.1:6060/debug/pprof,里面有不少有用的内容。

    如果本身有 http 服务,可以看下 net/http/pprof init() 函数代码,可以照着合并到已有的 http 服务内。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2810 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 03:57 · PVG 11:57 · LAX 20:57 · JFK 23:57
    ♥ Do have faith in what you're doing.