1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.grinder.console.swingui;
24
25 import junit.framework.TestCase;
26
27 import java.awt.BorderLayout;
28 import java.awt.Color;
29 import java.text.DecimalFormat;
30 import java.util.Random;
31 import javax.swing.JComponent;
32 import javax.swing.JFrame;
33
34 import net.grinder.console.common.ResourcesImplementation;
35 import net.grinder.statistics.PeakStatisticExpression;
36 import net.grinder.statistics.StatisticExpression;
37 import net.grinder.statistics.StatisticExpressionFactory;
38 import net.grinder.statistics.StatisticsIndexMap;
39 import net.grinder.statistics.StatisticsServices;
40 import net.grinder.statistics.StatisticsServicesImplementation;
41 import net.grinder.statistics.StatisticsSet;
42 import net.grinder.statistics.StatisticsSetFactory;
43
44
45
46
47
48
49 public class TestGraph extends TestCase {
50
51 public TestGraph(String name) {
52 super(name);
53 }
54
55 private int m_pauseTime = 1;
56 private Random s_random = new Random();
57 private JFrame m_frame;
58
59 protected void setUp() throws Exception {
60 m_frame = new JFrame("Test Graph");
61 }
62
63 protected void tearDown() throws Exception {
64 m_frame.dispose();
65 }
66
67 private void createUI(JComponent component) throws Exception {
68 m_frame.getContentPane().add(component, BorderLayout.CENTER);
69 m_frame.pack();
70 m_frame.setVisible(true);
71 }
72
73 public void testRamp() throws Exception {
74 final Graph graph = new Graph(25);
75 createUI(graph);
76
77 graph.setMaximum(150);
78
79 for (int i = 0; i < 150; i++) {
80 graph.add(i);
81 pause();
82 }
83
84 graph.setMaximum(0);
85
86 for (int i = 0; i < 10; i++) {
87 graph.add(i);
88 pause();
89 }
90 }
91
92 public void testRandom() throws Exception {
93 final Graph graph = new Graph(100);
94 createUI(graph);
95
96 graph.setMaximum(1);
97
98 for (int i = 0; i < 200; i++) {
99 graph.add(s_random.nextDouble());
100 pause();
101 }
102 }
103
104 public void testLabelledGraph() throws Exception {
105 final StatisticsServices statisticsServices =
106 StatisticsServicesImplementation.getInstance();
107
108 final StatisticsIndexMap indexMap =
109 statisticsServices.getStatisticsIndexMap();
110
111 final StatisticsIndexMap.LongIndex periodIndex = indexMap
112 .getLongIndex("period");
113 final StatisticsIndexMap.LongIndex errorStatisticIndex = indexMap
114 .getLongIndex("errors");
115 final StatisticsIndexMap.LongIndex untimedTestsIndex = indexMap
116 .getLongIndex("untimedTests");
117 final StatisticsIndexMap.LongSampleIndex timedTestsIndex = indexMap
118 .getLongSampleIndex("timedTests");
119
120 final StatisticExpressionFactory statisticExpressionFactory =
121 statisticsServices.getStatisticExpressionFactory();
122
123 final StatisticExpression tpsExpression = statisticExpressionFactory
124 .createExpression("(* 1000 (/(+ (count timedTests) untimedTests) period))");
125
126 final PeakStatisticExpression peakTPSExpression = statisticExpressionFactory
127 .createPeak(indexMap.getDoubleIndex("peakTPS"), tpsExpression);
128
129 final LabelledGraph labelledGraph =
130 new LabelledGraph(
131 "Test",
132 new ResourcesImplementation(
133 "net.grinder.console.common.resources.Console"),
134 tpsExpression,
135 peakTPSExpression,
136 statisticsServices.getTestStatisticsQueries());
137
138 createUI(labelledGraph);
139
140 final StatisticsSetFactory statisticsSetFactory =
141 statisticsServices.getStatisticsSetFactory();
142
143 final StatisticsSet cumulativeStatistics = statisticsSetFactory.create();
144
145 final DecimalFormat format = new DecimalFormat();
146
147 final int period = 1000;
148
149 for (int i = 0; i < 200; i++) {
150 final StatisticsSet intervalStatistics = statisticsSetFactory.create();
151
152 intervalStatistics.setValue(periodIndex, period);
153
154 while (s_random.nextInt() > 0) {
155 intervalStatistics.addValue(untimedTestsIndex, 1);
156 }
157
158 long time;
159
160 while ((time = s_random.nextInt()) > 0) {
161 intervalStatistics.addSample(timedTestsIndex, time % 10000);
162 }
163
164 while (s_random.nextFloat() > 0.95) {
165 intervalStatistics.addValue(errorStatisticIndex, 1);
166 }
167
168 cumulativeStatistics.add(intervalStatistics);
169 cumulativeStatistics.setValue(periodIndex, (1 + i) * period);
170
171 peakTPSExpression.update(intervalStatistics, cumulativeStatistics);
172 labelledGraph.add(intervalStatistics, cumulativeStatistics, format);
173 pause();
174 }
175
176 LabelledGraph.resetPeak();
177 labelledGraph.calculateColour(100);
178 LabelledGraph.resetPeak();
179
180 final Color colour1 = labelledGraph.calculateColour(100);
181 assertFalse(colour1.equals(labelledGraph.calculateColour(50)));
182 assertEquals(colour1, labelledGraph.calculateColour(100));
183 assertEquals(colour1, labelledGraph.calculateColour(150));
184 assertEquals(colour1, labelledGraph.calculateColour(100));
185
186 LabelledGraph.resetPeak();
187 assertFalse(colour1.equals(labelledGraph.calculateColour(100)));
188 assertEquals(colour1, labelledGraph.calculateColour(150));
189
190 final LabelledGraph labelledGraph2 =
191 new LabelledGraph(
192 "Test",
193 new ResourcesImplementation(
194 "net.grinder.console.common.resources.Console"),
195 Colours.DARK_GREEN,
196 tpsExpression,
197 peakTPSExpression,
198 statisticsServices.getTestStatisticsQueries());
199 assertEquals(Colours.DARK_GREEN, labelledGraph2.calculateColour(100));
200 assertEquals(Colours.DARK_GREEN, labelledGraph2.calculateColour(0));
201 }
202
203 private void pause() throws Exception {
204 if (m_pauseTime > 0) {
205 Thread.sleep(m_pauseTime);
206 }
207 }
208 }
209