×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Checking upload speed in an application in j2me.

    Hi all,

    how can i check the upload speed in an application.


    Thanks

    pranshu

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: Checking upload speed in an application in j2me.

    Quote Originally Posted by shubham5263 View Post
    Hi all,
    how can i check the upload speed in an application.

    Thanks
    pranshu
    If you have created an app that uploads files to a server, you need to capture the time taken by your app in uploading - by recording start and end time from System.currentTimeMillis() with different file sizes in bytes... divide No. of Bytes/time taken to get the upload speed.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  3. #3
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Checking upload speed in an application in j2me.

    Hi pranshu,

    Keep in mind that the accuracy level provided by either System.currentTimeMillis() or Date.getTime() is given in milliseconds and not nanoseconds. This might be an issue if you attempt to measure the uploading speed of very small post requests for example.

    Here is an example of how one could measure the upload speed of data transfer implemented with the DataOutputStream.writeByte() method.

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import java.io.*;
     
     
     
    public class UploadSpeedMIDlet 
    	extends MIDlet 
    	implements CommandListener {
     
    	/** the default value for the url string is * * user can change it to some other urls within the application* */
    	private static String defaultURL = "https://www.google.fi/m";
    	 
    	// GUI component for user to enter web url
    	private Display myDisplay = null;
    	private Form mainScreen;
    	private TextField requestField;
    
    	
    	// the "send" button used on mainScreen
    	Command sendCommand = new Command("SEND", Command.OK, 1);
    	// the "back" button used on resultScreen
    	Command backCommand = new Command("BACK", Command.EXIT, 1);
    	 
    	public UploadSpeedMIDlet()
    	{
    		// initializing the GUI components for entering web url
    		myDisplay = Display.getDisplay(this);
    		mainScreen = new Form("Type in a URL:");
    		requestField = new TextField(null, defaultURL, 100, TextField.URL);
    		mainScreen.append(requestField);
    		mainScreen.addCommand(sendCommand);
    		mainScreen.addCommand(backCommand);
    		mainScreen.setCommandListener(this);
    
    	}
    	 
    	public void startApp() 
    	{
    		myDisplay.setCurrent(mainScreen);
    	}
    	 
    	public void pauseApp() {}
    	 
    	public void destroyApp(boolean unconditional) {}
    	 
    	public void commandAction(Command c, Displayable s) 
    	{
    		// when user clicks on "send" button on mainScreen
    		if (c == sendCommand) 
    		{
    			// retrieving the web url that user enteredString 
    			String urlstring = requestField.getString();
    	 
    			// sending a POST request to web serverString with hard coded attributes
    			
    			sendPostRequest(urlstring);
    	 
    		} 
    		else if (c == backCommand) 
    		{
    			notifyDestroyed();
    		}
    	}
    	
    	// send a POST request to web server
    	public String sendPostRequest(String urlstring) 
    	{
    		HttpConnection hc = null;
    		DataInputStream dis = null;
    		DataOutputStream dos = null;
    	 
    		String message = "";
    	 
    		// specifying the query string
    		String requeststring = "q=SomeVeryLongRequestStringInOrderToBeAbleToCountTimeInMillisecondsDelta";
    		try {
    			// openning up http connection with the web server
    			// for both read and write access
    			hc = (HttpConnection) Connector.open(urlstring, Connector.READ_WRITE);
    	 
    			// setting the request method to POST
    			hc.setRequestMethod(HttpConnection.POST);
    	 
    			// obtaining output stream for sending query string
    			dos = hc.openDataOutputStream();
    			byte[] request_body = requeststring.getBytes();
    	 			
    			//counts the bytes sent
    			int byteCounter = 1;
    			//used for calculating the time between bytes that are being sent
    			long previous = 0;
    			long now = 0;
    			
    			float dif = 0;
    			
    			for (int i = 0; i < request_body.length; i++){
    				
    				//if this is the first round previous has not been initialized
    				if( now != 0) {
    					previous = now;				
    				}
    				
    				now = System.currentTimeMillis();
    				
    				if(previous != 0){ 
    
    					dif = now - previous;			
    					
    					//the speed can only be calculated if there is a delta
    					//in the time stamps between this byte and the previous byte sent
    					if( dif != 0) {	
    						mainScreen.append(byteCounter/dif + " bytes/ms\n");
    						byteCounter = 0;
    					}
    				}
    				//the actual uploading
    				dos.writeByte(request_body[i]);
    				
    				byteCounter = byteCounter + 1;			
    			} 
    			mainScreen.append("Done!\n");
    		}
    		catch (IOException ioe) {
    			
    		} 
    		finally {
    			try { 
    				if (hc != null) {
    					hc.close();
    				}
    			}
    			catch (IOException ignored) {
    				//add error message here
    			}
    			
    			try { 
    				if (dis != null) {
    					dis.close();
    				}
    			} 
    			catch (IOException ignored) {
    				//add error message here
    			} 
    			try 
    			{ 
    				if (dos != null){ 
    					dos.close();
    				}
    			} 
    			catch (IOException ignored) {
    				//add error message here
    			} 
    		}
    		return message;
    	}
    }

Similar Threads

  1. Is there already a tool used for measure latency, down-/upload speed?
    By paipeng in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2009-10-05, 20:25
  2. How to increase speed of J2ME application
    By j.wawdhane in forum Mobile Java General
    Replies: 6
    Last Post: 2009-01-05, 08:53
  3. content upload and checking for viruses
    By qasitouch in forum Digital Rights Management & Content Downloading
    Replies: 5
    Last Post: 2007-08-24, 15:57
  4. Programmatically checking JSR support in j2me app
    By pkiddie in forum Mobile Java General
    Replies: 3
    Last Post: 2007-01-13, 23:50

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×