github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/bench_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/utils"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/state"
    14  )
    15  
    16  type BenchmarkSuite struct {
    17  }
    18  
    19  var _ = gc.Suite(&BenchmarkSuite{})
    20  
    21  func (*BenchmarkSuite) BenchmarkAddUnit(c *gc.C) {
    22  	// TODO(rog) embed ConnSuite in BenchmarkSuite when
    23  	// gocheck calls appropriate fixture methods for benchmark
    24  	// functions.
    25  	var s ConnSuite
    26  	s.SetUpSuite(c)
    27  	defer s.TearDownSuite(c)
    28  	s.SetUpTest(c)
    29  	defer s.TearDownTest(c)
    30  	charm := s.AddTestingCharm(c, "wordpress")
    31  	svc := s.AddTestingService(c, "wordpress", charm)
    32  	c.ResetTimer()
    33  	for i := 0; i < c.N; i++ {
    34  		_, err := svc.AddUnit()
    35  		c.Assert(err, jc.ErrorIsNil)
    36  	}
    37  }
    38  
    39  func (*BenchmarkSuite) BenchmarkAddAndAssignUnit(c *gc.C) {
    40  	var s ConnSuite
    41  	s.SetUpSuite(c)
    42  	defer s.TearDownSuite(c)
    43  	s.SetUpTest(c)
    44  	defer s.TearDownTest(c)
    45  	charm := s.AddTestingCharm(c, "wordpress")
    46  	svc := s.AddTestingService(c, "wordpress", charm)
    47  	c.ResetTimer()
    48  	for i := 0; i < c.N; i++ {
    49  		unit, err := svc.AddUnit()
    50  		c.Assert(err, jc.ErrorIsNil)
    51  		err = s.State.AssignUnit(unit, state.AssignClean)
    52  		c.Assert(err, jc.ErrorIsNil)
    53  	}
    54  }
    55  
    56  func (*BenchmarkSuite) BenchmarkAddMetrics1_1(c *gc.C)     { benchmarkAddMetrics(1, 1, c) }
    57  func (*BenchmarkSuite) BenchmarkAddMetrics1_10(c *gc.C)    { benchmarkAddMetrics(1, 10, c) }
    58  func (*BenchmarkSuite) BenchmarkAddMetrics1_100(c *gc.C)   { benchmarkAddMetrics(1, 100, c) }
    59  func (*BenchmarkSuite) BenchmarkAddMetrics100_1(c *gc.C)   { benchmarkAddMetrics(100, 1, c) }
    60  func (*BenchmarkSuite) BenchmarkAddMetrics100_10(c *gc.C)  { benchmarkAddMetrics(100, 10, c) }
    61  func (*BenchmarkSuite) BenchmarkAddMetrics100_100(c *gc.C) { benchmarkAddMetrics(100, 100, c) }
    62  func (*BenchmarkSuite) BenchmarkAddMetrics10_1(c *gc.C)    { benchmarkAddMetrics(10, 1, c) }
    63  func (*BenchmarkSuite) BenchmarkAddMetrics10_10(c *gc.C)   { benchmarkAddMetrics(10, 10, c) }
    64  func (*BenchmarkSuite) BenchmarkAddMetrics10_100(c *gc.C)  { benchmarkAddMetrics(10, 100, c) }
    65  
    66  func benchmarkAddMetrics(metricsPerBatch, batches int, c *gc.C) {
    67  	var s ConnSuite
    68  	s.SetUpSuite(c)
    69  	defer s.TearDownSuite(c)
    70  	s.SetUpTest(c)
    71  	defer s.TearDownTest(c)
    72  	now := time.Now()
    73  	metrics := make([]state.Metric, metricsPerBatch)
    74  	for i := range metrics {
    75  		metrics[i] = state.Metric{
    76  			Key:   "metricKey",
    77  			Value: "keyValue",
    78  			Time:  now,
    79  		}
    80  	}
    81  	charm := s.AddTestingCharm(c, "wordpress")
    82  	svc := s.AddTestingService(c, "wordpress", charm)
    83  	unit, err := svc.AddUnit()
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	serviceCharmURL, _ := svc.CharmURL()
    86  	err = unit.SetCharmURL(serviceCharmURL)
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	c.ResetTimer()
    89  	for i := 0; i < c.N; i++ {
    90  		for n := 0; n < batches; n++ {
    91  			_, err := s.State.AddMetrics(
    92  				state.BatchParam{
    93  					UUID:     utils.MustNewUUID().String(),
    94  					Created:  now,
    95  					CharmURL: serviceCharmURL.String(),
    96  					Metrics:  metrics,
    97  					Unit:     unit.UnitTag(),
    98  				},
    99  			)
   100  			c.Assert(err, jc.ErrorIsNil)
   101  		}
   102  	}
   103  }
   104  
   105  // BenchmarkCleanupMetrics needs to add metrics each time over the cycle.
   106  // Because of this the benchmark includes addmetric time
   107  func (*BenchmarkSuite) BenchmarkCleanupMetrics(c *gc.C) {
   108  	numberOfMetrics := 50
   109  	var s ConnSuite
   110  	s.SetUpSuite(c)
   111  	defer s.TearDownSuite(c)
   112  	s.SetUpTest(c)
   113  	defer s.TearDownTest(c)
   114  	oldTime := time.Now().Add(-(state.CleanupAge))
   115  	charm := s.AddTestingCharm(c, "wordpress")
   116  	svc := s.AddTestingService(c, "wordpress", charm)
   117  	unit, err := svc.AddUnit()
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	serviceCharmURL, _ := svc.CharmURL()
   120  	err = unit.SetCharmURL(serviceCharmURL)
   121  	c.Assert(err, jc.ErrorIsNil)
   122  	c.ResetTimer()
   123  	for i := 0; i < c.N; i++ {
   124  		for i := 0; i < numberOfMetrics; i++ {
   125  			m, err := s.State.AddMetrics(
   126  				state.BatchParam{
   127  					UUID:     utils.MustNewUUID().String(),
   128  					Created:  oldTime,
   129  					CharmURL: serviceCharmURL.String(),
   130  					Metrics:  []state.Metric{{}},
   131  					Unit:     unit.UnitTag(),
   132  				},
   133  			)
   134  			c.Assert(err, jc.ErrorIsNil)
   135  			err = m.SetSent(time.Now())
   136  			c.Assert(err, jc.ErrorIsNil)
   137  		}
   138  		err := s.State.CleanupOldMetrics()
   139  		c.Assert(err, jc.ErrorIsNil)
   140  	}
   141  }