github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/file_service_bench_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  	"bytes"
    19  	"context"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func benchmarkFileService(ctx context.Context, b *testing.B, newFS func() FileService) {
    26  
    27  	b.Run("parallel raed", func(b *testing.B) {
    28  		fs := newFS()
    29  
    30  		content := bytes.Repeat([]byte("x"), 16*1024*1024)
    31  		parts := fixedSplit(content, 512*1024)
    32  		writeVector := IOVector{
    33  			FilePath: "foo",
    34  		}
    35  		offset := int64(0)
    36  		for _, part := range parts {
    37  			writeVector.Entries = append(writeVector.Entries, IOEntry{
    38  				Offset:      offset,
    39  				Size:        int64(len(part)),
    40  				Data:        part,
    41  				ToCacheData: CacheOriginalData,
    42  			})
    43  			offset += int64(len(part))
    44  		}
    45  		err := fs.Write(ctx, writeVector)
    46  		assert.Nil(b, err)
    47  
    48  		parts2 := fixedSplit(content, 4*1024)
    49  
    50  		b.SetBytes(int64(len(content)))
    51  		b.ResetTimer()
    52  
    53  		b.RunParallel(func(pb *testing.PB) {
    54  
    55  			readVector := &IOVector{
    56  				FilePath: "foo",
    57  			}
    58  			offset := int64(0)
    59  			for _, part := range parts2 {
    60  				readVector.Entries = append(readVector.Entries, IOEntry{
    61  					Offset:      offset,
    62  					Size:        int64(len(part)),
    63  					ToCacheData: CacheOriginalData,
    64  				})
    65  				offset += int64(len(part))
    66  			}
    67  
    68  			for pb.Next() {
    69  				for i := range readVector.Entries {
    70  					readVector.Entries[i].done = false
    71  					readVector.Entries[i].Data = nil
    72  				}
    73  				err := fs.Read(ctx, readVector)
    74  				assert.Nil(b, err)
    75  			}
    76  
    77  		})
    78  
    79  	})
    80  
    81  }