Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions commands/ppreviewPush.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"fmt"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/StackExchange/dnscontrol/v4/models"
"github.com/StackExchange/dnscontrol/v4/pkg/bindserial"
Expand All @@ -25,6 +27,7 @@ import (
"github.com/StackExchange/dnscontrol/v4/pkg/providers"
"github.com/StackExchange/dnscontrol/v4/pkg/rfc4183"
"github.com/StackExchange/dnscontrol/v4/pkg/zonerecs"
"github.com/dustin/go-humanize"
"github.com/nozzle/throttler"
"github.com/urfave/cli/v3"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -409,8 +412,10 @@ func prun(args PPreviewArgs, push bool, interactive bool, out printer.CLI, repor
fmt.Fprintf(os.Stderr, "##teamcity[buildStatus status='SUCCESS' text='%d corrections']", totalCorrections)
}
rfc4183.PrintWarning()
out.PrintfIf(fullMode, "Inaccurate statistics: %s\n", stats(cfg))
notifier.Done()
out.Printf("Done. %d corrections.\n", totalCorrections)

err = writeReport(report, reportItems)
if err != nil {
return errors.New("could not write report")
Expand All @@ -424,6 +429,56 @@ func prun(args PPreviewArgs, push bool, interactive bool, out printer.CLI, repor
return nil
}

// stats returns a JSON string with memory usage statistics.
// These stats are unofficial and subject to change without notice.
// "average_mem_per_record" is misleading because it includes all memory overhead.
func stats(cfg *models.DNSConfig) string {

// https://www.datadoghq.com/blog/go-memory-metrics/
// [T]he following expression accurately reflects the value the runtime attempts to maintain as the limit:
// runtime.MemStats.Sys − runtime.MemStats.HeapReleased
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
memoryInUse := m.Sys - m.HeapReleased

numRecords := countRecords(cfg)
memPerRecord := int64(float64(memoryInUse) / float64(max(1, numRecords)))
memPerRecordStr := humanize.IBytes(uint64(memPerRecord)) + " bytes"

statsInfo := struct {
MemoryInUse uint64 `json:"memory_in_use"`
MemoryInUseStr string `json:"memory_in_use_str"`
NumRecords int `json:"num_records"`
NumZones int `json:"num_zones"`
RCSize int `json:"rc_size"`
Benchmark1 int64 `json:"benchmark1"`
Benchmark1Str string `json:"benchmark1str"`
}{
MemoryInUse: memoryInUse,
MemoryInUseStr: humanize.Bytes(memoryInUse),
NumRecords: numRecords,
NumZones: len(cfg.Domains),
RCSize: int(unsafe.Sizeof((models.RecordConfig{}))),
Benchmark1: memPerRecord,
Benchmark1Str: memPerRecordStr,
}

jsonBytes, err := json.Marshal(statsInfo)
if err != nil {
return fmt.Sprintf("error marshaling stats: %v", err)
}
return string(jsonBytes)
}

func countRecords(cfg *models.DNSConfig) int {
total := 0
for _, domain := range cfg.Domains {
total += len(domain.Records)
}
return total
}

// whichZonesToProcess takes a list of DomainConfigs and a filter string and
// returns a list of DomainConfigs whose Domain.UniqueName matched the
// filter. The filter string is a comma-separated list of domain names. If the
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ require (
github.com/aliyun/alibaba-cloud-sdk-go v1.63.107
github.com/aws/aws-sdk-go-v2/service/sts v1.41.6
github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v5 v5.0.18
github.com/dustin/go-humanize v1.0.1
github.com/failsafe-go/failsafe-go v0.9.5
github.com/fatih/color v1.18.0
github.com/fbiville/markdown-table-formatter v0.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c h1:+Zo5Ca9
github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c/go.mod h1:HJGU9ULdREjOcVGZVPB5s6zYmHi1RxzT71l2wQyLmnE=
github.com/dnsimple/dnsimple-go v1.7.0 h1:JKu9xJtZ3SqOC+BuYgAWeab7+EEx0sz422vu8j611ZY=
github.com/dnsimple/dnsimple-go v1.7.0/go.mod h1:EKpuihlWizqYafSnQHGCd/gyvy3HkEQJ7ODB4KdV8T8=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
Expand Down
Loading