001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.core;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.Date;
023: import java.util.Random;
024:
025: import org.apache.ivy.Ivy;
026: import org.apache.ivy.core.cache.DefaultResolutionCacheManager;
027: import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
028: import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
029: import org.apache.ivy.core.module.id.ModuleRevisionId;
030: import org.apache.ivy.core.report.ResolveReport;
031: import org.apache.ivy.core.resolve.ResolveOptions;
032: import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter;
033: import org.apache.ivy.plugins.resolver.FileSystemResolver;
034: import org.apache.ivy.util.FileUtil;
035: import org.apache.tools.ant.Project;
036: import org.apache.tools.ant.taskdefs.Delete;
037:
038: /**
039: * Not a Junit test, performance depends on the machine on which the test is run...
040: */
041: public class TestPerformance {
042: private static final String PATTERN = "build/test/perf/[module]/[artifact]-[revision].[ext]";
043:
044: private final Ivy ivy;
045:
046: private File cache;
047:
048: public TestPerformance() throws Exception {
049: ivy = new Ivy();
050: FileSystemResolver resolver = new FileSystemResolver();
051: resolver.setName("def");
052: resolver.setSettings(ivy.getSettings());
053:
054: resolver.addIvyPattern(PATTERN);
055: resolver.addArtifactPattern(PATTERN);
056:
057: ivy.getSettings().addResolver(resolver);
058: ivy.getSettings().setDefaultResolver("def");
059: }
060:
061: protected void setUp() throws Exception {
062: createCache();
063: }
064:
065: private void createCache() {
066: cache = new File("build/cache");
067: cache.mkdirs();
068: }
069:
070: protected void tearDown() throws Exception {
071: cleanCache();
072: }
073:
074: private void cleanCache() {
075: Delete del = new Delete();
076: del.setProject(new Project());
077: del.setDir(cache);
078: del.execute();
079: }
080:
081: private void cleanRepo() {
082: Delete del = new Delete();
083: del.setProject(new Project());
084: del.setDir(new File("build/test/perf"));
085: del.execute();
086: }
087:
088: private void generateModules(int nbModules, int minDependencies,
089: int maxDependencies, int minVersions, int maxVersions)
090: throws IOException {
091: int nb = 0;
092: int curDep = 1;
093: int varDeps = maxDependencies - minDependencies;
094: int varVersions = maxVersions - minVersions;
095: Random r = new Random(System.currentTimeMillis());
096:
097: while (nb < nbModules) {
098: int deps = minDependencies + r.nextInt(varDeps + 1);
099: int versions = minVersions + r.nextInt(varVersions + 1);
100:
101: int prevCurDep = curDep;
102: for (int ver = 0; ver < versions; ver++) {
103: DefaultModuleDescriptor md = new DefaultModuleDescriptor(
104: ModuleRevisionId.newInstance("apache", "mod"
105: + nb, "1." + ver), "integration",
106: new Date());
107:
108: curDep = prevCurDep;
109: for (int i = 0; i < deps && curDep < nbModules; i++) {
110: int d;
111: if (i % 2 == 1) {
112: d = nb + i;
113: if (d >= prevCurDep) {
114: d = curDep;
115: curDep++;
116: }
117: } else {
118: d = curDep;
119: curDep++;
120: }
121: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
122: md, ModuleRevisionId.newInstance("apache",
123: "mod" + d, "latest.integration"),
124: false, false, true);
125: dd.addDependencyConfiguration("default", "default");
126: md.addDependency(dd);
127: }
128: XmlModuleDescriptorWriter.write(md, new File(
129: "build/test/perf/mod" + nb + "/ivy-1." + ver
130: + ".xml"));
131: FileUtil
132: .copy(
133: new File(
134: "test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"),
135: new File("build/test/perf/mod" + nb
136: + "/mod" + nb + "-1." + ver
137: + ".jar"), null);
138: }
139: nb++;
140: }
141: }
142:
143: public void testPerfs() throws Exception {
144: generateModules(70, 2, 5, 2, 15);
145:
146: long start = System.currentTimeMillis();
147: ResolveReport report = ivy.resolve(new File(
148: "build/test/perf/mod0/ivy-1.0.xml").toURL(),
149: getResolveOptions(new String[] { "*" }).setRevision(
150: "1.0"));
151: long end = System.currentTimeMillis();
152: System.out.println("resolve "
153: + report.getConfigurationReport("default")
154: .getNodesNumber() + " modules took "
155: + (end - start) + " ms");
156:
157: cleanRepo();
158: }
159:
160: private ResolveOptions getResolveOptions(String[] confs) {
161: return new ResolveOptions().setConfs(confs);
162: }
163:
164: public static void main(String[] args) throws Exception {
165: TestPerformance t = new TestPerformance();
166: t.setUp();
167: t.testPerfs();
168: t.tearDown();
169: }
170: }
|