Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
169 changes: 76 additions & 93 deletions cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ const commonSliceGenerateTest = `func generateTest${structName}() ${structName}
}

func fillTest${structName}(tv ${structName}) {
tv.Resize(7)
for i := 0; i < tv.Len(); i++ {
fillTest${elementName}(tv.At(i))
l := 7
tv.EnsureCapacity(l)
for i := 0; i < l; i++ {
fillTest${elementName}(tv.AppendEmpty())
}
}`

Expand All @@ -119,7 +120,7 @@ const slicePtrTemplate = `// ${structName} logically represents a slice of ${ele
// Important: zero-initialized instance is not valid for use.
type ${structName} struct {
// orig points to the slice ${originName} field contained somewhere else.
// We use pointer-to-slice to be able to modify it in functions like Resize.
// We use pointer-to-slice to be able to modify it in functions like EnsureCapacity.
orig *[]*${originName}
}

Expand All @@ -128,7 +129,7 @@ func new${structName}(orig *[]*${originName}) ${structName} {
}

// New${structName} creates a ${structName} with 0 elements.
// Can use "Resize" to initialize with a given length.
// Can use "EnsureCapacity" to initialize with a given capacity.
func New${structName}() ${structName} {
orig := []*${originName}(nil)
return ${structName}{&orig}
Expand Down Expand Up @@ -172,43 +173,42 @@ func (es ${structName}) CopyTo(dest ${structName}) {
*dest.orig = wrappers
}

// Resize is an operation that resizes the slice:
// 1. If the newLen <= len then equivalent with slice[0:newLen:cap].
// 2. If the newLen > len then (newLen - cap) empty elements will be appended to the slice.
// EnsureCapacity is an operation that ensures the slice has at least the specified capacity.
// 1. If the newCap <= cap then no change in capacity.
// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap.
//
// Here is how a new ${structName} can be initialized:
// es := New${structName}()
// es.Resize(4)
// for i := 0; i < es.Len(); i++ {
// e := es.At(i)
// es.EnsureCapacity(4)
// for i := 0; i < 4; i++ {
// e := es.AppendEmpty()
// // Here should set all the values for e.
// }
func (es ${structName}) Resize(newLen int) {
oldLen := len(*es.orig)
func (es ${structName}) EnsureCapacity(newCap int) {
oldCap := cap(*es.orig)
if newLen <= oldLen {
*es.orig = (*es.orig)[:newLen:oldCap]
if newCap <= oldCap {
return
}

if newLen > oldCap {
newOrig := make([]*${originName}, oldLen, newLen)
copy(newOrig, *es.orig)
*es.orig = newOrig
}

// Add extra empty elements to the array.
extraOrigs := make([]${originName}, newLen-oldLen)
for i := range extraOrigs {
*es.orig = append(*es.orig, &extraOrigs[i])
}
newOrig := make([]*${originName}, len(*es.orig), newCap)
copy(newOrig, *es.orig)
*es.orig = newOrig
}

// AppendEmpty will append to the end of the slice an empty ${elementName}.
// It returns the newly added ${elementName}.
func (es ${structName}) AppendEmpty() ${elementName} {
*es.orig = append(*es.orig, &${originName}{})
return es.At(es.Len() - 1)
}

// AppendEmptyN will ensure that the slice has the capacity to hold n additional
// entries and then append n empty entries to the end of the slice.
func (es ${structName}) AppendEmptyN(n int) {
es.EnsureCapacity(es.Len()+n)
for i := 0; i < n; i++ {
*es.orig = append(*es.orig, &${originName}{})
}
}`

