github.com/aaabigfish/gopkg@v1.1.0/cloud/metainfo/info_test.go (about)

     1  // Copyright 2021 ByteDance Inc.
     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 metainfo_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/aaabigfish/gopkg/cloud/metainfo"
    23  )
    24  
    25  func TestWithValue(t *testing.T) {
    26  	ctx := context.Background()
    27  
    28  	k, v := "Key", "Value"
    29  	ctx = metainfo.WithValue(ctx, k, v)
    30  	assert(t, ctx != nil)
    31  
    32  	x, ok := metainfo.GetValue(ctx, k)
    33  	assert(t, ok)
    34  	assert(t, x == v)
    35  }
    36  
    37  func TestWithValues(t *testing.T) {
    38  	ctx := context.Background()
    39  
    40  	k, v := "Key", "Value"
    41  	ctx = metainfo.WithValue(ctx, k, v)
    42  
    43  	kvs := []string{"Key-1", "Value-1", "Key-2", "Value-2", "Key-3", "Value-3"}
    44  	ctx = metainfo.WithValues(ctx, kvs...)
    45  	assert(t, ctx != nil)
    46  
    47  	for i := 1; i <= 3; i++ {
    48  		x, ok := metainfo.GetValue(ctx, fmt.Sprintf("Key-%d", i))
    49  		assert(t, ok)
    50  		assert(t, x == fmt.Sprintf("Value-%d", i))
    51  	}
    52  }
    53  
    54  func TestWithEmpty(t *testing.T) {
    55  	ctx := context.Background()
    56  
    57  	k, v := "Key", "Value"
    58  	ctx = metainfo.WithValue(ctx, k, "")
    59  	assert(t, ctx != nil)
    60  
    61  	_, ok := metainfo.GetValue(ctx, k)
    62  	assert(t, !ok)
    63  
    64  	ctx = metainfo.WithValue(ctx, "", v)
    65  	assert(t, ctx != nil)
    66  
    67  	_, ok = metainfo.GetValue(ctx, "")
    68  	assert(t, !ok)
    69  }
    70  
    71  func TestDelValue(t *testing.T) {
    72  	ctx := context.Background()
    73  
    74  	k, v := "Key", "Value"
    75  	ctx = metainfo.WithValue(ctx, k, v)
    76  	assert(t, ctx != nil)
    77  
    78  	x, ok := metainfo.GetValue(ctx, k)
    79  	assert(t, ok)
    80  	assert(t, x == v)
    81  
    82  	ctx = metainfo.DelValue(ctx, k)
    83  	assert(t, ctx != nil)
    84  
    85  	x, ok = metainfo.GetValue(ctx, k)
    86  	assert(t, !ok)
    87  
    88  	assert(t, metainfo.DelValue(ctx, "") == ctx)
    89  }
    90  
    91  func TestGetAll(t *testing.T) {
    92  	ctx := context.Background()
    93  
    94  	ss := []string{"1", "2", "3"}
    95  	for _, k := range ss {
    96  		ctx = metainfo.WithValue(ctx, "key"+k, "val"+k)
    97  	}
    98  
    99  	m := metainfo.GetAllValues(ctx)
   100  	assert(t, m != nil)
   101  	assert(t, len(m) == len(ss))
   102  
   103  	for _, k := range ss {
   104  		assert(t, m["key"+k] == "val"+k)
   105  	}
   106  }
   107  
   108  func TestRangeValues(t *testing.T) {
   109  	ctx := context.Background()
   110  
   111  	ss := []string{"1", "2", "3"}
   112  	for _, k := range ss {
   113  		ctx = metainfo.WithValue(ctx, "key"+k, "val"+k)
   114  	}
   115  
   116  	m := make(map[string]string, 3)
   117  	f := func(k, v string) bool {
   118  		m[k] = v
   119  		return true
   120  	}
   121  
   122  	metainfo.RangeValues(ctx, f)
   123  	assert(t, m != nil)
   124  	assert(t, len(m) == len(ss))
   125  
   126  	for _, k := range ss {
   127  		assert(t, m["key"+k] == "val"+k)
   128  	}
   129  }
   130  
   131  func TestGetAll2(t *testing.T) {
   132  	ctx := context.Background()
   133  
   134  	ss := []string{"1", "2", "3"}
   135  	for _, k := range ss {
   136  		ctx = metainfo.WithValue(ctx, "key"+k, "val"+k)
   137  	}
   138  
   139  	ctx = metainfo.DelValue(ctx, "key2")
   140  
   141  	m := metainfo.GetAllValues(ctx)
   142  	assert(t, m != nil)
   143  	assert(t, len(m) == len(ss)-1)
   144  
   145  	for _, k := range ss {
   146  		if k == "2" {
   147  			_, exist := m["key"+k]
   148  			assert(t, !exist)
   149  		} else {
   150  			assert(t, m["key"+k] == "val"+k)
   151  		}
   152  	}
   153  }
   154  
   155  ///////////////////////////////////////////////
   156  
   157  func TestWithPersistentValue(t *testing.T) {
   158  	ctx := context.Background()
   159  
   160  	k, v := "Key", "Value"
   161  	ctx = metainfo.WithPersistentValue(ctx, k, v)
   162  	assert(t, ctx != nil)
   163  
   164  	x, ok := metainfo.GetPersistentValue(ctx, k)
   165  	assert(t, ok)
   166  	assert(t, x == v)
   167  }
   168  
   169  func TestWithPersistentEmpty(t *testing.T) {
   170  	ctx := context.Background()
   171  
   172  	k, v := "Key", "Value"
   173  	ctx = metainfo.WithPersistentValue(ctx, k, "")
   174  	assert(t, ctx != nil)
   175  
   176  	_, ok := metainfo.GetPersistentValue(ctx, k)
   177  	assert(t, !ok)
   178  
   179  	ctx = metainfo.WithPersistentValue(ctx, "", v)
   180  	assert(t, ctx != nil)
   181  
   182  	_, ok = metainfo.GetPersistentValue(ctx, "")
   183  	assert(t, !ok)
   184  }
   185  
   186  func TestWithPersistentValues(t *testing.T) {
   187  	ctx := context.Background()
   188  
   189  	kvs := []string{"Key-1", "Value-1", "Key-2", "Value-2", "Key-3", "Value-3"}
   190  	ctx = metainfo.WithPersistentValues(ctx, kvs...)
   191  	assert(t, ctx != nil)
   192  
   193  	for i := 1; i <= 3; i++ {
   194  		x, ok := metainfo.GetPersistentValue(ctx, fmt.Sprintf("Key-%d", i))
   195  		assert(t, ok)
   196  		assert(t, x == fmt.Sprintf("Value-%d", i))
   197  	}
   198  }
   199  
   200  func TestWithPersistentValuesEmpty(t *testing.T) {
   201  	ctx := context.Background()
   202  
   203  	k, v := "Key", "Value"
   204  	kvs := []string{"", v, k, ""}
   205  
   206  	ctx = metainfo.WithPersistentValues(ctx, kvs...)
   207  	assert(t, ctx != nil)
   208  
   209  	_, ok := metainfo.GetPersistentValue(ctx, k)
   210  	assert(t, !ok)
   211  
   212  	_, ok = metainfo.GetPersistentValue(ctx, "")
   213  	assert(t, !ok)
   214  }
   215  
   216  func TestWithPersistentValuesRepeat(t *testing.T) {
   217  	ctx := context.Background()
   218  
   219  	kvs := []string{"Key", "Value-1", "Key", "Value-2", "Key", "Value-3"}
   220  
   221  	ctx = metainfo.WithPersistentValues(ctx, kvs...)
   222  	assert(t, ctx != nil)
   223  
   224  	x, ok := metainfo.GetPersistentValue(ctx, "Key")
   225  	assert(t, ok)
   226  	assert(t, x == "Value-3")
   227  }
   228  
   229  func TestDelPersistentValue(t *testing.T) {
   230  	ctx := context.Background()
   231  
   232  	k, v := "Key", "Value"
   233  	ctx = metainfo.WithPersistentValue(ctx, k, v)
   234  	assert(t, ctx != nil)
   235  
   236  	x, ok := metainfo.GetPersistentValue(ctx, k)
   237  	assert(t, ok)
   238  	assert(t, x == v)
   239  
   240  	ctx = metainfo.DelPersistentValue(ctx, k)
   241  	assert(t, ctx != nil)
   242  
   243  	x, ok = metainfo.GetPersistentValue(ctx, k)
   244  	assert(t, !ok)
   245  
   246  	assert(t, metainfo.DelPersistentValue(ctx, "") == ctx)
   247  }
   248  
   249  func TestGetAllPersistent(t *testing.T) {
   250  	ctx := context.Background()
   251  
   252  	ss := []string{"1", "2", "3"}
   253  	for _, k := range ss {
   254  		ctx = metainfo.WithPersistentValue(ctx, "key"+k, "val"+k)
   255  	}
   256  
   257  	m := metainfo.GetAllPersistentValues(ctx)
   258  	assert(t, m != nil)
   259  	assert(t, len(m) == len(ss))
   260  
   261  	for _, k := range ss {
   262  		assert(t, m["key"+k] == "val"+k)
   263  	}
   264  }
   265  
   266  func TestRangePersistent(t *testing.T) {
   267  	ctx := context.Background()
   268  
   269  	ss := []string{"1", "2", "3"}
   270  	for _, k := range ss {
   271  		ctx = metainfo.WithPersistentValue(ctx, "key"+k, "val"+k)
   272  	}
   273  
   274  	m := make(map[string]string, 3)
   275  	f := func(k, v string) bool {
   276  		m[k] = v
   277  		return true
   278  	}
   279  
   280  	metainfo.RangePersistentValues(ctx, f)
   281  	assert(t, m != nil)
   282  	assert(t, len(m) == len(ss))
   283  
   284  	for _, k := range ss {
   285  		assert(t, m["key"+k] == "val"+k)
   286  	}
   287  }
   288  
   289  func TestGetAllPersistent2(t *testing.T) {
   290  	ctx := context.Background()
   291  
   292  	ss := []string{"1", "2", "3"}
   293  	for _, k := range ss {
   294  		ctx = metainfo.WithPersistentValue(ctx, "key"+k, "val"+k)
   295  	}
   296  
   297  	ctx = metainfo.DelPersistentValue(ctx, "key2")
   298  
   299  	m := metainfo.GetAllPersistentValues(ctx)
   300  	assert(t, m != nil)
   301  	assert(t, len(m) == len(ss)-1)
   302  
   303  	for _, k := range ss {
   304  		if k == "2" {
   305  			_, exist := m["key"+k]
   306  			assert(t, !exist)
   307  		} else {
   308  			assert(t, m["key"+k] == "val"+k)
   309  		}
   310  	}
   311  }
   312  
   313  ///////////////////////////////////////////////
   314  
   315  func TestNilSafty(t *testing.T) {
   316  	assert(t, metainfo.TransferForward(nil) == nil)
   317  
   318  	_, tOK := metainfo.GetValue(nil, "any")
   319  	assert(t, !tOK)
   320  	assert(t, metainfo.GetAllValues(nil) == nil)
   321  	assert(t, metainfo.WithValue(nil, "any", "any") == nil)
   322  	assert(t, metainfo.DelValue(nil, "any") == nil)
   323  
   324  	_, pOK := metainfo.GetPersistentValue(nil, "any")
   325  	assert(t, !pOK)
   326  	assert(t, metainfo.GetAllPersistentValues(nil) == nil)
   327  	assert(t, metainfo.WithPersistentValue(nil, "any", "any") == nil)
   328  	assert(t, metainfo.DelPersistentValue(nil, "any") == nil)
   329  }
   330  
   331  func TestTransitAndPersistent(t *testing.T) {
   332  	ctx := context.Background()
   333  
   334  	ctx = metainfo.WithValue(ctx, "A", "a")
   335  	ctx = metainfo.WithPersistentValue(ctx, "A", "b")
   336  
   337  	x, xOK := metainfo.GetValue(ctx, "A")
   338  	y, yOK := metainfo.GetPersistentValue(ctx, "A")
   339  
   340  	assert(t, xOK)
   341  	assert(t, yOK)
   342  	assert(t, x == "a")
   343  	assert(t, y == "b")
   344  
   345  	_, uOK := metainfo.GetValue(ctx, "B")
   346  	_, vOK := metainfo.GetPersistentValue(ctx, "B")
   347  
   348  	assert(t, !uOK)
   349  	assert(t, !vOK)
   350  
   351  	ctx = metainfo.DelValue(ctx, "A")
   352  	_, pOK := metainfo.GetValue(ctx, "A")
   353  	q, qOK := metainfo.GetPersistentValue(ctx, "A")
   354  	assert(t, !pOK)
   355  	assert(t, qOK)
   356  	assert(t, q == "b")
   357  }
   358  
   359  func TestTransferForward(t *testing.T) {
   360  	ctx := context.Background()
   361  
   362  	ctx = metainfo.WithValue(ctx, "A", "t")
   363  	ctx = metainfo.WithPersistentValue(ctx, "A", "p")
   364  	ctx = metainfo.WithValue(ctx, "A", "ta")
   365  	ctx = metainfo.WithPersistentValue(ctx, "A", "pa")
   366  
   367  	ctx = metainfo.TransferForward(ctx)
   368  	assert(t, ctx != nil)
   369  
   370  	x, xOK := metainfo.GetValue(ctx, "A")
   371  	y, yOK := metainfo.GetPersistentValue(ctx, "A")
   372  
   373  	assert(t, xOK)
   374  	assert(t, yOK)
   375  	assert(t, x == "ta")
   376  	assert(t, y == "pa")
   377  
   378  	ctx = metainfo.TransferForward(ctx)
   379  	assert(t, ctx != nil)
   380  
   381  	x, xOK = metainfo.GetValue(ctx, "A")
   382  	y, yOK = metainfo.GetPersistentValue(ctx, "A")
   383  
   384  	assert(t, !xOK)
   385  	assert(t, yOK)
   386  	assert(t, y == "pa")
   387  
   388  	ctx = metainfo.WithValue(ctx, "B", "tb")
   389  
   390  	ctx = metainfo.TransferForward(ctx)
   391  	assert(t, ctx != nil)
   392  
   393  	y, yOK = metainfo.GetPersistentValue(ctx, "A")
   394  	z, zOK := metainfo.GetValue(ctx, "B")
   395  
   396  	assert(t, yOK)
   397  	assert(t, y == "pa")
   398  	assert(t, zOK)
   399  	assert(t, z == "tb")
   400  }
   401  
   402  func TestOverride(t *testing.T) {
   403  	ctx := context.Background()
   404  	ctx = metainfo.WithValue(ctx, "base", "base")
   405  	ctx = metainfo.WithValue(ctx, "base2", "base")
   406  	ctx = metainfo.WithValue(ctx, "base3", "base")
   407  
   408  	ctx1 := metainfo.WithValue(ctx, "a", "a")
   409  	ctx2 := metainfo.WithValue(ctx, "b", "b")
   410  
   411  	av, ae := metainfo.GetValue(ctx1, "a")
   412  	bv, be := metainfo.GetValue(ctx2, "b")
   413  	assert(t, ae && av == "a", ae, av)
   414  	assert(t, be && bv == "b", be, bv)
   415  }
   416  
   417  ///////////////////////////////////////////////
   418  
   419  func initMetaInfo(count int) (context.Context, []string, []string) {
   420  	ctx := context.Background()
   421  	var keys, vals []string
   422  	for i := 0; i < count; i++ {
   423  		k, v := fmt.Sprintf("key-%d", i), fmt.Sprintf("val-%d", i)
   424  		ctx = metainfo.WithValue(ctx, k, v)
   425  		ctx = metainfo.WithPersistentValue(ctx, k, v)
   426  		keys = append(keys, k)
   427  		vals = append(vals, v)
   428  	}
   429  	return ctx, keys, vals
   430  }
   431  
   432  func benchmark(b *testing.B, api string, count int) {
   433  	ctx, keys, vals := initMetaInfo(count)
   434  	switch api {
   435  	case "TransferForward":
   436  		b.ReportAllocs()
   437  		b.ResetTimer()
   438  		for i := 0; i < b.N; i++ {
   439  			_ = metainfo.TransferForward(ctx)
   440  		}
   441  	case "GetValue":
   442  		b.ReportAllocs()
   443  		b.ResetTimer()
   444  		for i := 0; i < b.N; i++ {
   445  			_, _ = metainfo.GetValue(ctx, keys[i%len(keys)])
   446  		}
   447  	case "GetAllValues":
   448  		b.ReportAllocs()
   449  		b.ResetTimer()
   450  		for i := 0; i < b.N; i++ {
   451  			_ = metainfo.GetAllValues(ctx)
   452  		}
   453  	case "RangeValues":
   454  		b.ReportAllocs()
   455  		b.ResetTimer()
   456  		for i := 0; i < b.N; i++ {
   457  			metainfo.RangeValues(ctx, func(_, _ string) bool {
   458  				return true
   459  			})
   460  		}
   461  	case "WithValue":
   462  		b.ReportAllocs()
   463  		b.ResetTimer()
   464  		for i := 0; i < b.N; i++ {
   465  			_ = metainfo.WithValue(ctx, "key", "val")
   466  		}
   467  	case "WithValues":
   468  		b.ReportAllocs()
   469  		b.ResetTimer()
   470  		for i := 0; i < b.N; i++ {
   471  			_ = metainfo.WithValues(ctx, "key--1", "val--1", "key--2", "val--2", "key--3", "val--3")
   472  		}
   473  	case "WithValueAcc":
   474  		b.ReportAllocs()
   475  		b.ResetTimer()
   476  		for i := 0; i < b.N; i++ {
   477  			ctx = metainfo.WithValue(ctx, vals[i%len(vals)], "val")
   478  		}
   479  	case "DelValue":
   480  		b.ReportAllocs()
   481  		b.ResetTimer()
   482  		for i := 0; i < b.N; i++ {
   483  			_ = metainfo.DelValue(ctx, "key")
   484  		}
   485  	case "GetPersistentValue":
   486  		b.ReportAllocs()
   487  		b.ResetTimer()
   488  		for i := 0; i < b.N; i++ {
   489  			_, _ = metainfo.GetPersistentValue(ctx, keys[i%len(keys)])
   490  		}
   491  	case "GetAllPersistentValues":
   492  		b.ReportAllocs()
   493  		b.ResetTimer()
   494  		for i := 0; i < b.N; i++ {
   495  			_ = metainfo.GetAllPersistentValues(ctx)
   496  		}
   497  	case "RangePersistentValues":
   498  		b.ReportAllocs()
   499  		b.ResetTimer()
   500  		for i := 0; i < b.N; i++ {
   501  			metainfo.RangePersistentValues(ctx, func(_, _ string) bool {
   502  				return true
   503  			})
   504  		}
   505  	case "WithPersistentValue":
   506  		b.ReportAllocs()
   507  		b.ResetTimer()
   508  		for i := 0; i < b.N; i++ {
   509  			_ = metainfo.WithPersistentValue(ctx, "key", "val")
   510  		}
   511  	case "WithPersistentValues":
   512  		b.ReportAllocs()
   513  		b.ResetTimer()
   514  		for i := 0; i < b.N; i++ {
   515  			_ = metainfo.WithPersistentValues(ctx, "key--1", "val--1", "key--2", "val--2", "key--3", "val--3")
   516  		}
   517  	case "WithPersistentValueAcc":
   518  		b.ReportAllocs()
   519  		b.ResetTimer()
   520  		for i := 0; i < b.N; i++ {
   521  			ctx = metainfo.WithPersistentValue(ctx, vals[i%len(vals)], "val")
   522  		}
   523  		_ = ctx
   524  	case "DelPersistentValue":
   525  		b.ReportAllocs()
   526  		b.ResetTimer()
   527  		for i := 0; i < b.N; i++ {
   528  			_ = metainfo.DelPersistentValue(ctx, "key")
   529  		}
   530  	case "SaveMetaInfoToMap":
   531  		b.ReportAllocs()
   532  		b.ResetTimer()
   533  		for i := 0; i < b.N; i++ {
   534  			m := make(map[string]string)
   535  			metainfo.SaveMetaInfoToMap(ctx, m)
   536  		}
   537  	case "SetMetaInfoFromMap":
   538  		m := make(map[string]string)
   539  		c := context.Background()
   540  		metainfo.SaveMetaInfoToMap(ctx, m)
   541  		b.ReportAllocs()
   542  		b.ResetTimer()
   543  		for i := 0; i < b.N; i++ {
   544  			_ = metainfo.SetMetaInfoFromMap(c, m)
   545  		}
   546  	}
   547  }
   548  
   549  func benchmarkParallel(b *testing.B, api string, count int) {
   550  	ctx, keys, vals := initMetaInfo(count)
   551  	switch api {
   552  	case "TransferForward":
   553  		b.ReportAllocs()
   554  		b.ResetTimer()
   555  		b.RunParallel(func(pb *testing.PB) {
   556  			for pb.Next() {
   557  				_ = metainfo.TransferForward(ctx)
   558  			}
   559  		})
   560  	case "GetValue":
   561  		b.ReportAllocs()
   562  		b.ResetTimer()
   563  		b.RunParallel(func(pb *testing.PB) {
   564  			var i int
   565  			for pb.Next() {
   566  				_, _ = metainfo.GetValue(ctx, keys[i%len(keys)])
   567  				i++
   568  			}
   569  		})
   570  	case "GetAllValues":
   571  		b.ReportAllocs()
   572  		b.ResetTimer()
   573  		b.RunParallel(func(pb *testing.PB) {
   574  			for pb.Next() {
   575  				_ = metainfo.GetAllValues(ctx)
   576  			}
   577  		})
   578  	case "RangeValues":
   579  		b.ReportAllocs()
   580  		b.ResetTimer()
   581  		b.RunParallel(func(pb *testing.PB) {
   582  			for pb.Next() {
   583  				metainfo.RangeValues(ctx, func(_, _ string) bool {
   584  					return true
   585  				})
   586  			}
   587  		})
   588  	case "WithValue":
   589  		b.ReportAllocs()
   590  		b.ResetTimer()
   591  		b.RunParallel(func(pb *testing.PB) {
   592  			for pb.Next() {
   593  				_ = metainfo.WithValue(ctx, "key", "val")
   594  			}
   595  		})
   596  	case "WithValues":
   597  		b.ReportAllocs()
   598  		b.ResetTimer()
   599  		b.RunParallel(func(pb *testing.PB) {
   600  			for pb.Next() {
   601  				_ = metainfo.WithValues(ctx, "key--1", "val--1", "key--2", "val--2", "key--3", "val--3")
   602  			}
   603  		})
   604  	case "WithValueAcc":
   605  		b.ReportAllocs()
   606  		b.ResetTimer()
   607  		b.RunParallel(func(pb *testing.PB) {
   608  			tmp := ctx
   609  			var i int
   610  			for pb.Next() {
   611  				tmp = metainfo.WithValue(tmp, vals[i%len(vals)], "val")
   612  				i++
   613  			}
   614  		})
   615  	case "DelValue":
   616  		b.ReportAllocs()
   617  		b.ResetTimer()
   618  		b.RunParallel(func(pb *testing.PB) {
   619  			for pb.Next() {
   620  				_ = metainfo.DelValue(ctx, "key")
   621  			}
   622  		})
   623  	case "GetPersistentValue":
   624  		b.ReportAllocs()
   625  		b.ResetTimer()
   626  		b.RunParallel(func(pb *testing.PB) {
   627  			var i int
   628  			for pb.Next() {
   629  				_, _ = metainfo.GetPersistentValue(ctx, keys[i%len(keys)])
   630  				i++
   631  			}
   632  		})
   633  	case "GetAllPersistentValues":
   634  		b.ReportAllocs()
   635  		b.ResetTimer()
   636  		b.RunParallel(func(pb *testing.PB) {
   637  			for pb.Next() {
   638  				_ = metainfo.GetAllPersistentValues(ctx)
   639  			}
   640  		})
   641  	case "RangePersistentValues":
   642  		b.ReportAllocs()
   643  		b.ResetTimer()
   644  		b.RunParallel(func(pb *testing.PB) {
   645  			for pb.Next() {
   646  				metainfo.RangePersistentValues(ctx, func(_, _ string) bool {
   647  					return true
   648  				})
   649  			}
   650  		})
   651  	case "WithPersistentValue":
   652  		b.ReportAllocs()
   653  		b.ResetTimer()
   654  		b.RunParallel(func(pb *testing.PB) {
   655  			for pb.Next() {
   656  				_ = metainfo.WithPersistentValue(ctx, "key", "val")
   657  			}
   658  		})
   659  	case "WithPersistentValues":
   660  		b.ReportAllocs()
   661  		b.ResetTimer()
   662  		b.RunParallel(func(pb *testing.PB) {
   663  			for pb.Next() {
   664  				_ = metainfo.WithPersistentValues(ctx, "key--1", "val--1", "key--2", "val--2", "key--3", "val--3")
   665  			}
   666  		})
   667  	case "WithPersistentValueAcc":
   668  		b.ReportAllocs()
   669  		b.ResetTimer()
   670  		b.RunParallel(func(pb *testing.PB) {
   671  			tmp := ctx
   672  			var i int
   673  			for pb.Next() {
   674  				tmp = metainfo.WithPersistentValue(tmp, vals[i%len(vals)], "val")
   675  				i++
   676  			}
   677  		})
   678  	case "DelPersistentValue":
   679  		b.ReportAllocs()
   680  		b.ResetTimer()
   681  		b.RunParallel(func(pb *testing.PB) {
   682  			for pb.Next() {
   683  				_ = metainfo.DelPersistentValue(ctx, "key")
   684  			}
   685  		})
   686  	case "SaveMetaInfoToMap":
   687  		b.ReportAllocs()
   688  		b.ResetTimer()
   689  		b.RunParallel(func(pb *testing.PB) {
   690  			for pb.Next() {
   691  				m := make(map[string]string)
   692  				metainfo.SaveMetaInfoToMap(ctx, m)
   693  			}
   694  		})
   695  	case "SetMetaInfoFromMap":
   696  		m := make(map[string]string)
   697  		c := context.Background()
   698  		metainfo.SaveMetaInfoToMap(ctx, m)
   699  		b.ReportAllocs()
   700  		b.ResetTimer()
   701  		b.RunParallel(func(pb *testing.PB) {
   702  			for pb.Next() {
   703  				_ = metainfo.SetMetaInfoFromMap(c, m)
   704  			}
   705  		})
   706  	}
   707  }
   708  
   709  func BenchmarkAll(b *testing.B) {
   710  	APIs := []string{
   711  		"TransferForward",
   712  		"GetValue",
   713  		"GetAllValues",
   714  		"WithValue",
   715  		"WithValues",
   716  		"WithValueAcc",
   717  		"DelValue",
   718  		"GetPersistentValue",
   719  		"GetAllPersistentValues",
   720  		"RangePersistentValues",
   721  		"WithPersistentValue",
   722  		"WithPersistentValues",
   723  		"WithPersistentValueAcc",
   724  		"DelPersistentValue",
   725  		"SaveMetaInfoToMap",
   726  		"SetMetaInfoFromMap",
   727  	}
   728  	for _, api := range APIs {
   729  		for _, cnt := range []int{10, 20, 50, 100} {
   730  			fun := fmt.Sprintf("%s_%d", api, cnt)
   731  			b.Run(fun, func(b *testing.B) { benchmark(b, api, cnt) })
   732  		}
   733  	}
   734  }
   735  
   736  func BenchmarkAllParallel(b *testing.B) {
   737  	APIs := []string{
   738  		"TransferForward",
   739  		"GetValue",
   740  		"GetAllValues",
   741  		"WithValue",
   742  		"WithValues",
   743  		"WithValueAcc",
   744  		"DelValue",
   745  		"GetPersistentValue",
   746  		"GetPersistentValues",
   747  		"GetAllPersistentValues",
   748  		"RangePersistentValues",
   749  		"WithPersistentValue",
   750  		"WithPersistentValueAcc",
   751  		"DelPersistentValue",
   752  		"SaveMetaInfoToMap",
   753  		"SetMetaInfoFromMap",
   754  	}
   755  	for _, api := range APIs {
   756  		for _, cnt := range []int{10, 20, 50, 100} {
   757  			fun := fmt.Sprintf("%s_%d", api, cnt)
   758  			b.Run(fun, func(b *testing.B) { benchmarkParallel(b, api, cnt) })
   759  		}
   760  	}
   761  }