1// Copyright The OpenTelemetry Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package otlp 16 17import ( 18 "testing" 19 "time" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 24 "go.opentelemetry.io/collector/model/pdata" 25) 26 27func TestProtobufLogsUnmarshaler_error(t *testing.T) { 28 p := NewProtobufLogsUnmarshaler() 29 _, err := p.UnmarshalLogs([]byte("+$%")) 30 assert.Error(t, err) 31} 32 33func TestProtobufMetricsUnmarshaler_error(t *testing.T) { 34 p := NewProtobufMetricsUnmarshaler() 35 _, err := p.UnmarshalMetrics([]byte("+$%")) 36 assert.Error(t, err) 37} 38 39func TestProtobufTracesUnmarshaler_error(t *testing.T) { 40 p := NewProtobufTracesUnmarshaler() 41 _, err := p.UnmarshalTraces([]byte("+$%")) 42 assert.Error(t, err) 43} 44 45func BenchmarkLogsToProtobuf(b *testing.B) { 46 marshaler := NewProtobufLogsMarshaler() 47 logs := generateBenchmarkLogs(128) 48 b.ResetTimer() 49 for n := 0; n < b.N; n++ { 50 buf, err := marshaler.MarshalLogs(logs) 51 require.NoError(b, err) 52 assert.NotEqual(b, 0, len(buf)) 53 } 54} 55 56func BenchmarkLogsFromProtobuf(b *testing.B) { 57 marshaler := NewProtobufLogsMarshaler() 58 unmarshaler := NewProtobufLogsUnmarshaler() 59 baseLogs := generateBenchmarkLogs(128) 60 buf, err := marshaler.MarshalLogs(baseLogs) 61 require.NoError(b, err) 62 assert.NotEqual(b, 0, len(buf)) 63 b.ResetTimer() 64 b.ReportAllocs() 65 for n := 0; n < b.N; n++ { 66 logs, err := unmarshaler.UnmarshalLogs(buf) 67 require.NoError(b, err) 68 assert.Equal(b, baseLogs.ResourceLogs().Len(), logs.ResourceLogs().Len()) 69 } 70} 71 72func BenchmarkMetricsToProtobuf(b *testing.B) { 73 marshaler := NewProtobufMetricsMarshaler() 74 metrics := generateBenchmarkMetrics(128) 75 b.ResetTimer() 76 for n := 0; n < b.N; n++ { 77 buf, err := marshaler.MarshalMetrics(metrics) 78 require.NoError(b, err) 79 assert.NotEqual(b, 0, len(buf)) 80 } 81} 82 83func BenchmarkMetricsFromProtobuf(b *testing.B) { 84 marshaler := NewProtobufMetricsMarshaler() 85 unmarshaler := NewProtobufMetricsUnmarshaler() 86 baseMetrics := generateBenchmarkMetrics(128) 87 buf, err := marshaler.MarshalMetrics(baseMetrics) 88 require.NoError(b, err) 89 assert.NotEqual(b, 0, len(buf)) 90 b.ResetTimer() 91 b.ReportAllocs() 92 for n := 0; n < b.N; n++ { 93 metrics, err := unmarshaler.UnmarshalMetrics(buf) 94 require.NoError(b, err) 95 assert.Equal(b, baseMetrics.ResourceMetrics().Len(), metrics.ResourceMetrics().Len()) 96 } 97} 98 99func BenchmarkTracesToProtobuf(b *testing.B) { 100 marshaler := NewProtobufTracesMarshaler() 101 traces := generateBenchmarkTraces(128) 102 b.ResetTimer() 103 for n := 0; n < b.N; n++ { 104 buf, err := marshaler.MarshalTraces(traces) 105 require.NoError(b, err) 106 assert.NotEqual(b, 0, len(buf)) 107 } 108} 109 110func BenchmarkTracesFromProtobuf(b *testing.B) { 111 marshaler := NewProtobufTracesMarshaler() 112 unmarshaler := NewProtobufTracesUnmarshaler() 113 baseTraces := generateBenchmarkTraces(128) 114 buf, err := marshaler.MarshalTraces(baseTraces) 115 require.NoError(b, err) 116 assert.NotEqual(b, 0, len(buf)) 117 b.ResetTimer() 118 b.ReportAllocs() 119 for n := 0; n < b.N; n++ { 120 traces, err := unmarshaler.UnmarshalTraces(buf) 121 require.NoError(b, err) 122 assert.Equal(b, baseTraces.ResourceSpans().Len(), traces.ResourceSpans().Len()) 123 } 124} 125 126func generateBenchmarkLogs(logsCount int) pdata.Logs { 127 endTime := pdata.TimestampFromTime(time.Now()) 128 129 md := pdata.NewLogs() 130 ilm := md.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty() 131 ilm.Logs().EnsureCapacity(logsCount) 132 for i := 0; i < logsCount; i++ { 133 im := ilm.Logs().AppendEmpty() 134 im.SetName("test_name") 135 im.SetTimestamp(endTime) 136 } 137 return md 138} 139 140func generateBenchmarkMetrics(metricsCount int) pdata.Metrics { 141 now := time.Now() 142 startTime := pdata.TimestampFromTime(now.Add(-10 * time.Second)) 143 endTime := pdata.TimestampFromTime(now) 144 145 md := pdata.NewMetrics() 146 ilm := md.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty() 147 ilm.Metrics().EnsureCapacity(metricsCount) 148 for i := 0; i < metricsCount; i++ { 149 im := ilm.Metrics().AppendEmpty() 150 im.SetName("test_name") 151 im.SetDataType(pdata.MetricDataTypeSum) 152 idp := im.Sum().DataPoints().AppendEmpty() 153 idp.SetStartTimestamp(startTime) 154 idp.SetTimestamp(endTime) 155 idp.SetIntVal(123) 156 } 157 return md 158} 159 160func generateBenchmarkTraces(metricsCount int) pdata.Traces { 161 now := time.Now() 162 startTime := pdata.TimestampFromTime(now.Add(-10 * time.Second)) 163 endTime := pdata.TimestampFromTime(now) 164 165 md := pdata.NewTraces() 166 ilm := md.ResourceSpans().AppendEmpty().InstrumentationLibrarySpans().AppendEmpty() 167 ilm.Spans().EnsureCapacity(metricsCount) 168 for i := 0; i < metricsCount; i++ { 169 im := ilm.Spans().AppendEmpty() 170 im.SetName("test_name") 171 im.SetStartTimestamp(startTime) 172 im.SetEndTimestamp(endTime) 173 } 174 return md 175} 176