github.com/matrixorigin/matrixone@v1.2.0/pkg/util/metric/m_hardware_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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  
    15  package metric
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	prom "github.com/prometheus/client_golang/prometheus"
    24  	dto "github.com/prometheus/client_model/go"
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func TestHardwareCPU(t *testing.T) {
    29  	Convey("collect cpu succ", t, func() {
    30  		reg := prom.NewRegistry()
    31  		reg.MustRegister(newBatchStatsCollector(cpuPercent{}, cpuTotal{}))
    32  
    33  		mf, err := reg.Gather()
    34  		So(err, ShouldBeNil)
    35  		So(len(mf), ShouldEqual, 2)
    36  		// order by metric name
    37  		So(mf[0].GetType(), ShouldEqual, dto.MetricType_GAUGE)
    38  		So(mf[1].GetType(), ShouldEqual, dto.MetricType_COUNTER)
    39  
    40  		time.Sleep(time.Second)
    41  		mf2, err := reg.Gather()
    42  		So(err, ShouldBeNil)
    43  		So(len(mf2), ShouldEqual, 2)
    44  
    45  		deltaBusy := mf2[1].Metric[0].Counter.GetValue() - mf[1].Metric[0].Counter.GetValue()
    46  		deltaPercent := mf2[0].Metric[0].Gauge.GetValue()
    47  
    48  		So(deltaBusy*100, ShouldAlmostEqual, deltaPercent, 60 /* 60% diff will be ok anyway */)
    49  	})
    50  }
    51  
    52  func TestHardwareMem(t *testing.T) {
    53  	Convey("collect mem succ", t, func() {
    54  		reg := prom.NewRegistry()
    55  		reg.MustRegister(newBatchStatsCollector(memAvail{}, memUsed{}))
    56  
    57  		mf, err := reg.Gather()
    58  		So(err, ShouldBeNil)
    59  		So(len(mf), ShouldEqual, 2)
    60  	})
    61  }
    62  
    63  type errorMetric struct{}
    64  
    65  func (c errorMetric) Desc() *prom.Desc {
    66  	return prom.NewDesc(
    67  		"test_error_metric",
    68  		"a metric returning errors",
    69  		nil, nil,
    70  	)
    71  }
    72  
    73  func (c errorMetric) Metric(ctx context.Context, _ *statCaches) (prom.Metric, error) {
    74  	return nil, moerr.NewInternalError(ctx, "Something went wrong")
    75  }
    76  
    77  func TestHardwareError(t *testing.T) {
    78  	Convey("collect no error metric", t, func() {
    79  		reg := prom.NewRegistry()
    80  		reg.MustRegister(newBatchStatsCollector(errorMetric{}))
    81  
    82  		mf, err := reg.Gather()
    83  		So(err, ShouldBeNil)
    84  		So(len(mf), ShouldEqual, 0)
    85  	})
    86  }