github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/marshal_bench_test.go (about)

     1  package phpserialize_test
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/trim21/go-phpserialize"
    10  )
    11  
    12  func BenchmarkMarshal_type(b *testing.B) {
    13  	for _, data := range testCase {
    14  		data := data
    15  		b.Run(data.Name, func(b *testing.B) {
    16  			b.RunParallel(func(pb *testing.PB) {
    17  				for pb.Next() {
    18  					_, err := phpserialize.Marshal(data.Data)
    19  					if err != nil {
    20  						b.FailNow()
    21  					}
    22  				}
    23  			})
    24  		})
    25  	}
    26  }
    27  
    28  func BenchmarkMarshal_field_as_string(b *testing.B) {
    29  	data := struct {
    30  		F int `php:",string"`
    31  	}{}
    32  	b.RunParallel(func(pb *testing.PB) {
    33  		for pb.Next() {
    34  			_, err := phpserialize.Marshal(data)
    35  			if err != nil {
    36  				b.FailNow()
    37  			}
    38  		}
    39  	})
    40  }
    41  
    42  func BenchmarkMarshal_ifce(b *testing.B) {
    43  	for _, data := range testCase {
    44  		data := data
    45  		b.Run(data.Name, func(b *testing.B) {
    46  			b.RunParallel(func(pb *testing.PB) {
    47  				for pb.Next() {
    48  					_, err := phpserialize.Marshal(data)
    49  					if err != nil {
    50  						b.FailNow()
    51  					}
    52  				}
    53  			})
    54  		})
    55  	}
    56  }
    57  
    58  func BenchmarkMarshal_map_type(b *testing.B) {
    59  	for i := 1; i <= 1000; i = i * 10 {
    60  		i := i
    61  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
    62  			var m = make(map[int]uint, i)
    63  			for j := 0; j < i; j++ {
    64  				m[j+1] = uint(j + 2)
    65  			}
    66  			b.RunParallel(func(pb *testing.PB) {
    67  				for pb.Next() {
    68  					_, err := phpserialize.Marshal(m)
    69  					if err != nil {
    70  						b.FailNow()
    71  					}
    72  				}
    73  			})
    74  		})
    75  	}
    76  }
    77  
    78  func BenchmarkMarshal_map_as_ifce(b *testing.B) {
    79  	for i := 1; i <= 1000; i = i * 10 {
    80  		i := i
    81  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
    82  			var m = make(map[int]uint, i)
    83  			for j := 0; j < i; j++ {
    84  				m[j+1] = uint(j + 2)
    85  			}
    86  			b.RunParallel(func(pb *testing.PB) {
    87  				var v = struct{ Value any }{m}
    88  				for pb.Next() {
    89  					_, err := phpserialize.Marshal(v)
    90  					if err != nil {
    91  						b.FailNow()
    92  					}
    93  				}
    94  			})
    95  		})
    96  	}
    97  }
    98  
    99  func BenchmarkMarshal_map_with_ifce_value(b *testing.B) {
   100  	for i := 1; i <= 1000; i = i * 10 {
   101  		i := i
   102  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
   103  			var m = make(map[int]any, i)
   104  			for j := 0; j < i; j++ {
   105  				m[j+1] = uint(j + 2)
   106  			}
   107  			b.RunParallel(func(pb *testing.PB) {
   108  				for pb.Next() {
   109  					_, err := phpserialize.Marshal(m)
   110  					if err != nil {
   111  						b.FailNow()
   112  					}
   113  				}
   114  			})
   115  		})
   116  	}
   117  }
   118  
   119  func BenchmarkMarshal_slice_of_value(b *testing.B) {
   120  	type D struct {
   121  		Value []uint
   122  	}
   123  	for i := 1; i <= 1000; i = i * 10 {
   124  		i := i
   125  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
   126  			var m = make([]uint, i)
   127  			for j := 0; j < i; j++ {
   128  				m[j] = uint(j + 2)
   129  			}
   130  			b.RunParallel(func(pb *testing.PB) {
   131  				for pb.Next() {
   132  					_, err := phpserialize.Marshal(D{m})
   133  					if err != nil {
   134  						b.FailNow()
   135  					}
   136  				}
   137  			})
   138  		})
   139  	}
   140  }
   141  
   142  func BenchmarkMarshal_ifce_slice_as_value(b *testing.B) {
   143  	type D struct {
   144  		Value any
   145  	}
   146  	for i := 1; i <= 1000; i = i * 10 {
   147  		i := i
   148  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
   149  			var m = make([]uint, i)
   150  			for j := 0; j < i; j++ {
   151  				m[j] = uint(j + 2)
   152  			}
   153  			b.RunParallel(func(pb *testing.PB) {
   154  				for pb.Next() {
   155  					_, err := phpserialize.Marshal(D{m})
   156  					if err != nil {
   157  						b.FailNow()
   158  					}
   159  				}
   160  			})
   161  			runtime.KeepAlive(m)
   162  		})
   163  	}
   164  }
   165  
   166  func BenchmarkMarshal_ifce_slice_of_type(b *testing.B) {
   167  	type D struct {
   168  		Value any
   169  	}
   170  	for i := 1; i <= 1000; i = i * 10 {
   171  		i := i
   172  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
   173  			var m = make([]User, i)
   174  			for j := 0; j < i; j++ {
   175  				m[j] = User{ID: uint64(j + 2), Name: "u-" + strconv.Itoa(j+2)}
   176  			}
   177  
   178  			phpserialize.Marshal(D{Value: m}) // warm up
   179  
   180  			b.RunParallel(func(pb *testing.PB) {
   181  				for pb.Next() {
   182  					_, err := phpserialize.Marshal(D{Value: m})
   183  					if err != nil {
   184  						b.FailNow()
   185  					}
   186  				}
   187  			})
   188  		})
   189  	}
   190  }
   191  func BenchmarkMarshal_1_type_slice_of_type(b *testing.B) {
   192  	type D struct {
   193  		Value []User
   194  	}
   195  	for i := 1; i <= 1000; i = i * 10 {
   196  		i := i
   197  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
   198  			var m = make([]User, i)
   199  			for j := 0; j < i; j++ {
   200  				m[j] = User{ID: uint64(j + 2), Name: "u-" + strconv.Itoa(j+2)}
   201  			}
   202  			b.RunParallel(func(pb *testing.PB) {
   203  				for pb.Next() {
   204  					_, err := phpserialize.Marshal(D{Value: m})
   205  					if err != nil {
   206  						b.FailNow()
   207  					}
   208  				}
   209  			})
   210  		})
   211  	}
   212  }
   213  
   214  func BenchmarkMarshal_1_ifce_slice_of_ifce(b *testing.B) {
   215  	type D struct {
   216  		Value any
   217  	}
   218  	for i := 1; i <= 1000; i = i * 10 {
   219  		i := i
   220  		b.Run(fmt.Sprintf("len-%d", i), func(b *testing.B) {
   221  			var m = make([]any, i)
   222  			for j := 0; j < i; j++ {
   223  				m[j] = User{ID: uint64(j + 2), Name: "u-" + strconv.Itoa(j+2)}
   224  			}
   225  
   226  			phpserialize.Marshal(D{Value: m}) // warm up
   227  
   228  			b.RunParallel(func(pb *testing.PB) {
   229  				for pb.Next() {
   230  					_, err := phpserialize.Marshal(D{Value: m})
   231  					if err != nil {
   232  						b.FailNow()
   233  					}
   234  				}
   235  			})
   236  		})
   237  	}
   238  }
   239  
   240  func BenchmarkMarshal_large_struct_10(b *testing.B) {
   241  	var v struct {
   242  		Field0 bool
   243  		Field1 bool
   244  		Field2 bool
   245  		Field3 bool
   246  		Field4 bool
   247  		Field5 bool
   248  		Field6 bool
   249  		Field7 bool
   250  		Field8 bool
   251  		Field9 bool
   252  	}
   253  
   254  	b.RunParallel(func(pb *testing.PB) {
   255  		for pb.Next() {
   256  			_, err := phpserialize.Marshal(v)
   257  			if err != nil {
   258  				b.FailNow()
   259  			}
   260  		}
   261  	})
   262  }
   263  
   264  func BenchmarkMarshal_large_struct_50(b *testing.B) {
   265  	var v struct {
   266  		Field0  bool
   267  		Field1  bool
   268  		Field2  bool
   269  		Field3  bool
   270  		Field4  bool
   271  		Field5  bool
   272  		Field6  bool
   273  		Field7  bool
   274  		Field8  bool
   275  		Field9  bool
   276  		Field10 bool
   277  		Field11 bool
   278  		Field12 bool
   279  		Field13 bool
   280  		Field14 bool
   281  		Field15 bool
   282  		Field16 bool
   283  		Field17 bool
   284  		Field18 bool
   285  		Field19 bool
   286  		Field20 bool
   287  		Field21 bool
   288  		Field22 bool
   289  		Field23 bool
   290  		Field24 bool
   291  		Field25 bool
   292  		Field26 bool
   293  		Field27 bool
   294  		Field28 bool
   295  		Field29 bool
   296  		Field30 bool
   297  		Field31 bool
   298  		Field32 bool
   299  		Field33 bool
   300  		Field34 bool
   301  		Field35 bool
   302  		Field36 bool
   303  		Field37 bool
   304  		Field38 bool
   305  		Field39 bool
   306  		Field40 bool
   307  		Field41 bool
   308  		Field42 bool
   309  		Field43 bool
   310  		Field44 bool
   311  		Field45 bool
   312  		Field46 bool
   313  		Field47 bool
   314  		Field48 bool
   315  		Field49 bool
   316  	}
   317  	for i := 0; i < b.N; i++ {
   318  		_, err := phpserialize.Marshal(v)
   319  		if err != nil {
   320  			b.FailNow()
   321  		}
   322  	}
   323  }
   324  
   325  func BenchmarkMarshal_large_struct_100(b *testing.B) {
   326  	var v struct {
   327  		Field0  bool
   328  		Field1  bool
   329  		Field2  bool
   330  		Field3  bool
   331  		Field4  bool
   332  		Field5  bool
   333  		Field6  bool
   334  		Field7  bool
   335  		Field8  bool
   336  		Field9  bool
   337  		Field10 bool
   338  		Field11 bool
   339  		Field12 bool
   340  		Field13 bool
   341  		Field14 bool
   342  		Field15 bool
   343  		Field16 bool
   344  		Field17 bool
   345  		Field18 bool
   346  		Field19 bool
   347  		Field20 bool
   348  		Field21 bool
   349  		Field22 bool
   350  		Field23 bool
   351  		Field24 bool
   352  		Field25 bool
   353  		Field26 bool
   354  		Field27 bool
   355  		Field28 bool
   356  		Field29 bool
   357  		Field30 bool
   358  		Field31 bool
   359  		Field32 bool
   360  		Field33 bool
   361  		Field34 bool
   362  		Field35 bool
   363  		Field36 bool
   364  		Field37 bool
   365  		Field38 bool
   366  		Field39 bool
   367  		Field40 bool
   368  		Field41 bool
   369  		Field42 bool
   370  		Field43 bool
   371  		Field44 bool
   372  		Field45 bool
   373  		Field46 bool
   374  		Field47 bool
   375  		Field48 bool
   376  		Field49 bool
   377  		Field50 bool
   378  		Field51 bool
   379  		Field52 bool
   380  		Field53 bool
   381  		Field54 bool
   382  		Field55 bool
   383  		Field56 bool
   384  		Field57 bool
   385  		Field58 bool
   386  		Field59 bool
   387  		Field60 bool
   388  		Field61 bool
   389  		Field62 bool
   390  		Field63 bool
   391  		Field64 bool
   392  		Field65 bool
   393  		Field66 bool
   394  		Field67 bool
   395  		Field68 bool
   396  		Field69 bool
   397  		Field70 bool
   398  		Field71 bool
   399  		Field72 bool
   400  		Field73 bool
   401  		Field74 bool
   402  		Field75 bool
   403  		Field76 bool
   404  		Field77 bool
   405  		Field78 bool
   406  		Field79 bool
   407  		Field80 bool
   408  		Field81 bool
   409  		Field82 bool
   410  		Field83 bool
   411  		Field84 bool
   412  		Field85 bool
   413  		Field86 bool
   414  		Field87 bool
   415  		Field88 bool
   416  		Field89 bool
   417  		Field90 bool
   418  		Field91 bool
   419  		Field92 bool
   420  		Field93 bool
   421  		Field94 bool
   422  		Field95 bool
   423  		Field96 bool
   424  		Field97 bool
   425  		Field98 bool
   426  		Field99 bool
   427  	}
   428  
   429  	for i := 0; i < b.N; i++ {
   430  		_, err := phpserialize.Marshal(v)
   431  		if err != nil {
   432  			b.FailNow()
   433  		}
   434  	}
   435  }
   436  
   437  func BenchmarkMarshal_int(b *testing.B) {
   438  	for i := 10; i <= 1000; i *= 10 {
   439  		i := i
   440  		b.Run(fmt.Sprintf("marshal int %d", i), func(b *testing.B) {
   441  			var s = make([]int, i)
   442  			for j := 0; j < i; j++ {
   443  				s[j] = j
   444  			}
   445  			b.RunParallel(func(pb *testing.PB) {
   446  				for pb.Next() {
   447  					if _, err := phpserialize.Marshal(s); err != nil {
   448  						b.Fatal(err)
   449  					}
   450  				}
   451  			})
   452  		})
   453  	}
   454  }
   455  
   456  func BenchmarkMarshal_uint(b *testing.B) {
   457  	for i := 10; i <= 1000; i *= 10 {
   458  		var s = make([]uint, i)
   459  		for j := 0; j < i; j++ {
   460  			s[j] = uint(j)
   461  		}
   462  		b.Run(fmt.Sprintf("marshal uint %d", i), func(b *testing.B) {
   463  			b.RunParallel(func(pb *testing.PB) {
   464  				for pb.Next() {
   465  					if _, err := phpserialize.Marshal(s); err != nil {
   466  						b.Fatal(err.Error())
   467  					}
   468  				}
   469  			})
   470  		})
   471  	}
   472  }
   473  
   474  func BenchmarkMarshal_many_map_field(b *testing.B) {
   475  	var s = struct {
   476  		Map1 map[string]int `php:"map_1"`
   477  		Map2 map[int]string `php:"map_2"`
   478  		Map3 map[uint]int   `php:"map_3"`
   479  	}{
   480  		Map1: map[string]int{
   481  			"one":   1,
   482  			"two":   2,
   483  			"three": 3,
   484  		},
   485  		Map2: map[int]string{
   486  			1: "1111",
   487  			2: "2222",
   488  			3: "3333",
   489  			4: "4444",
   490  			5: "5555",
   491  			6: "6666",
   492  		},
   493  		Map3: map[uint]int{
   494  			1: 1111,
   495  			2: 2222,
   496  			3: 3333,
   497  			4: 4444,
   498  			5: 5555,
   499  			6: 6666,
   500  		},
   501  	}
   502  
   503  	b.RunParallel(func(pb *testing.PB) {
   504  		for pb.Next() {
   505  			_, err := phpserialize.Marshal(s)
   506  			if err != nil {
   507  				b.FailNow()
   508  			}
   509  		}
   510  	})
   511  }