1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs.perf;
18  
19  import org.apache.commons.vfs.FileName;
20  import org.apache.commons.vfs.FileObject;
21  import org.apache.commons.vfs.FileSystemException;
22  import org.apache.commons.vfs.FileSystemManager;
23  import org.apache.commons.vfs.VFS;
24  
25  public class FileNamePerformance
26  {
27  	private final static int NUOF_RESOLVES = 100000;
28  
29  	public static void main(String[] args) throws FileSystemException
30  	{
31  		FileSystemManager mgr = VFS.getManager();
32  
33  		FileObject root = mgr
34  				.resolveFile("smb://HOME//vfsusr:vfs%2f%25//te:st@10.0.1.54/vfsusr");
35  		FileName rootName = root.getName();
36  	
37  		testNames(mgr, rootName);
38  
39  		testChildren(root);
40  
41  		testFiles(mgr);
42  	}
43  
44  	private static void testFiles(FileSystemManager mgr) throws FileSystemException
45  	{
46  		for (int i = 0; i < 10; i++)
47  		{
48  			// warmup jvm
49  			mgr.resolveFile("smb://HOME//vfsusr:vfs%2f%25//te:st@10.0.1.54/vfsusr/many/path/elements/with%25esc/any%25where/to/file.txt");
50  		}
51  
52  		long start = System.currentTimeMillis();
53  		for (int i = 0; i < NUOF_RESOLVES; i++)
54  		{
55  			mgr.resolveFile("smb://HOME//vfsusr:vfs%2f%25//te:st@10.0.1.54/vfsusr/many/path/elements/with%25esc/any%25where/to/file.txt");
56  		}
57  		long end = System.currentTimeMillis();
58  
59  		System.err.println("time to resolve " + NUOF_RESOLVES + " files: "
60  				+ (end - start) + "ms");
61  	}
62  
63  	private static void testChildren(FileObject root) throws FileSystemException
64  	{
65  		for (int i = 0; i < 10; i++)
66  		{
67  			// warmup jvm
68  			root.resolveFile("/many/path/elements/with%25esc/any%25where/to/file.txt");
69  		}
70  
71  		long start = System.currentTimeMillis();
72  		for (int i = 0; i < NUOF_RESOLVES; i++)
73  		{
74  			root.resolveFile("/many/path/elements/with%25esc/any%25where/to/file.txt");
75  		}
76  		long end = System.currentTimeMillis();
77  
78  		System.err.println("time to resolve " + NUOF_RESOLVES + " childs: "
79  				+ (end - start) + "ms");
80  	}
81  
82  	private static void testNames(FileSystemManager mgr, FileName rootName) throws FileSystemException
83  	{
84  		for (int i = 0; i < 10; i++)
85  		{
86  			// warmup jvm
87  			mgr.resolveName(rootName,
88  					"/many/path/elements/with%25esc/any%25where/to/file.txt");
89  		}
90  
91  		long start = System.currentTimeMillis();
92  		for (int i = 0; i < NUOF_RESOLVES; i++)
93  		{
94  			mgr.resolveName(rootName,
95  					"/many/path/elements/with%25esc/any%25where/to/file.txt");
96  		}
97  		long end = System.currentTimeMillis();
98  
99  		System.err.println("time to resolve " + NUOF_RESOLVES + " names: "
100 				+ (end - start) + "ms");
101 	}
102 }