Created
September 20, 2018 18:18
-
-
Save pioh/1564cf598512b705505f781b2ec503f2 to your computer and use it in GitHub Desktop.
Profiler example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/pkg/errors" | |
) | |
func main() { | |
if err := StartProfiler(); err != nil { | |
fmt.Print("%+v\n", errors.Wrap(err, "Неудалось запустить профайлер\n")) | |
} | |
for { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
"path" | |
"strings" | |
"github.com/caarlos0/env" | |
"github.com/pkg/errors" | |
) | |
type ProfilerConfig struct { | |
Enabled bool `env:"PROFILER_ENABLED" envDefault:"true"` | |
OutputDirectory string `env:"PROFILER_OUTPUT_DIRECTORY" envDefault:"~/valuation.profiler/"` | |
ScriptsDirectory string `env:"PROFILER_SCRIPTS_DIRECTORY" envDefault:"./"` | |
History string `env:"PROFILER_HISTORY" envDefault:"1440"` | |
Duration string `env:"PROFILER_DURATION" envDefault:"60"` | |
Interval string `env:"PROFILER_INTERVAL" envDefault:"10000000"` | |
} | |
func GetProfilerConfig() (*ProfilerConfig, error) { | |
conf := &ProfilerConfig{} | |
if err := env.Parse(conf); err != nil { | |
return nil, errors.Wrap(err, "Failed parse profiler config from env\n") | |
} | |
conf.OutputDirectory = strings.Replace(conf.OutputDirectory, "~", os.Getenv("HOME"), 1) | |
conf.ScriptsDirectory = strings.Replace(conf.ScriptsDirectory, "~", os.Getenv("HOME"), 1) | |
return conf, nil | |
} | |
func StartProfiler() error { | |
conf, err := GetProfilerConfig() | |
if err != nil { | |
return errors.Wrap(err, "Unable to get config for profiler\n") | |
} | |
if !conf.Enabled { | |
fmt.Print("Profiler disabled\n") | |
return nil | |
} | |
if err := os.MkdirAll(conf.OutputDirectory, 0644); err != nil { | |
return errors.Wrapf(err, "failed create output directory %s\n", conf.OutputDirectory) | |
} | |
cmd := exec.Command( | |
"./sleep.sh", | |
"--history", conf.History, | |
"-d", conf.Duration, | |
"-i", conf.Interval, | |
"-o", "svg", | |
"-f", path.Join(conf.OutputDirectory, "prof_"), | |
"-ff", ".svg", | |
) | |
cmd.Dir = conf.ScriptsDirectory | |
cmd.Stderr = os.Stderr | |
cmd.Stdout = os.Stdout | |
cmd.StdoutPipe() | |
if err := cmd.Start(); err != nil { | |
return errors.Wrapf( | |
err, | |
"failed start profiler script %s\n", | |
path.Join(conf.ScriptsDirectory, "sleep.sh"), | |
) | |
} | |
fmt.Printf("Profiler started with pid '%d'\n", cmd.Process.Pid) | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo oke | |
sleep 5 | |
echo oke 2 | |
sleep 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment