| // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| package main |
| |
| import ( |
| "fmt" |
| "sort" |
| |
| "fuchsia.googlesource.com/infra/infra/fuchsia/testexec" |
| ) |
| |
| // makeShards is the core algorithm to this tool. It takes a set of test specs and produces |
| // a set of shards which may then be converted into Swarming tasks. |
| // |
| // This is the most naive algorithm at the moment. It just merges all tests together which |
| // have the same environment setting into the same shard. |
| func makeShards(specs []testexec.TestSpec, prefix string) []*testexec.Shard { |
| // Collect the order of the shards so our shard ordering is deterministic with |
| // respect to the input. |
| envToSuites := make(map[testexec.Environment][]testexec.Test) |
| envs := []testexec.Environment{} |
| for _, spec := range specs { |
| for _, env := range spec.Envs { |
| if _, ok := envToSuites[env]; !ok { |
| envs = append(envs, env) |
| } |
| envToSuites[env] = append(envToSuites[env], spec.Test) |
| } |
| } |
| |
| shards := make([]*testexec.Shard, 0, len(envs)) |
| for i, env := range envs { |
| tests := envToSuites[env] |
| sort.Slice(tests, func(i, j int) bool { |
| return tests[i].Location < tests[j].Location |
| }) |
| name := fmt.Sprintf("%04d", i) |
| if prefix != "" { |
| name = fmt.Sprintf("%s-%s", prefix, name) |
| } |
| shards = append(shards, &testexec.Shard{ |
| Name: name, |
| Tests: tests, |
| Env: env, |
| }) |
| } |
| |
| return shards |
| } |