commit dc2c80d4735d41cc7ba14a3c42a4576220d94c62
parent 5ebb59e9776b3fa17de96f8778dea5d42d3a2557
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Sat, 10 Apr 2021 15:29:54 +0800
feat(profiling): introduce profiling via github.com/pkg/profile
Diffstat:
3 files changed, 52 insertions(+), 0 deletions(-)
diff --git a/cmd/kanban/main.go b/cmd/kanban/main.go
@@ -5,7 +5,9 @@ import (
"log"
"os"
"path/filepath"
+ "strings"
+ "github.com/pkg/profile"
"github.com/spf13/pflag"
"gioui.org/font/gofont"
@@ -20,14 +22,19 @@ import (
var (
MemStorage bool
+ ProfileOpt string
)
func init() {
pflag.BoolVar(&MemStorage, "mem-storage", false, "store entities in memory")
+ pflag.StringVar(&ProfileOpt, "profile", "", fmt.Sprintf("record runtime performance statistics %s", profiles))
pflag.Parse()
}
func main() {
+ if stopper := Profile(ProfileOpt).Start(); stopper != nil {
+ defer stopper.Stop()
+ }
storage, err := func() (storage.Storer, error) {
if MemStorage {
return mem.New(), nil
@@ -60,3 +67,45 @@ func main() {
}()
app.Main()
}
+
+// Profile starts a profiler based on the provided option.
+type Profile string
+
+var (
+ CPU Profile = "cpu"
+ Mem Profile = "mem"
+ Block Profile = "block"
+ Trace Profile = "trace"
+
+ profiles = Profiles{
+ CPU,
+ Mem,
+ Block,
+ Trace,
+ }
+)
+
+func (p Profile) Start() interface{ Stop() } {
+ switch p {
+ case "cpu":
+ return profile.Start(profile.CPUProfile)
+ case "mem":
+ return profile.Start(profile.MemProfile)
+ }
+ return nil
+}
+
+type Profiles []Profile
+
+func (p Profiles) String() string {
+ var s strings.Builder
+ s.WriteByte('[')
+ for ii, opt := range p {
+ s.WriteString(string(opt))
+ if ii != len(p)-1 {
+ s.WriteString(", ")
+ }
+ }
+ s.WriteByte(']')
+ return s.String()
+}
diff --git a/go.mod b/go.mod
@@ -7,6 +7,7 @@ require (
gioui.org/x v0.0.0-20210120222453-b55819bc712b
github.com/boltdb/bolt v1.3.1
github.com/google/uuid v1.2.0
+ github.com/pkg/profile v1.5.0
github.com/spf13/pflag v1.0.5
golang.org/x/exp v0.0.0-20210212053707-62dc52270d37
golang.org/x/image v0.0.0-20201208152932-35266b937fa6 // indirect
diff --git a/go.sum b/go.sum
@@ -12,6 +12,8 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/pkg/profile v1.5.0 h1:042Buzk+NhDI+DeSAA62RwJL8VAuZUMQZUjCsRz1Mug=
+github.com/pkg/profile v1.5.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=