KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SmbThreadTest


1 import jcifs.smb.SmbFile;
2 import jcifs.smb.NtlmPasswordAuthentication;
3 import jcifs.smb.SmbAuthException;
4
5
6 import java.util.Random JavaDoc;
7 import java.net.MalformedURLException JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 public class SmbThreadTest extends Thread JavaDoc {
11
12     int maxDepth;
13     int id;
14     String JavaDoc url;
15     NtlmPasswordAuthentication auth;
16     long start_time;
17
18     static Random JavaDoc rnd = new Random JavaDoc(1234);
19     static long test_time = 100*1000;
20     static long num_sessions = 1000;
21     static long session_time = (test_time / num_sessions) * 400;
22
23     static boolean verbose = false;
24
25
26     SmbThreadTest(NtlmPasswordAuthentication auth, String JavaDoc url, int maxDepth, int id) {
27         this.url = url;
28         this.auth = auth;
29         this.maxDepth = maxDepth;
30         this.id = id;
31         this.start_time = System.currentTimeMillis();
32     }
33
34     void traverse( SmbFile f, int depth ) throws MalformedURLException JavaDoc, IOException JavaDoc {
35
36         if( depth == 0 ) {
37             return;
38         }
39         SmbFile[] l = null;
40         try {
41             if (f.exists())
42                 l = f.listFiles();
43         } catch (SmbAuthException ae) {
44             System.err.println("SAE: " + ae.getMessage());
45             ae.printStackTrace( System.err );
46             return;
47         } catch (NullPointerException JavaDoc npe) {
48             System.err.println("NPE");
49             npe.printStackTrace( System.err );
50             return;
51         }
52         for(int i = 0; l != null && i < l.length; i++ ) {
53             try {
54                 boolean exists = l[i].exists();
55                 if (verbose) {
56                     System.out.print(id);
57                     for( int j = maxDepth - depth; j > 0; j-- ) {
58                        System.out.print( " " );
59                     }
60                     System.out.println( l[i] + " " + exists );
61                 }
62                 if( l[i].isDirectory() ) {
63                     traverse( l[i], depth - 1 );
64                 }
65             } catch (SmbAuthException ae) {
66                 System.err.println("SAE: " + ae.getMessage());
67                 ae.printStackTrace( System.err );
68             } catch( IOException JavaDoc ioe ) {
69                 System.out.println( l[i] + ":" );
70                 ioe.printStackTrace( System.out );
71             }
72             try {
73                 Thread.sleep(Math.abs(rnd.nextInt(2)+1));
74             } catch (InterruptedException JavaDoc e) {
75
76             }
77         }
78     }
79
80     public void run () {
81         SmbFile f = null;
82         int runs = 0;
83         while(true) {
84             try {
85                 Thread.sleep(100);
86             }catch (InterruptedException JavaDoc e) {}
87
88             while (f == null) {
89                 try {
90                     f = new SmbFile(url, auth);
91                 } catch (Exception JavaDoc e) {
92                     System.err.println(e.getMessage());
93                     e.printStackTrace();
94                 }
95             }
96             try {
97                 traverse(f, maxDepth);
98             } catch (Exception JavaDoc e) {
99                 System.err.println(e.getMessage());
100                 e.printStackTrace();
101             }
102             runs++;
103             long time = System.currentTimeMillis() - start_time;
104             if (time > session_time) {
105                 System.err.println(id + " exit (" + time/runs + ")");
106                 return;
107             }
108         }
109     }
110
111     public static void createThreads(String JavaDoc url, int i, int count) {
112         NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null);
113         int num = 0;
114         System.err.println("creating " + count + " threads");
115         while (num < count) {
116             SmbThreadTest sc = new SmbThreadTest(auth, url, 3, i * 100 + num++);
117             sc.start();
118             try {
119                 Thread.sleep(50);
120             } catch (InterruptedException JavaDoc e) {
121                 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
122
}
123         }
124     }
125
126     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
127         for(int i = 0; i < num_sessions; i++) {
128             createThreads(argv[0], i+1, Math.abs(rnd.nextInt(4)+1));
129             sleep((test_time / num_sessions)*100);
130         }
131         sleep(6000000);
132     }
133 }
134
135
Popular Tags