[magneticod] switch to pkg/profile from std lib

This commit is contained in:
Bora M. Alper 2019-05-19 17:24:27 +01:00
parent 061aff8ddc
commit d55e65419d
No known key found for this signature in database
GPG Key ID: 8F1A9504E1BD114D

View File

@ -5,24 +5,22 @@ import (
"net" "net"
"os" "os"
"os/signal" "os/signal"
"runtime"
"runtime/pprof"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pkg/profile"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"github.com/boramalper/magnetico/pkg/util" "github.com/Wessie/appdirs"
"github.com/boramalper/magnetico/cmd/magneticod/bittorrent/metadata" "github.com/boramalper/magnetico/cmd/magneticod/bittorrent/metadata"
"github.com/boramalper/magnetico/cmd/magneticod/dht" "github.com/boramalper/magnetico/cmd/magneticod/dht"
"github.com/Wessie/appdirs"
"github.com/boramalper/magnetico/pkg/persistence" "github.com/boramalper/magnetico/pkg/persistence"
"github.com/boramalper/magnetico/pkg/util"
) )
type opFlags struct { type opFlags struct {
@ -74,25 +72,16 @@ func main() {
zap.ReplaceGlobals(logger) zap.ReplaceGlobals(logger)
if opFlags.Profile == "cpu" { switch opFlags.Profile {
file, err := os.OpenFile("magneticod_cpu.prof", os.O_CREATE|os.O_WRONLY, 0755) case "cpu":
if err != nil { defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
zap.L().Panic("Could not open the cpu profile file!", zap.Error(err)) case "memory":
} defer profile.Start(
if err = pprof.StartCPUProfile(file); err != nil { profile.MemProfile,
zap.L().Fatal("Could not start CPU profiling!", zap.Error(err)) profile.ProfilePath("."),
} profile.NoShutdownHook,
defer func() { profile.MemProfileRate(1),
if err = file.Sync(); err != nil { ).Stop()
zap.L().Fatal("Could not sync profiling file!", zap.Error(err))
}
}()
defer func() {
if err = file.Close(); err != nil {
zap.L().Fatal("Could not close profiling file!", zap.Error(err))
}
}()
defer pprof.StopCPUProfile()
} }
// Initialise the random number generator // Initialise the random number generator
@ -110,8 +99,6 @@ func main() {
trawlingManager := dht.NewTrawlingManager(opFlags.IndexerAddrs, opFlags.IndexerInterval) trawlingManager := dht.NewTrawlingManager(opFlags.IndexerAddrs, opFlags.IndexerInterval)
metadataSink := metadata.NewSink(5*time.Second, opFlags.LeechMaxN) metadataSink := metadata.NewSink(5*time.Second, opFlags.LeechMaxN)
zap.L().Debug("Peer ID", zap.ByteString("peerID", metadataSink.PeerID))
// The Event Loop // The Event Loop
for stopped := false; !stopped; { for stopped := false; !stopped; {
select { select {
@ -134,23 +121,6 @@ func main() {
zap.L().Info("Fetched!", zap.String("name", md.Name), util.HexField("infoHash", md.InfoHash)) zap.L().Info("Fetched!", zap.String("name", md.Name), util.HexField("infoHash", md.InfoHash))
case <-interruptChan: case <-interruptChan:
if opFlags.Profile == "heap" {
file, err := os.OpenFile("magneticod_heap.prof", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
zap.L().Panic("Could not open the memory profile file!", zap.Error(err))
}
runtime.GC() // get up-to-date statistics
if err = pprof.WriteHeapProfile(file); err != nil {
zap.L().Fatal("Could not write heap profile!", zap.Error(err))
}
if err = file.Sync(); err != nil {
zap.L().Fatal("Could not sync profiling file!", zap.Error(err))
}
if err = file.Close(); err != nil {
zap.L().Fatal("Could not close profiling file!", zap.Error(err))
}
}
trawlingManager.Terminate() trawlingManager.Terminate()
stopped = true stopped = true
} }
@ -171,7 +141,7 @@ func parseFlags() (*opFlags, error) {
LeechMaxN uint `long:"leech-max-n" description:"Maximum number of leeches." default:"200"` LeechMaxN uint `long:"leech-max-n" description:"Maximum number of leeches." default:"200"`
Verbose []bool `short:"v" long:"verbose" description:"Increases verbosity."` Verbose []bool `short:"v" long:"verbose" description:"Increases verbosity."`
Profile string `long:"profile" description:"Enable profiling." choice:"cpu" choice:"heap"` Profile string `long:"profile" description:"Enable profiling." choice:"cpu" choice:"memory"`
} }
opF := new(opFlags) opF := new(opFlags)