const slicePtrTestTemplate = `func Test${structName}(t *testing.T) {
Expand All @@ -217,7 +217,7 @@ const slicePtrTestTemplate = `func Test${structName}(t *testing.T) {
es = new${structName}(&[]*${originName}{})
assert.EqualValues(t, 0, es.Len())

es.Resize(7)
es.AppendEmptyN(7)
emptyVal := new${elementName}(&${originName}{})
testVal := generateTest${elementName}()
assert.EqualValues(t, 7, es.Len())
Expand All @@ -243,46 +243,38 @@ func Test${structName}_CopyTo(t *testing.T) {
assert.EqualValues(t, generateTest${structName}(), dest)
}

func Test${structName}_Resize(t *testing.T) {
func Test${structName}_EnsureCapacity(t *testing.T) {
es := generateTest${structName}()
emptyVal := new${elementName}(&${originName}{})
// Test Resize less elements.
const resizeSmallLen = 4
expectedEs := make(map[*${originName}]bool, resizeSmallLen)
for i := 0; i < resizeSmallLen; i++ {
// Test ensure smaller capacity.
const ensureSmallLen = 4
expectedEs := make(map[*${originName}]bool)
for i := 0; i < es.Len(); i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, resizeSmallLen, len(expectedEs))
es.Resize(resizeSmallLen)
assert.Equal(t, resizeSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, resizeSmallLen)
assert.Equal(t, es.Len(), len(expectedEs))
es.EnsureCapacity(ensureSmallLen)
assert.Less(t, ensureSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, es.Len())
for i := 0; i < es.Len(); i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)

// Test Resize more elements.
const resizeLargeLen = 7
// Test ensure larger capacity
const ensureLargeLen = 9
oldLen := es.Len()
expectedEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, oldLen, len(expectedEs))
es.Resize(resizeLargeLen)
assert.Equal(t, resizeLargeLen, es.Len())
es.EnsureCapacity(ensureLargeLen)
assert.Equal(t, ensureLargeLen, cap(*es.orig))
foundEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)
for i := oldLen; i < resizeLargeLen; i++ {
assert.EqualValues(t, emptyVal, es.At(i))
}

// Test Resize 0 elements.
es.Resize(0)
assert.Equal(t, 0, es.Len())
}`

const sliceValueTemplate = `// ${structName} logically represents a slice of ${elementName}.
Expand All @@ -294,7 +286,7 @@ const sliceValueTemplate = `// ${structName} logically represents a slice of ${e
// Important: zero-initialized instance is not valid for use.
type ${structName} struct {
// orig points to the slice ${originName} field contained somewhere else.
// We use pointer-to-slice to be able to modify it in functions like Resize.
// We use pointer-to-slice to be able to modify it in functions like EnsureCapacity.
orig *[]${originName}
}

Expand All @@ -303,7 +295,7 @@ func new${structName}(orig *[]${originName}) ${structName} {
}

// New${structName} creates a ${structName} with 0 elements.
// Can use "Resize" to initialize with a given length.
// Can use "EnsureCapacity" to initialize with a given capacity.
func New${structName}() ${structName} {
orig := []${originName}(nil)
return ${structName}{&orig}
Expand Down Expand Up @@ -342,43 +334,42 @@ func (es ${structName}) CopyTo(dest ${structName}) {
}
}

// Resize is an operation that resizes the slice:
// 1. If the newLen <= len then equivalent with slice[0:newLen:cap].
// 2. If the newLen > len then (newLen - cap) empty elements will be appended to the slice.
// EnsureCapacity is an operation that ensures the slice has at least the specified capacity.
// 1. If the newCap <= cap then no change in capacity.
// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap.
//
// Here is how a new ${structName} can be initialized:
// es := New${structName}()
// es.Resize(4)
// for i := 0; i < es.Len(); i++ {
// e := es.At(i)
// es.EnsureCapacity(4)
// for i := 0; i < 4; i++ {
// e := es.AppendEmpty()
// // Here should set all the values for e.
// }
func (es ${structName}) Resize(newLen int) {
oldLen := len(*es.orig)
func (es ${structName}) EnsureCapacity(newCap int) {
oldCap := cap(*es.orig)
if newLen <= oldLen {
*es.orig = (*es.orig)[:newLen:oldCap]
if newCap <= oldCap {
return
}

if newLen > oldCap {
newOrig := make([]${originName}, oldLen, newLen)
copy(newOrig, *es.orig)
*es.orig = newOrig
}

// Add extra empty elements to the array.
empty := ${originName}{}
for i := oldLen; i < newLen; i++ {
*es.orig = append(*es.orig, empty)
}
newOrig := make([]${originName}, len(*es.orig), newCap)
copy(newOrig, *es.orig)
*es.orig = newOrig
}

// AppendEmpty will append to the end of the slice an empty ${elementName}.
// It returns the newly added ${elementName}.
func (es ${structName}) AppendEmpty() ${elementName} {
*es.orig = append(*es.orig, ${originName}{})
return es.At(es.Len() - 1)
}

// AppendEmptyN will ensure that the slice has the capacity to hold n additional
// entries and then append n empty entries to the end of the slice.
func (es ${structName}) AppendEmptyN(n int) {
es.EnsureCapacity(es.Len()+n)
for i := 0; i < n; i++ {
*es.orig = append(*es.orig, ${originName}{})
}
}`

