| // 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 catapult_test |
| |
| import ( |
| "encoding/json" |
| "reflect" |
| "testing" |
| |
| . "fuchsia.googlesource.com/testing/catapult" |
| schema "fuchsia.googlesource.com/testing/perf/schema/v1" |
| "github.com/stretchr/testify/assert" |
| "github.com/stretchr/testify/suite" |
| ) |
| |
| type HistogramTestSuite struct { |
| suite.Suite |
| } |
| |
| func TestHistogram(t *testing.T) { |
| suite.Run(t, new(HistogramTestSuite)) |
| } |
| |
| // TODO(kjharland): After writing this test the team decided we shouldn't use |
| // assertion libraries. Remove `assert.*` references in a later change. |
| func (s *HistogramTestSuite) TestConvertVariantsToHistograms() { |
| var perfTestOutput = []byte(`{ |
| "variants": [{ |
| "finish_time": 1234567, |
| "variant_desc": "example_variant", |
| "boot_args": "example_boot_arg1", |
| "fbenchmarks_data": [{ |
| "label": "example_benchmark_data", |
| "unit": "ns", |
| "samples": [{ |
| "label": "example_sample", |
| "values": [10, 20, 30, 40, 50] |
| }] |
| }] |
| }] |
| } |
| `) |
| |
| var variants struct { |
| Variants []schema.Variant |
| } |
| |
| json.Unmarshal(perfTestOutput, &variants) |
| histograms := ConvertVariantsToHistograms(variants.Variants) |
| |
| assert.Equal(s.T(), len(histograms), 1) |
| |
| histogram := histograms[0] |
| assert.Equal(s.T(), histogram.Name, "example_variant, example_benchmark_data") |
| assert.Len(s.T(), histogram.GUID, 36) |
| assert.Equal(s.T(), histogram.Unit, "ms_smallerIsBetter") |
| assert.Empty(s.T(), histogram.Description) |
| assert.Empty(s.T(), histogram.Diagnostics) |
| assert.ElementsMatch(s.T(), histogram.SampleValues, []float64{ |
| 1e-5, |
| 2e-5, |
| 3e-5, |
| 4e-5, |
| 5e-5, |
| }) |
| assert.Equal(s.T(), histogram.MaxNumSampleValues, 5) |
| assert.Equal(s.T(), histogram.NumNans, 0) |
| |
| assert.ElementsMatch(s.T(), histogram.Running, []float64{ |
| 5, // count |
| 5e-05, // max |
| -4.5841637507904744, // meanlogs |
| 3.0000000000000004e-05, //mean |
| 1e-5, // min |
| 1.5000000000000001e-4, //sum |
| 2.5e-10, // variance |
| }) |
| } |
| |
| func (s *HistogramTestSuite) TestHistogram_AddDiagnostic() { |
| var testName, testGUID string |
| var histogram Histogram |
| |
| setUp := func() { |
| testName = "test-name" |
| testGUID = "test-guid" |
| histogram = Histogram{} |
| histogram.AddDiagnostic(testName, testGUID) |
| } |
| |
| s.T().Run("should do nothing if adding an identical name-guid pair", func(t *testing.T) { |
| setUp() |
| expectedDiagnostics := map[string]string{testName: testGUID} |
| |
| histogram.AddDiagnostic(testName, testGUID) |
| |
| if !reflect.DeepEqual(expectedDiagnostics, histogram.Diagnostics) { |
| s.T().Errorf("invalid diagnostics map. Expected %v. Got %v", |
| expectedDiagnostics, histogram.Diagnostics) |
| } |
| }) |
| |
| s.T().Run("should add a new name-guid pair when given a unique name.", func(t *testing.T) { |
| setUp() |
| expectedDiagnostics := map[string]string{ |
| testName: testGUID, |
| "different-name": testGUID, |
| } |
| |
| histogram.AddDiagnostic("different-name", testGUID) |
| |
| if !reflect.DeepEqual(expectedDiagnostics, histogram.Diagnostics) { |
| s.T().Errorf("invalid diagnostics map. Expected %v. Got %v", |
| expectedDiagnostics, histogram.Diagnostics) |
| } |
| }) |
| |
| s.T().Run("should overwrite an existing name-guid pair", func(t *testing.T) { |
| setUp() |
| expectedDiagnostics := map[string]string{ |
| testName: "different-guid", |
| } |
| |
| histogram.AddDiagnostic(testName, "different-guid") |
| if !reflect.DeepEqual(expectedDiagnostics, histogram.Diagnostics) { |
| s.T().Errorf("Diagnostics map was modified. Expected %v. Got %v", |
| expectedDiagnostics, histogram.Diagnostics) |
| } |
| }) |
| } |