Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: benchmark tests for stat file #2659

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tools/integration_tests/benchmarking/benchmark_stat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package benchmarking

import (
"path"
"testing"

"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/benchmark_setup"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
)

////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////

type benchmarkStatTest struct{}

func (s *benchmarkStatTest) SetupB(t *testing.B) {
testDirPath = setup.SetupTestDirectory(testDirName)
}

func (s *benchmarkStatTest) TeardownB(t *testing.B) {}

// createFilesToStat creates the below object in the bucket.
// benchmarking/a.txt
func createFilesToStat(t *testing.B) {
operations.CreateFileOfSize(5, path.Join(testDirPath, "a.txt"), t)
}

////////////////////////////////////////////////////////////////////////
// Test scenarios
////////////////////////////////////////////////////////////////////////

func (s *benchmarkStatTest) Benchmark_Stat(b *testing.B) {
createFilesToStat(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := operations.StatFile(path.Join(testDirPath, "a.txt")); err != nil {
b.Errorf("testing error: %v", err)
}
}
}

////////////////////////////////////////////////////////////////////////
// Test Function (Runs once before all tests)
////////////////////////////////////////////////////////////////////////

func Benchmark_Stat(t *testing.B) {
ts := &benchmarkStatTest{}
benchmark_setup.RunBenchmarks(t, ts)
}
69 changes: 69 additions & 0 deletions tools/integration_tests/benchmarking/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package benchmarking

import (
"context"
"log"
"os"
"path"
"testing"

"cloud.google.com/go/storage"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/mounting/static_mounting"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
)

const (
testDirName = "benchmarking"
)

var (
storageClient *storage.Client
ctx context.Context
testDirPath string
)

func TestMain(m *testing.M) {
setup.ParseSetUpFlags()
setup.ExitWithFailureIfBothTestBucketAndMountedDirectoryFlagsAreNotSet()

// Create common storage client to be used in test.
ctx = context.Background()
closeStorageClient := client.CreateStorageClientWithCancel(&ctx, &storageClient)
defer func() {
if err := closeStorageClient(); err != nil {
log.Fatalf("closeStorageClient failed: %v", err)
}
}()

// If Mounted Directory flag is set, run tests for mounted directory.
setup.RunTestsForMountedDirectoryFlag(m)

// Else run tests for testBucket.
// Set up test directory.
setup.SetUpTestDirForTestBucketFlag()

flagsSet := [][]string{
{"--stat-cache-ttl=0"},
{"--client-protocol=grpc", "--stat-cache-ttl=0"},
}
successCode := static_mounting.RunTests(flagsSet, m)

// Clean up test directory created.
setup.CleanupDirectoryOnGCS(ctx, storageClient, path.Join(setup.TestBucket(), testDirName))
os.Exit(successCode)
}
20 changes: 20 additions & 0 deletions tools/integration_tests/run_benchmarking_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Provides example, how to run benchmarking tests.

# Use -benchtime=Nx to execute the BenchmarkXX N times.
# Use --testbucket to pass the bucket on which benchmark has to be run.
GODEBUG=asyncpreemptoff=1 go test ./tools/integration_tests/benchmarking/... -bench=. -benchtime=5x --integrationTest -v --testbucket=princer-empty-bucket
61 changes: 61 additions & 0 deletions tools/integration_tests/util/benchmark_setup/benchmark_setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package benchmark_setup

import (
"reflect"
"strings"
"testing"
)

type Benchmark interface {
SetupB(*testing.B)
TeardownB(*testing.B)
}

func getBenchmarkFunc(b *testing.B, xv reflect.Value, name string) func(*testing.B) {
if m := xv.MethodByName(name); m.IsValid() {
if f, ok := m.Interface().(func(*testing.B)); ok {
return f
}
// Method exists but has the wrong type signature.
b.Fatalf("benchmark function %v has unexpected signature (%T)", name, m.Interface())
}
return func(*testing.B) {}
}

