Source code: com/port80/graph/dot/impl/Anneal_TestBenchmark01.java
1 //
2 // Copyright(c) 2002, Chris Leung
3 //
4
5 package com.port80.graph.dot.impl;
6
7 import java.io.File;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import junit.framework.Assert;
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15
16 import com.port80.graph.IGraph;
17 import com.port80.graph.dot.impl.GridFactory.GridIterator;
18 import com.port80.util.Debug;
19 import com.port80.util.SystemWatch;
20 import com.port80.util.msg;
21 import com.port80.util.sprint;
22 import com.port80.util.struct.IntList;
23
24 /**
25 * @author chrisl
26 *
27 * To change this generated comment edit the template variable "typecomment":
28 * Window>Preferences>Java>Templates.
29 * To enable and disable the creation of type comments go to
30 * Window>Preferences>Java>Code Generation.
31 */
32 public class Anneal_TestBenchmark01 extends TestCase {
33
34 ////////////////////////////////////////////////////////////////////////
35
36 private static final String NAME = "TestAnnealBenchmark_01";
37 private static final boolean DEBUG = false;
38 private static boolean VERBOSE = false;
39 private static boolean CHECK = true;
40
41 private static boolean fSAVE=false;
42
43 ////////////////////////////////////////////////////////////////////////
44
45 static private String fTestFilename;
46 private IGraph fGraph;
47 private VirtualGraph fVGraph;
48
49 ////////////////////////////////////////////////////////////////////////
50
51 public Anneal_TestBenchmark01(String name) {
52 super(name);
53 }
54
55 ////////////////////////////////////////////////////////////////////////
56
57 public void testAnnealBenchmark_01() {
58 fGraph = TestUtil.createGraph(new File(fTestFilename));
59 fVGraph = new VirtualGraph(fGraph);
60 MinCross.dot(fVGraph, 0, 6, 0);
61 fVGraph.removeAux();
62 Position.dotPosition(fVGraph);
63 if (fSAVE) {
64 // Save graph layout.
65 Position.saveResult(fVGraph);
66 TestUtil.updateShape(fGraph);
67 Route.dot(fVGraph);
68 msg.println(fGraph.sprintGraph());
69 TestUtil.saveImage("out/test/testAnnealBenchmark_01", 1f, fGraph);
70 }
71 Anneal anneal = new Anneal(fVGraph,1);
72 //
73 benchmarkRankCost(anneal, fVGraph);
74 benchmarkGridIterator(fVGraph);
75 }
76
77 /**
78 * Simple benchmark of the rank cost methods.
79 */
80 public void benchmarkRankCost(Anneal anneal, VirtualGraph vgraph) {
81 final String PREFIX = NAME + ".benchmarkRankCost(): ";
82 if (VERBOSE)
83 msg.println("\n### " + PREFIX);
84 {
85 int ITER = 1000;
86 float MIN = 1000*1000f;
87 // float MIN = 900*1000f;
88 int distcost = 64;
89 SystemWatch timer = new SystemWatch().start();
90 //
91 VirtualGraph.Rank rank;
92 int n=0;
93 for (int i = 0; i < ITER; ++i) {
94 for (int r = vgraph.minRank; r < vgraph.maxRank; ++r) {
95 vgraph.staticRankCost(r);
96 n+=vgraph.ranks[r].nVts;
97 }
98 }
99 timer.stop();
100 float benchmark = n/ timer.elapsed();
101 if (true)
102 msg.println(
103 sprint
104 .f("\n### %-32s: %8d iterations in %.2f, %d iter/sec, %d vertex/sec. (requierd=%d)")
105 .a("rankCost() benchmark")
106 .a(ITER)
107 .a(timer.elapsed())
108 .a(ITER/timer.elapsed())
109 .a(benchmark)
110 .a(MIN)
111 .end());
112 if (CHECK) {
113 Assert.assertTrue(
114 PREFIX
115 + "benchmark too low: expected>"
116 + MIN
117 + " iter/sec: actual="
118 + ITER / timer.elapsed(),
119 benchmark > MIN);
120 }
121 }
122 }
123
124 //
125 // Simple benchmark of GridIterator.
126 //
127 public void benchmarkGridIterator(VirtualGraph vgraph) {
128 final String PREFIX = NAME + ".benchmarkGridIterator(): ";
129 if (VERBOSE)
130 msg.println("\n# GridIterator benchmark:");
131 int ITER = 250;
132 float MIN = 1800*1000f;
133 int DISTCOST = 100*100;
134 int distcost=DISTCOST;
135 GridFactory factory = new GridFactory(fVGraph,1);
136 //1
137 Grid grid;
138 GridFactory.GridIterator it;
139 VirtualGraph.Rank rank, rank1;
140 VirtualVertex src, dest;
141 //
142 IntList xs = new IntList(100);
143 vgraph.staticCost();
144 if (VERBOSE) {
145 int[][] spacetable=factory.getSpaceTable();
146 for (int r = vgraph.minRank; r < vgraph.maxRank; ++r) {
147 int[] spaces=spacetable[r];
148 msg.println("r="+r+", spaces=",spaces);
149 }
150 }
151 SystemWatch timer = new SystemWatch().start();
152 int n=0;
153 for (int i = 0; i < ITER; ++i) {
154 factory.clear();
155 rank=vgraph.ranks[vgraph.minRank];
156 rank1=vgraph.ranks[vgraph.maxRank];
157 factory.initGrid(rank.vts[0],rank1.vts[rank1.nVts-1]);
158 for (int r = vgraph.minRank; r < vgraph.maxRank; ++r) {
159 rank = vgraph.ranks[r];
160 rank1 = vgraph.ranks[r + 1];
161 src = rank.vts[rank.nVts / 4];
162 dest = rank1.vts[rank1.nVts * 3 / 4];
163 distcost+=500*500;
164 if (distcost > 2000 * 2000)
165 distcost = DISTCOST;
166 xs.clear();
167 it = factory.new GridIterator(src.rank, src.x, distcost);
168 for (grid = it.next(); grid != null; grid = it.next()) {
169 xs.add(grid.x);
170 if (Grid.distCostOf(src.x-grid.x) > distcost)
171 break;
172 }
173 if(VERBOSE) msg.println("r=" + r + ", allocated grids="+factory.size()+", distcost=" + distcost+"\n"+xs);
174 n+=xs.size();
175 }
176 }
177 timer.stop();
178 float benchmark = n/ timer.elapsed();
179 if (true)
180 msg.println(
181 sprint
182 .f("### %-32s: %8d iterations in %.2f, %d iter/sec, %d vertices/sec. (required=%d)")
183 .a("GridIterator benchmark")
184 .a(ITER)
185 .a(timer.elapsed())
186 .a(ITER/timer.elapsed())
187 .a(benchmark)
188 .a(MIN)
189 .end());
190 if (CHECK) {
191 Assert.assertTrue(
192 PREFIX
193 + "benchmark too low: expected>"
194 + MIN
195 + " iter/sec: actual="
196 + ITER / timer.elapsed(),
197 benchmark > MIN);
198 }
199 }
200
201 // Main ////////////////////////////////////////////////////////////////
202 //
203
204 public static void main(String[] args) {
205 Map opts = new HashMap();
206 args = msg.getArgs(opts, NAME, args, "- verbose=v nocheck=c save=s");
207 if (args.length == 0)
208 usage();
209 fTestFilename = args[0];
210 if (opts.get("verbose") != null) {
211 Debug.enable("verbose");
212 VERBOSE = true;
213 }
214 if (opts.get("nocheck") != null) {
215 CHECK = false;
216 }
217 if (opts.get("save") != null) {
218 fSAVE= true;
219 }
220 junit.textui.TestRunner.run(new TestSuite(Anneal_TestBenchmark01.class));
221 // junit.swingui.TestRunner.run(new TestVirtualGraph("TestVirtualGraph").getClass());
222 }
223
224 private static void usage() {
225 msg.print("Usage: " + NAME + " <dotfile>");
226 System.exit(100);
227 }
228
229 ////////////////////////////////////////////////////////////////////////
230
231 }