const sliceValueTestTemplate = `func Test${structName}(t *testing.T) {
Expand All @@ -387,7 +378,7 @@ const sliceValueTestTemplate = `func Test${structName}(t *testing.T) {
es = new${structName}(&[]${originName}{})
assert.EqualValues(t, 0, es.Len())

es.Resize(7)
es.AppendEmptyN(7)
emptyVal := new${elementName}(&${originName}{})
testVal := generateTest${elementName}()
assert.EqualValues(t, 7, es.Len())
Expand All @@ -413,46 +404,38 @@ func Test${structName}_CopyTo(t *testing.T) {
assert.EqualValues(t, generateTest${structName}(), dest)
}

func Test${structName}_Resize(t *testing.T) {
func Test${structName}_EnsureCapacity(t *testing.T) {
es := generateTest${structName}()
emptyVal := new${elementName}(&${originName}{})
// Test Resize less elements.
const resizeSmallLen = 4
expectedEs := make(map[*${originName}]bool, resizeSmallLen)
for i := 0; i < resizeSmallLen; i++ {
// Test ensure smaller capacity.
const ensureSmallLen = 4
expectedEs := make(map[*${originName}]bool)
for i := 0; i < es.Len(); i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, resizeSmallLen, len(expectedEs))
es.Resize(resizeSmallLen)
assert.Equal(t, resizeSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, resizeSmallLen)
assert.Equal(t, es.Len(), len(expectedEs))
es.EnsureCapacity(ensureSmallLen)
assert.Less(t, ensureSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, es.Len())
for i := 0; i < es.Len(); i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)

// Test Resize more elements.
const resizeLargeLen = 7
// Test ensure larger capacity
const ensureLargeLen = 9
oldLen := es.Len()
expectedEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, oldLen, len(expectedEs))
es.Resize(resizeLargeLen)
assert.Equal(t, resizeLargeLen, es.Len())
es.EnsureCapacity(ensureLargeLen)
assert.Equal(t, ensureLargeLen, cap(*es.orig))
foundEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)
for i := oldLen; i < resizeLargeLen; i++ {
assert.EqualValues(t, emptyVal, es.At(i))
}

