aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/performancetimer_test.html
blob: f4727c42f51e42b2b8e230d666baf8e92328d4ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
Author:  attila@google.com (Attila Bodis)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.testing.PerformanceTimer</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.math');
    goog.require('goog.testing.MockClock');
    goog.require('goog.testing.PerformanceTimer');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
  <div id="sandbox"></div>
  <script>
    var mockClock;
    var sandbox = document.getElementById('sandbox');
    var timer;

    // Fast test function, pretty much guaranteed to run in under 5,000ms.
    function fastTestFunction() {
      for (var i = 0; i < 10; i++) {
        var div = document.createElement('div');
        div.innerHTML = 'Div no. ' + i;
        sandbox.appendChild(div);
      }
    }

    function setUp() {
      mockClock = new goog.testing.MockClock(true);
      timer = new goog.testing.PerformanceTimer();
    }

    function tearDown() {
      mockClock.dispose();
      timer = null;
      goog.dom.removeChildren(sandbox);
    }

    function testConstructor() {
      assertTrue('Timer must be an instance of goog.testing.PerformanceTimer',
          timer instanceof goog.testing.PerformanceTimer);
      assertEquals('Timer must collect the default number of samples', 10,
          timer.getNumSamples());
      assertEquals('Timer must have the default timeout interval', 5000,
          timer.getTimeoutInterval());
    }

    function testRun_noSetUpOrTearDown() {
      runAndAssert(false, false);
    }

    function testRun_withSetup() {
      runAndAssert(true, false);
    }

    function testRun_withTearDown() {
      runAndAssert(false, true);
    }

    function testRun_withSetUpAndTearDown() {
      runAndAssert(true, true);
    }

    function runAndAssert(useSetUp, useTearDown) {
      var fakeExecutionTime = [100, 95, 98, 104, 130, 101, 96, 98, 90, 103]
      var count = 0;
      var testFunction = function() {
        mockClock.tick(fakeExecutionTime[count++]);
      };

      var setUpCount = 0;
      var setUpFunction = function() {
        // Should have no effect on total time.
        mockClock.tick(7);
        setUpCount++;
      };

      var tearDownCount = 0;
      var tearDownFunction = function() {
        // Should have no effect on total time.
        mockClock.tick(11);
        tearDownCount++;
      };

      // Fast test function should complete successfully in under 5 seconds...
      var task = new goog.testing.PerformanceTimer.Task(testFunction);
      if (useSetUp) {
        task.withSetUp(setUpFunction);
      }
      if (useTearDown) {
        task.withTearDown(tearDownFunction);
      }
      var results = timer.runTask(task);

      assertNotNull('Results must be available.', results);

      assertEquals('Average is wrong.',
          goog.math.average.apply(null, fakeExecutionTime), results['average']);
      assertEquals('Standard deviation is wrong.',
          goog.math.standardDeviation.apply(null, fakeExecutionTime),
          results['standardDeviation']);

      assertEquals('Count must be as expected.', 10, results['count']);
      assertEquals('Maximum is wrong.', 130, results['maximum']);
      assertEquals('Mimimum is wrong.', 90, results['minimum']);
      assertEquals('Total must be a nonnegative number.',
          goog.math.sum.apply(null, fakeExecutionTime), results['total']);

      assertEquals('Set up count must be as expected.',
          useSetUp ? 10 : 0, setUpCount);
      assertEquals('Tear down count must be as expected.',
          useTearDown ? 10 : 0, tearDownCount);
    }

    function testTimeout() {
      var count = 0;
      var testFunction = function() {
        mockClock.tick(100);
        ++count;
      };

      timer.setNumSamples(200);
      timer.setTimeoutInterval(2500);
      var results = timer.run(testFunction);

      assertNotNull('Results must be available', results);
      assertEquals('Count is wrong', count, results['count']);
      assertTrue('Count must less than expected',
          results['count'] < timer.getNumSamples());
    }

    function testCreateResults() {
      var samples = [53, 0, 103];
      var expectedResults = {
        'average': 52,
        'count': 3,
        'maximum': 103,
        'minimum': 0,
        'standardDeviation': goog.math.standardDeviation.apply(null, samples),
        'total': 156
      };
      assertObjectEquals(
          expectedResults,
          goog.testing.PerformanceTimer.createResults(samples));
    }
  </script>
</body>
</html>