KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > TestPerformance


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.Random JavaDoc;
13
14 import org.apache.tools.ant.Project;
15 import org.apache.tools.ant.taskdefs.Delete;
16
17 import fr.jayasoft.ivy.report.ResolveReport;
18 import fr.jayasoft.ivy.resolver.FileSystemResolver;
19 import fr.jayasoft.ivy.util.FileUtil;
20 import fr.jayasoft.ivy.xml.XmlModuleDescriptorWriter;
21
22 /**
23  * Not a Junit test, performance depends on the machine on which the test is run...
24  */

25 public class TestPerformance {
26     private final static String JavaDoc PATTERN = "build/test/perf/[module]/[artifact]-[revision].[ext]";
27
28     private final Ivy _ivy;
29     private File JavaDoc _cache;
30
31     public TestPerformance() throws Exception JavaDoc {
32         _ivy = new Ivy();
33         FileSystemResolver resolver = new FileSystemResolver();
34         resolver.setName("def");
35         resolver.setIvy(_ivy);
36         
37         resolver.addIvyPattern(PATTERN);
38         resolver.addArtifactPattern(PATTERN);
39         
40         _ivy.addResolver(resolver);
41         _ivy.setDefaultResolver("def");
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45         createCache();
46     }
47
48     private void createCache() {
49         _cache = new File JavaDoc("build/cache");
50         _cache.mkdirs();
51     }
52     
53     protected void tearDown() throws Exception JavaDoc {
54         cleanCache();
55     }
56
57     private void cleanCache() {
58         Delete del = new Delete();
59         del.setProject(new Project());
60         del.setDir(_cache);
61         del.execute();
62     }
63
64     private void cleanRepo() {
65         Delete del = new Delete();
66         del.setProject(new Project());
67         del.setDir(new File JavaDoc("build/test/perf"));
68         del.execute();
69     }
70
71
72     private void generateModules(int nbModules, int minDependencies, int maxDependencies, int minVersions, int maxVersions) throws IOException JavaDoc {
73         int nb = 0;
74         int curDep = 1;
75         int varDeps = maxDependencies - minDependencies;
76         int varVersions = maxVersions - minVersions;
77         Random JavaDoc r = new Random JavaDoc(System.currentTimeMillis());
78         
79         while (nb < nbModules) {
80             int deps = minDependencies + r.nextInt(varDeps+1);
81             int versions = minVersions + r.nextInt(varVersions+1);
82             
83             int prevCurDep = curDep;
84             for (int ver = 0; ver < versions; ver++) {
85                 DefaultModuleDescriptor md = new DefaultModuleDescriptor(ModuleRevisionId.newInstance("jayasoft", "mod"+nb, "1."+ver),
86                         "integration", new Date JavaDoc());
87                 
88                 curDep = prevCurDep;
89                 for (int i = 0; i<deps && curDep < nbModules; i++) {
90                     int d;
91                     if (i%2 == 1) {
92                         d = nb + i;
93                         if (d >= prevCurDep) {
94                             d = curDep;
95                             curDep++;
96                         }
97                     } else {
98                         d = curDep;
99                         curDep++;
100                     }
101                     DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md,
102                             ModuleRevisionId.newInstance("jayasoft", "mod"+d, "latest.integration"),
103                             false, false, true);
104                     dd.addDependencyConfiguration("default", "default");
105                     md.addDependency(dd);
106                 }
107                 XmlModuleDescriptorWriter.write(md, new File JavaDoc("build/test/perf/mod"+nb+"/ivy-1."+ver+".xml"));
108                 FileUtil.copy(new File JavaDoc("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), new File JavaDoc("build/test/perf/mod"+nb+"/mod"+nb+"-1."+ver+".jar"), null);
109             }
110             nb++;
111         }
112     }
113     
114     public void testPerfs() throws Exception JavaDoc {
115         generateModules(70, 2, 5, 2, 15);
116         
117         long start = System.currentTimeMillis();
118         ResolveReport report = _ivy.resolve(new File JavaDoc("build/test/perf/mod0/ivy-1.0.xml").toURL(), "1.0", new String JavaDoc[] {"*"}, _cache, null, true);
119         long end = System.currentTimeMillis();
120         System.out.println("resolve "+report.getConfigurationReport("default").getNodesNumber()+" modules took "+(end - start)+" ms");
121
122         cleanRepo();
123     }
124     
125     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
126         TestPerformance t = new TestPerformance();
127         t.setUp();
128         t.testPerfs();
129         t.tearDown();
130     }
131 }
132
Popular Tags