github.com/y-miyazaki/go-common@v0.3.1/example/benchmark/struct_chain_test.go (about) 1 package main 2 3 import "testing" 4 5 type TestStruct struct { 6 Title string 7 Name string 8 Age string 9 TestStruct2 *TestStruct2 10 } 11 type TestStruct2 struct { 12 Title2 string 13 Name2 string 14 Age2 string 15 TestStruct3 *TestStruct3 16 } 17 type TestStruct3 struct { 18 Title3 string 19 Name3 string 20 Age3 string 21 } 22 23 func CreateTestData() []TestStruct { 24 data := []TestStruct{} 25 for i := 0; i < 100000; i++ { 26 data = append(data, TestStruct{ 27 Title: "testtitle", 28 Name: "testname", 29 Age: "1", 30 TestStruct2: &TestStruct2{ 31 Title2: "testtitle2", 32 Name2: "testname2", 33 Age2: "2", 34 TestStruct3: &TestStruct3{ 35 Title3: "testtitle3", 36 Name3: "testname3", 37 Age3: "3", 38 }, 39 }, 40 }) 41 } 42 return data 43 } 44 45 func BenchmarkStruct1(b *testing.B) { 46 data := CreateTestData() 47 var base []string = []string{} 48 49 for _, v := range data { 50 base = append(base, v.TestStruct2.TestStruct3.Title3) 51 base = append(base, v.TestStruct2.TestStruct3.Name3) 52 base = append(base, v.TestStruct2.TestStruct3.Age3) 53 base = append(base, v.TestStruct2.TestStruct3.Title3) 54 base = append(base, v.TestStruct2.TestStruct3.Name3) 55 base = append(base, v.TestStruct2.TestStruct3.Age3) 56 } 57 } 58 59 func BenchmarkStruct2(b *testing.B) { 60 data := CreateTestData() 61 var base []string = []string{} 62 63 for _, v := range data { 64 t3 := v.TestStruct2.TestStruct3 65 base = append(base, t3.Title3) 66 base = append(base, t3.Name3) 67 base = append(base, t3.Age3) 68 base = append(base, t3.Title3) 69 base = append(base, t3.Name3) 70 base = append(base, t3.Age3) 71 } 72 } 73 74 func BenchmarkMake(b *testing.B) { 75 data := []string{} 76 77 for i := 0; i < b.N; i++ { 78 line := make([]string, 0, 20) 79 line = []string{ 80 "1", 81 "1", 82 "1", 83 "1", 84 "1", 85 "1", 86 "1", 87 "1", 88 "1", 89 "1", 90 "1", 91 "1", 92 "1", 93 "1", 94 "1", 95 "1", 96 "1", 97 "1", 98 "1", 99 "1", 100 } 101 data = append(data, line...) 102 } 103 } 104 105 func BenchmarkMake2(b *testing.B) { 106 data := []string{} 107 108 for i := 0; i < b.N; i++ { 109 line := make([]string, 0, 10) 110 line = []string{ 111 "1", 112 "1", 113 "1", 114 "1", 115 "1", 116 "1", 117 "1", 118 "1", 119 "1", 120 "1", 121 "1", 122 "1", 123 "1", 124 "1", 125 "1", 126 "1", 127 "1", 128 "1", 129 "1", 130 "1", 131 } 132 data = append(data, line...) 133 } 134 } 135 func BenchmarkMakeNo(b *testing.B) { 136 data := []string{} 137 138 for i := 0; i < b.N; i++ { 139 line := []string{ 140 "1", 141 "1", 142 "1", 143 "1", 144 "1", 145 "1", 146 "1", 147 "1", 148 "1", 149 "1", 150 "1", 151 "1", 152 "1", 153 "1", 154 "1", 155 "1", 156 "1", 157 "1", 158 "1", 159 "1", 160 } 161 data = append(data, line...) 162 } 163 }