github.com/matrixorigin/matrixone@v0.7.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(b *testing.B, newFS func() FileService) {
    26  
    27  	b.Run("Read", func(b *testing.B) {
    28  		fs := newFS()
    29  		ctx := context.Background()
    30  
    31  		content := bytes.Repeat([]byte("x"), 16*1024*1024)
    32  		parts := fixedSplit(content, 512*1024)
    33  		writeVector := IOVector{
    34  			FilePath: "foo",
    35  		}
    36  		offset := int64(0)
    37  		for _, part := range parts {
    38  			writeVector.Entries = append(writeVector.Entries, IOEntry{
    39  				Offset: offset,
    40  				Size:   int64(len(part)),
    41  				Data:   part,
    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.ResetTimer()
    51  		for i := 0; i < b.N; i++ {
    52  			readVector := &IOVector{
    53  				FilePath: "foo",
    54  			}
    55  			offset = int64(0)
    56  			for _, part := range parts2 {
    57  				readVector.Entries = append(readVector.Entries, IOEntry{
    58  					Offset: offset,
    59  					Size:   int64(len(part)),
    60  				})
    61  				offset += int64(len(part))
    62  			}
    63  			err = fs.Read(ctx, readVector)
    64  			b.SetBytes(int64(len(content)))
    65  			assert.Nil(b, err)
    66  		}
    67  
    68  	})
    69  
    70  }