222
223func (suite *balanceSchedulerPlanAnalyzeTestSuite) TestAnalyzerResult5() {
224 plans := make([]Plan, 0)
225 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 0, Status: NewStatus(StatusStoreRemoveLimitThrottled)})226 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[3], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
227 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[2], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
228 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[1], Target: suite.stores[0], Step: 2, Status: NewStatus(StatusStoreScoreDisallowed)})
194
195func (suite *balanceSchedulerPlanAnalyzeTestSuite) TestAnalyzerResult4() {
196 plans := make([]Plan, 0)
197 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 0, Status: NewStatus(StatusStoreDown)})198 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[3], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
199 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[2], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
200 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[1], Target: suite.stores[0], Step: 2, Status: NewStatus(StatusStoreScoreDisallowed)})
175
176func (suite *balanceSchedulerPlanAnalyzeTestSuite) TestAnalyzerResult3() {
177 plans := make([]Plan, 0)
178 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 0, Status: NewStatus(StatusStoreDown)})179 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[3], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
180 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[2], Region: suite.regions[0], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
181 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[1], Region: suite.regions[1], Step: 1, Status: NewStatus(StatusRegionNotMatchRule)})
155
156func (suite *balanceSchedulerPlanAnalyzeTestSuite) TestAnalyzerResult2() {
157 plans := make([]Plan, 0)
158 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 0, Status: NewStatus(StatusStoreDown)})159 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[3], Step: 0, Status: NewStatus(StatusStoreDown)})
160 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[2], Step: 0, Status: NewStatus(StatusStoreDown)})
161 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[1], Step: 0, Status: NewStatus(StatusStoreDown)})
115
116func (suite *balanceSchedulerPlanAnalyzeTestSuite) TestAnalyzerResult1() {
117 plans := make([]Plan, 0)
118 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 2, Target: suite.stores[0], Status: NewStatus(StatusStoreScoreDisallowed)})119 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 2, Target: suite.stores[1], Status: NewStatus(StatusStoreScoreDisallowed)})
120 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 2, Target: suite.stores[2], Status: NewStatus(StatusStoreScoreDisallowed)})
121 plans = append(plans, &BalanceSchedulerPlan{Source: suite.stores[4], Step: 2, Target: suite.stores[3], Status: NewStatus(StatusStoreNotMatchRule)})
Multiple calls for append
could be combined into a single call for append
.
The append
built-in function appends elements to the end of a slice. If it has
sufficient capacity, the destination is resliced to accommodate the new
elements, but if capacity is not enough, then append
will allocate a new
underlying array and return the updated slice. Therefore, it is necessary to
store the result of append, often in the variable holding the slice itself.
Appending calls in a single call of append
allocates memory just once to
accommodate all the elements to be appended. Whereas multiple calls to append
introduce many overheads, most notably being the possibility of more calls for
memory allocation because the total number of elements to be appended over
multiple calls of append
is unknown beforehand, resulting in inaccurate
preallocation.
Read more about a
similar implementation for append
.
Hence, a single call to append
is recommended.
Bad practice
xs = append(xs, 1)
xs = append(xs, 2)
xs = append(xs, 3)
xs = append(xs, 4)
xs = append(xs, 5)
Recommended
xs = append(xs, 1, 2, 3, 4, 5)