github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/memory_fs_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 fileservice
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/perfcounter"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestMemoryFS(t *testing.T) {
    27  
    28  	t.Run("file service", func(t *testing.T) {
    29  		testFileService(t, 0, func(name string) FileService {
    30  			fs, err := NewMemoryFS(name, DisabledCacheConfig, nil)
    31  			assert.Nil(t, err)
    32  			return fs
    33  		})
    34  	})
    35  
    36  	t.Run("replaceable file service", func(t *testing.T) {
    37  		testReplaceableFileService(t, func() ReplaceableFileService {
    38  			fs, err := NewMemoryFS("memory", DisabledCacheConfig, nil)
    39  			assert.Nil(t, err)
    40  			return fs
    41  		})
    42  	})
    43  
    44  }
    45  
    46  func BenchmarkMemoryFS(b *testing.B) {
    47  	benchmarkFileService(context.Background(), b, func() FileService {
    48  		fs, err := NewMemoryFS("memory", DisabledCacheConfig, nil)
    49  		assert.Nil(b, err)
    50  		return fs
    51  	})
    52  }
    53  
    54  func BenchmarkMemoryFSWithMemoryCache(b *testing.B) {
    55  	ctx := context.Background()
    56  	var counterSet perfcounter.CounterSet
    57  	ctx = perfcounter.WithCounterSet(ctx, &counterSet)
    58  
    59  	benchmarkFileService(ctx, b, func() FileService {
    60  		fs, err := NewMemoryFS("memory", DisabledCacheConfig, nil)
    61  		assert.Nil(b, err)
    62  		fs.caches = append(fs.caches, NewMemCache(
    63  			NewMemoryCache(128*1024*1024, true, nil),
    64  			nil,
    65  		))
    66  		return fs
    67  	})
    68  
    69  	read := counterSet.FileService.Cache.Memory.Read.Load()
    70  	hit := counterSet.FileService.Cache.Memory.Hit.Load()
    71  	fmt.Printf("hit rate: %v / %v = %.4v\n", hit, read, float64(hit)/float64(read))
    72  	fmt.Printf("capacity %+v\n", counterSet.FileService.Cache.Memory.Capacity.Load())
    73  	fmt.Printf("used %+v\n", counterSet.FileService.Cache.Memory.Used.Load())
    74  }
    75  
    76  func BenchmarkMemoryFSWithMemoryCacheLowCapacity(b *testing.B) {
    77  	ctx := context.Background()
    78  	var counterSet perfcounter.CounterSet
    79  	ctx = perfcounter.WithCounterSet(ctx, &counterSet)
    80  
    81  	benchmarkFileService(ctx, b, func() FileService {
    82  		fs, err := NewMemoryFS("memory", DisabledCacheConfig, nil)
    83  		assert.Nil(b, err)
    84  		fs.caches = append(fs.caches, NewMemCache(
    85  			NewMemoryCache(2*1024*1024, true, nil),
    86  			nil,
    87  		))
    88  		return fs
    89  	})
    90  
    91  	read := counterSet.FileService.Cache.Memory.Read.Load()
    92  	hit := counterSet.FileService.Cache.Memory.Hit.Load()
    93  	fmt.Printf("hit rate: %v / %v = %.4v\n", hit, read, float64(hit)/float64(read))
    94  	fmt.Printf("capacity %+v\n", counterSet.FileService.Cache.Memory.Capacity.Load())
    95  	fmt.Printf("used %+v\n", counterSet.FileService.Cache.Memory.Used.Load())
    96  }