Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Need...more...speed!

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need...more...speed!

    I am participating in a cyber-sec course at the local uni. Our first project involves a web server that has a submission form and students scors reflected on it. You gain points by submitting the hash value of a secret word of a fellow student. For every point you gain, they lose a point.

    One of the aspects of the project is automation. I noticed that if I just refresh the submission confirmed page, it resubmits my points. So, I wrote a simple java program that just access this page as fast as it can.

    My question: Can any of you spot a way to make this faster? It seems it takes almost a second or two for each submission. Is the u.openStream(); waiting for a response? Is there a way to make it just submit, then ignore a response? Is this the most efficient way to be doing this?

    URL I am submitting is as follows:
    http://um.cyber.rose.edu/TNSubmissio...h=STUDENTSHASH

    The code I have thus far is below.

    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.io.IOException;
     
    import java.io.BufferedInputStream;
     
    class XThread extends Thread {
     
    	XThread() {
    	}
    	XThread(String threadName) {
    		super(threadName); // Initialize thread.
    		//System.out.println(this);
    		start();
    	}
    	public void run() {
    		//Display info about this particular thread
             URL u;
    	      BufferedInputStream is = null;
    	      String myNAME = new String("me");
    	      String vNAME = new String("andrew");
    	      String vHASH = new String("4252943e53d9677d1ef12f4fad9e026b");//me
     
     
    	    //  int wall=1;
     
    	      try {
    	        u = new URL("http://um.cyber.rose.edu/TNSubmissionHandler.aspx?Name="+myNAME+"&Victim="+vNAME+"&VictimHash="+vHASH);
     
     
    		for (int i = 0; i < 1000000; i++) {
    	        	is = new BufferedInputStream(u.openStream());
    	        	System.out.println(i);
    	        	is.close();
    }
     
    	      } catch (MalformedURLException mue) {
     
    	         System.out.println("Ouch - a MalformedURLException happened.");
    	         mue.printStackTrace();
    	         System.exit(1);
     
    	      } catch (IOException ioe) {
     
    	         System.out.println("Oops- an IOException happened.");
    	         ioe.printStackTrace();
    	         System.exit(1);
     
    	      } finally {
     
    	         try {
    	            is.close();
    	         } catch (IOException ioe) {
    	            // just going to ignore this one
    	         }
    	      } // end of 'finally' clause
       }
    }
     
    public class SingleUser {
     
    	public static void main(String[] args) {
    Thread thread1 = new XThread();
    Thread thread2 = new XThread();
    Thread thread3 = new XThread();
    Thread thread4 = new XThread();
    Thread thread5 = new XThread();
    Thread thread6 = new XThread();
    Thread thread7 = new XThread();
    Thread thread8 = new XThread();
    Thread thread9 = new XThread();
    Thread thread10 = new XThread();
    Thread thread11 = new XThread();
    Thread thread12 = new XThread();
    Thread thread13 = new XThread();
    Thread thread14 = new XThread();
    Thread thread15 = new XThread();
    Thread thread16 = new XThread();
    Thread thread17 = new XThread();
    Thread thread18 = new XThread();
    Thread thread19 = new XThread();
    Thread thread20 = new XThread();
     
     
    		//Start the threads
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    thread5.start();
    thread6.start();
    thread7.start();
    thread8.start();
    thread9.start();
    thread10.start();
    thread11.start();
    thread12.start();
    thread13.start();
    thread14.start();
    thread15.start();
    thread16.start();
    thread17.start();
    thread18.start();
    thread19.start();
    thread20.start();
     
    }
    Basically, start twenty (or more) threads, and in each thread loop the openStream().

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need...more...speed!

    Is this a way to cheat?

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need...more...speed!

    Quote Originally Posted by nPeep View Post
    You gain points by submitting the hash value of a secret word of a fellow student. For every point you gain, they lose a point.

    One of the aspects of the project is automation.
    No. It is within the scope of the project. In fact, the database and webfront-end were written and optimized for this to be possible.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need...more...speed!


  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need...more...speed!

    Quote Originally Posted by Norm View Post
    Yeah....yeah it is. Not seperate sites, then? Just looking to poll the opinions of two seperate communities, but thanks for the notification, Norm.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need...more...speed!

    As long as we're aware of where else this same discussion is taking place, we're ok with cross posting

    Unless your computer has 20 physical cores, it makes no sense to have that many threads in your application. These are just a waste of memory and cpu time.

    The number of cores you use should closely match the number of physical cores you have (the exception being threads that don't try to utilize 100% cpu core usage). You'll also be limited by your network connection speed (as well as the server's network speed).

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need...more...speed!

    Quote Originally Posted by helloworld922 View Post
    As long as we're aware of where else this same discussion is taking place, we're ok with cross posting

    Unless your computer has 20 physical cores, it makes no sense to have that many threads in your application. These are just a waste of memory and cpu time.

    The number of cores you use should closely match the number of physical cores you have (the exception being threads that don't try to utilize 100% cpu core usage). You'll also be limited by your network connection speed (as well as the server's network speed).
    Gotcha

    I figure that a thread to access the site shouldn't take up much CPU usage. But I have definitely noticed a performance boost between two and 20 threads.

    Another question: Would there be a way to just open one connection and continuously submit this url with it? If I understand it correctly, I think the current code is opening a new connection everytime.

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Need...more...speed!

    Quote Originally Posted by nPeep View Post
    If I understand it correctly, I think the current code is opening a new connection everytime.
    Now that's exactly how HTTP works, it opens a connection and closes it. As of HTML 5 and Java Servlet 3.0 spec there will be more support for things such as WebSockets to allow async communication both ways between the server and the browser.

    Anyway, I'm getting off topic. It seems as though your code might almost be trying to DoS the server

    This might not be much faster but it would shorten your code down slightly.

    final URL url = new URL("http://some.url.here/");
     
    for(int i = 0; i < 100; ++i) {
        final HttpURLConnection connection = url.openConnection();
        connection.connect();
    }

    // Json
    Last edited by Json; September 6th, 2010 at 08:06 AM.

  9. #9
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Need...more...speed!

    http://en.wikipedia.org/wiki/Ddos#Distributed_attack

    DDOS is punishable for 5 years, I'm pretty sure. BE CAREFUL

Similar Threads

  1. KB/s download speed calculating
    By Koâk in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2009, 03:05 PM