func TestTrimTileFunc(t *testing.T) {
	t1 := tiling.NewTile()
	t1.Scale = 1
	t1.TileIndex = 1
	now := time.Now().Unix()
	// Pretend this is a full tile by setting non-zero CommitTime's for all commits.
	for i, _ := range t1.Commits {
		t1.Commits[i].CommitTime = now
	}
	tr := types.NewPerfTrace()
	tr.Values[len(t1.Commits)-1] = 0.7
	t1.Traces["bar"] = tr

	var err error
	t1, err = trimTile(t1)
	if err != nil {
		t.Errorf("Failed to trim Tile: %s", err)
	}

	if got, want := len(t1.Commits), config.MAX_CLUSTER_COMMITS; got != want {
		t.Errorf("Failed to trim Tile correctly: Got %v Want %v", got, want)
	}
	if got, want := t1.Traces["bar"].Len(), config.MAX_CLUSTER_COMMITS; got != want {
		t.Errorf("Failed to trim Tile Values correctly: Got %v Want %v", got, want)
	}

	// Now test trimming a Tile with less than config.MAX_CLUSTER_COMMITS commits.
	const N = 20
	t1, err = t1.Trim(0, N)
	if err != nil {
		t.Errorf("Failed to trim Tile a second time: %s", err)
	}
	t1, err = trimTile(t1)
	if got, want := len(t1.Commits), N; got != want {
		t.Errorf("Failed to trim Tile correctly: Got %v Want %v", got, want)
	}
	if got, want := t1.Traces["bar"].Len(), N; got != want {
		t.Errorf("Failed to trim Tile Values correctly: Got %v Want %v", got, want)
	}
}
// addBenchDataToTile adds BenchData to a Tile.
//
// See the description at the top of this file for how the mapping works.
func addBenchDataToTile(benchData *BenchData, tile *tiling.Tile, offset int, counter metrics.Counter) {

	// cb is the anonymous closure we'll pass over all the trace values found in benchData.
	cb := func(key string, value float64, params map[string]string) {
		needsUpdate := false
		var trace *types.PerfTrace
		if tr, ok := tile.Traces[key]; !ok {
			trace = types.NewPerfTrace()
			tile.Traces[key] = trace
			needsUpdate = true
		} else {
			trace = tr.(*types.PerfTrace)
			if !util.MapsEqual(params, tile.Traces[key].Params()) {
				needsUpdate = true
			}
		}
		trace.Params_ = params
		trace.Values[offset] = value
		counter.Inc(1)

		if needsUpdate {
			// Update the Tile's ParamSet with any new keys or values we see.
			//
			// TODO(jcgregorio) Maybe defer this until we are about to Put the Tile
			// back to disk and rebuild ParamSet from scratch over all the Traces.
			for k, v := range params {
				if _, ok := tile.ParamSet[k]; !ok {
					tile.ParamSet[k] = []string{v}
				} else if !util.In(v, tile.ParamSet[k]) {
					tile.ParamSet[k] = append(tile.ParamSet[k], v)
				}
			}
		}
	}

	benchData.ForEach(cb)
}