github.com/kaydxh/golang@v0.0.131/pkg/database/redis/redis_benchmark_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package redis_test
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/google/uuid"
    31  )
    32  
    33  // -count the benchmark times, -benchtime the test execute times(用例执行次数) or execute time(用例执行时间)
    34  // -v detail log info , -benchmem summary of memory
    35  //go test -bench="Set" -benchtime=5s -count=3 .
    36  //go test -bench="Set" -benchtime=50x -count=3 .
    37  func BenchmarkSet(t *testing.B) {
    38  	db := GetDBOrDie()
    39  	//	defer db.Close()
    40  
    41  	keyPrefix := "test"
    42  
    43  	for n := 0; n < t.N; n++ {
    44  		fmt.Println("n: ", n)
    45  
    46  		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    47  		defer cancel()
    48  
    49  		key := fmt.Sprintf("%s-%d", keyPrefix, n)
    50  		result, err := db.Set(ctx, key, key, 0).Result()
    51  		if err != nil {
    52  			t.Fatalf("failed to set string, err: %v", err)
    53  		}
    54  
    55  		t.Logf("result of %v: %v", key, result)
    56  	}
    57  }
    58  
    59  //go test -v -run=redis_benchmark_test.go -test.bench="ParallelSet" -benchtime=5s -count=3 .
    60  //go test -v -run=redis_benchmark_test.go -test.bench="ParallelSet" -benchtime=5s -count=3 .
    61  func BenchmarkParallelSet(t *testing.B) {
    62  	db := GetDBOrDie()
    63  	//	defer db.Close()
    64  
    65  	keyPrefix := "test"
    66  
    67  	t.RunParallel(func(pb *testing.PB) {
    68  		for pb.Next() {
    69  			ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    70  			defer cancel()
    71  
    72  			key := fmt.Sprintf("%s-%s", keyPrefix, uuid.New().String())
    73  
    74  			result, err := db.Set(ctx, key, key, 0).Result()
    75  			if err != nil {
    76  				t.Fatalf("failed to set string, err: %v", err)
    77  			}
    78  
    79  			t.Logf("result of %v: %v", key, result)
    80  		}
    81  	})
    82  
    83  }