diff --git a/metricsdecorator.go b/metricsdecorator.go index b83447d2..99637d3b 100644 --- a/metricsdecorator.go +++ b/metricsdecorator.go @@ -8,8 +8,8 @@ import ( ) type metricsDecorator struct { + addRowsTime []*metrics.Time addRowTime []*metrics.Time - addColTime []*metrics.Time addPageTime []*metrics.Time addPDFTime []*metrics.Time headerTime *metrics.Time @@ -58,7 +58,7 @@ func (m *metricsDecorator) AddRows(rows ...core.Row) { m.inner.AddRows(rows...) }) - m.addRowTime = append(m.addRowTime, timeSpent) + m.addRowsTime = append(m.addRowsTime, timeSpent) } func (m *metricsDecorator) AddRow(rowHeight float64, cols ...core.Col) core.Row { @@ -67,7 +67,7 @@ func (m *metricsDecorator) AddRow(rowHeight float64, cols ...core.Col) core.Row r = m.inner.AddRow(rowHeight, cols...) }) - m.addColTime = append(m.addColTime, timeSpent) + m.addRowTime = append(m.addRowTime, timeSpent) return r } @@ -151,11 +151,11 @@ func (m *metricsDecorator) buildMetrics(bytesSize int) *metrics.Report { }) } - if len(m.addColTime) > 0 { + if len(m.addRowsTime) > 0 { timeMetrics = append(timeMetrics, metrics.TimeMetric{ - Key: "add_cols", - Times: m.addColTime, - Avg: m.getAVG(m.addColTime), + Key: "add_rows", + Times: m.addRowsTime, + Avg: m.getAVG(m.addRowsTime), }) } @@ -180,10 +180,6 @@ func (m *metricsDecorator) buildMetrics(bytesSize int) *metrics.Report { } func (m *metricsDecorator) getAVG(times []*metrics.Time) *metrics.Time { - if len(times) == 0 { - return nil - } - var sum float64 for _, time := range times { sum += time.Value diff --git a/metricsdecorator_test.go b/metricsdecorator_test.go new file mode 100644 index 00000000..b42debeb --- /dev/null +++ b/metricsdecorator_test.go @@ -0,0 +1,48 @@ +package maroto + +import ( + "fmt" + "testing" + + "github.com/johnfercher/maroto/v2/mocks" + "github.com/johnfercher/maroto/v2/pkg/components/page" + "github.com/stretchr/testify/assert" +) + +func TestNewMetricsDecorator(t *testing.T) { + // Act + sut := NewMetricsDecorator(nil) + + // Assert + assert.NotNil(t, sut) + assert.Equal(t, "*maroto.metricsDecorator", fmt.Sprintf("%T", sut)) +} + +func TestMetricsDecorator_AddPages(t *testing.T) { + // Arrange + pg := page.New() + + docToReturn := &mocks.Document{} + docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3}) + inner := &mocks.Maroto{} + inner.EXPECT().AddPages(pg) + inner.EXPECT().Generate().Return(docToReturn, nil) + + sut := NewMetricsDecorator(inner) + + // Act + sut.AddPages(pg) + sut.AddPages(pg) + + // Assert + doc, err := sut.Generate() + assert.Nil(t, err) + assert.NotNil(t, doc) + + report := doc.GetReport() + assert.NotNil(t, report) + assert.Equal(t, 2, len(report.TimeMetrics)) + assert.Equal(t, "generate", report.TimeMetrics[0].Key) + assert.Equal(t, "add_page", report.TimeMetrics[1].Key) + assert.Equal(t, 2, len(report.TimeMetrics[1].Times)) +}