// RunBenchmarks runs all "Benchmark*" functions that are members of x as sub-benchmarks
// of the current benchmark. SetupB is run before the benchmark function and TeardownB is
// run after each benchmark.
// x must extend Benchmark interface by implementing SetupB and TearDownB methods.
func RunBenchmarks(b *testing.B, x Benchmark) {
raj-prince marked this conversation as resolved.
Show resolved Hide resolved
xt := reflect.TypeOf(x)
xv := reflect.ValueOf(x)

for i := 0; i < xt.NumMethod(); i++ {
methodName := xt.Method(i).Name
if !strings.HasPrefix(methodName, "Benchmark") {
continue
}
benchmarkFunc := getBenchmarkFunc(b, xv, methodName)
b.Run(methodName, func(b *testing.B) {
// Execute TeardownB in b.Cleanup() to guarantee it is run even if benchmark
// function or setup uses b.Fatal().
b.Cleanup(func() { x.TeardownB(b) })
x.SetupB(b)
benchmarkFunc(b)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package benchmark_setup_test

import (
"testing"
"time"

"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/benchmark_setup"
"github.com/stretchr/testify/assert"
)

type benchmarkStructure struct {
setupCtr, teardownCtr, bench1, bench2 int
}

func (bs *benchmarkStructure) SetupB(*testing.B) {
bs.setupCtr++
}
func (bs *benchmarkStructure) BenchmarkExample1(*testing.B) {
// This is to ensure, right reading in the first go. Otherwise, it calls
// the method multiple times with different value of b.N.
time.Sleep(time.Second)
bs.bench1++
}

func (bs *benchmarkStructure) BenchmarkExample2(*testing.B) {
// This is to ensure, right reading in the first go. Otherwise, it calls
// the method multiple times with different value of b.N.
time.Sleep(time.Second)
bs.bench2++
}

func (bs *benchmarkStructure) TeardownB(*testing.B) {
bs.teardownCtr++
}

func BenchmarkRunBenchmarks(b *testing.B) {
benchmarkStruct := &benchmarkStructure{}

benchmark_setup.RunBenchmarks(b, benchmarkStruct)

assert.Equal(b, 2, benchmarkStruct.setupCtr)
assert.Equal(b, 1, benchmarkStruct.bench1)
assert.Equal(b, 1, benchmarkStruct.bench2)
assert.Equal(b, 2, benchmarkStruct.teardownCtr)
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func VerifyCountOfDirectoryEntries(expected, got int, t *testing.T) {
}
}

func CreateDirectory(dirPath string, t *testing.T) {
func CreateDirectory(dirPath string, t testing.TB) {
err := os.Mkdir(dirPath, DirPermission_0755)

// Verify MkDir operation succeeds.
Expand Down
8 changes: 4 additions & 4 deletions tools/integration_tests/util/operations/file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ func ClearCacheControlOnGcsObject(gcsObjPath string) error {
return err
}

func CreateFile(filePath string, filePerms os.FileMode, t *testing.T) (f *os.File) {
func CreateFile(filePath string, filePerms os.FileMode, t testing.TB) (f *os.File) {
// Creating a file shouldn't create file on GCS.
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, filePerms)
if err != nil {
Expand Down Expand Up @@ -548,7 +548,7 @@ func WriteWithoutClose(fh *os.File, content string, t *testing.T) {
}
}

func WriteAt(content string, offset int64, fh *os.File, t *testing.T) {
func WriteAt(content string, offset int64, fh *os.File, t testing.TB) {
_, err := fh.WriteAt([]byte(content), offset)
if err != nil {
t.Fatalf("%s.WriteAt(%s, %d): %v", fh.Name(), content, offset, err)
Expand All @@ -571,14 +571,14 @@ func SyncFile(fh *os.File, t *testing.T) {
}

func CreateFileWithContent(filePath string, filePerms os.FileMode,
content string, t *testing.T) {
content string, t testing.TB) {
fh := CreateFile(filePath, filePerms, t)
WriteAt(content, 0, fh, t)
CloseFile(fh)
}

// CreateFileOfSize creates a file of given size with random data.
func CreateFileOfSize(fileSize int64, filePath string, t *testing.T) {
func CreateFileOfSize(fileSize int64, filePath string, t testing.TB) {
randomData, err := GenerateRandomData(fileSize)
if err != nil {
t.Errorf("operations.GenerateRandomData: %v", err)
Expand Down
Loading