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

     1  // Copyright 2023 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 stats
    16  
    17  import (
    18  	"github.com/stretchr/testify/assert"
    19  	"go.uber.org/zap"
    20  	"testing"
    21  )
    22  
    23  // MockService for testing stats Registry
    24  type MockService struct {
    25  	stats *MockStats
    26  }
    27  
    28  type MockStats struct {
    29  	reads Counter
    30  	hits  Counter
    31  }
    32  
    33  func NewMockService() *MockService {
    34  	return &MockService{
    35  		stats: &MockStats{},
    36  	}
    37  }
    38  
    39  func (d *MockService) Do() {
    40  	d.stats.reads.Add(2)
    41  	d.stats.hits.Add(1)
    42  }
    43  
    44  func (d *MockService) Stats() *MockStats {
    45  	return d.stats
    46  }
    47  
    48  // LogExporter for the Mock Service declared above
    49  type MockServiceLogExporter struct {
    50  	service *MockService
    51  }
    52  
    53  func NewMockServiceLogExporter(service *MockService) LogExporter {
    54  	return &MockServiceLogExporter{
    55  		service: service,
    56  	}
    57  }
    58  
    59  func (c *MockServiceLogExporter) Export() []zap.Field {
    60  	var fields []zap.Field
    61  
    62  	stats := c.service.Stats()
    63  
    64  	reads := stats.reads.SwapW(0)
    65  	hits := stats.hits.SwapW(0)
    66  
    67  	fields = append(fields, zap.Any("reads", reads))
    68  	fields = append(fields, zap.Any("hits", hits))
    69  
    70  	return fields
    71  }
    72  
    73  func TestRegister(t *testing.T) {
    74  	// 1. Initialize service
    75  	service := NewMockService()
    76  
    77  	// 2. Initialize LogExporter for the service
    78  	serviceLogExporter := NewMockServiceLogExporter(service)
    79  
    80  	// 3. Register LogExporter to the default stats registry
    81  	Register("MockServiceStats1", WithLogExporter(serviceLogExporter))
    82  
    83  	assert.Equal(t, 1, len(DefaultRegistry.families))
    84  	assert.Equal(t, serviceLogExporter, DefaultRegistry.families["MockServiceStats1"].logExporter)
    85  }
    86  
    87  func TestExportLog(t *testing.T) {
    88  	// 1. Initialize service
    89  	service := NewMockService()
    90  
    91  	// 2. Initialize LogExporter for the service
    92  	serviceLogExporter := NewMockServiceLogExporter(service)
    93  
    94  	// 3. Register LogExporter to the default stats registry
    95  	Register("MockServiceStats2", WithLogExporter(serviceLogExporter))
    96  
    97  	// 4. Let the service perform some operations
    98  	service.Do()
    99  	service.Do()
   100  	service.Do()
   101  
   102  	//5. Call ExportLog for exporting the snapshots of registered stats.
   103  	result := DefaultRegistry.ExportLog()
   104  
   105  	assert.Equal(t, 2, len(result["MockServiceStats2"]))
   106  	assert.Equal(t, zap.Any("reads", 6), result["MockServiceStats2"][0])
   107  	assert.Equal(t, zap.Any("hits", 3), result["MockServiceStats2"][1])
   108  }