// Test Resize 0 elements.
es.Resize(0)
assert.Equal(t, 0, es.Len())
}`

type baseSlice interface {
Expand Down
4 changes: 2 additions & 2 deletions exporter/prometheusremotewriteexporter/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func getTimeSeries(labels []prompb.Label, samples ...prompb.Sample) *prompb.Time

func getQuantiles(bounds []float64, values []float64) pdata.ValueAtQuantileSlice {
quantiles := pdata.NewValueAtQuantileSlice()
quantiles.Resize(len(bounds))
quantiles.AppendEmptyN(len(bounds))

for i := 0; i < len(bounds); i++ {
quantile := quantiles.At(i)
Expand Down Expand Up @@ -449,7 +449,7 @@ func getMetricsFromMetricList(metricList ...pdata.Metric) pdata.Metrics {

rm := metrics.ResourceMetrics().AppendEmpty()
ilm := rm.InstrumentationLibraryMetrics().AppendEmpty()
ilm.Metrics().Resize(len(metricList))
ilm.Metrics().AppendEmptyN(len(metricList))
for i := 0; i < len(metricList); i++ {
metricList[i].CopyTo(ilm.Metrics().At(i))
}
Expand Down
14 changes: 7 additions & 7 deletions internal/goldendataset/metrics_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func newMetricGenerator() metricGenerator {
func (g *metricGenerator) genMetricFromCfg(cfg MetricsCfg) pdata.Metrics {
md := pdata.NewMetrics()
rms := md.ResourceMetrics()
rms.Resize(cfg.NumResourceMetrics)
rms.AppendEmptyN(cfg.NumResourceMetrics)
for i := 0; i < cfg.NumResourceMetrics; i++ {
rm := rms.At(i)
resource := rm.Resource()
Expand All @@ -103,7 +103,7 @@ func (g *metricGenerator) genMetricFromCfg(cfg MetricsCfg) pdata.Metrics {

func (g *metricGenerator) populateIlm(cfg MetricsCfg, rm pdata.ResourceMetrics) {
ilms := rm.InstrumentationLibraryMetrics()
ilms.Resize(cfg.NumILMPerResource)
ilms.AppendEmptyN(cfg.NumILMPerResource)
for i := 0; i < cfg.NumILMPerResource; i++ {
ilm := ilms.At(i)
g.populateMetrics(cfg, ilm)
Expand All @@ -112,7 +112,7 @@ func (g *metricGenerator) populateIlm(cfg MetricsCfg, rm pdata.ResourceMetrics)

func (g *metricGenerator) populateMetrics(cfg MetricsCfg, ilm pdata.InstrumentationLibraryMetrics) {
metrics := ilm.Metrics()
metrics.Resize(cfg.NumMetricsPerILM)
metrics.AppendEmptyN(cfg.NumMetricsPerILM)
for i := 0; i < cfg.NumMetricsPerILM; i++ {
metric := metrics.At(i)
g.populateMetricDesc(cfg, metric)
Expand Down Expand Up @@ -157,7 +157,7 @@ func (g *metricGenerator) populateMetricDesc(cfg MetricsCfg, metric pdata.Metric
}

func populateIntPoints(cfg MetricsCfg, pts pdata.IntDataPointSlice) {
pts.Resize(cfg.NumPtsPerMetric)
pts.AppendEmptyN(cfg.NumPtsPerMetric)
for i := 0; i < cfg.NumPtsPerMetric; i++ {
pt := pts.At(i)
pt.SetStartTimestamp(pdata.Timestamp(cfg.StartTime))
Expand All @@ -168,7 +168,7 @@ func populateIntPoints(cfg MetricsCfg, pts pdata.IntDataPointSlice) {
}

func populateDoublePoints(cfg MetricsCfg, pts pdata.DoubleDataPointSlice) {
pts.Resize(cfg.NumPtsPerMetric)
pts.AppendEmptyN(cfg.NumPtsPerMetric)
for i := 0; i < cfg.NumPtsPerMetric; i++ {
pt := pts.At(i)
pt.SetStartTimestamp(pdata.Timestamp(cfg.StartTime))
Expand All @@ -180,7 +180,7 @@ func populateDoublePoints(cfg MetricsCfg, pts pdata.DoubleDataPointSlice) {

func populateDoubleHistogram(cfg MetricsCfg, dh pdata.Histogram) {
pts := dh.DataPoints()
pts.Resize(cfg.NumPtsPerMetric)
pts.AppendEmptyN(cfg.NumPtsPerMetric)
for i := 0; i < cfg.NumPtsPerMetric; i++ {
pt := pts.At(i)
pt.SetStartTimestamp(pdata.Timestamp(cfg.StartTime))
Expand Down Expand Up @@ -217,7 +217,7 @@ func addDoubleHistogramVal(hdp pdata.HistogramDataPoint, val float64) {

func populateIntHistogram(cfg MetricsCfg, dh pdata.IntHistogram) {
pts := dh.DataPoints()
pts.Resize(cfg.NumPtsPerMetric)
pts.AppendEmptyN(cfg.NumPtsPerMetric)
for i := 0; i < cfg.NumPtsPerMetric; i++ {
pt := pts.At(i)
pt.SetStartTimestamp(pdata.Timestamp(cfg.StartTime))
Expand Down
Loading