| Author |
CPU utilization
|
Venkata Pavan Kumar vemuri
Greenhorn
Joined: Feb 01, 2010
Posts: 14
|
|
Hi All,
I am running a SAX parser and DOM parser application, I want to calculate the CPU utilization in MIPS for this java application. Can anyone please help me in this regard.
Thanks
Pavan.
|
 |
Venkata Pavan Kumar vemuri
Greenhorn
Joined: Feb 01, 2010
Posts: 14
|
|
I found the following code snippet but I am not able to understand what they are trying to do can anyone elaborate on this
I have put the part of code in bold
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class benchmarkSAX
{
public static void main(String argv[])
{
long a;
try
{
// Set up output stream
//out = new OutputStreamWriter(System.out, "UTF8");
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
File f= new File(argv[0]);
FileInputStream fi = null;
byte[] bt;
fi = new FileInputStream(f);
System.out.println(" file size "+(int)f.length());
bt = new byte[(int)f.length()];
fi.read(bt);
SAXParser saxParser = factory.newSAXParser();
int total;
int fl = (int) f.length();
if (fl <6000)
total = 2000;
else if (fl <15000)
total = 800;
if (fl<30000)
total = 500;
else if (fl < 60000)
total = 300;
else if (fl < 120000)
total = 150;
else if (fl <500000)
total = 50;
else if (fl < 2000000)
total = 20;
else
total = 5;;
long lt = 0;
a = System.currentTimeMillis();
ByteArrayInputStream bais =
new ByteArrayInputStream(bt);
while(System.currentTimeMillis()-a <30000)
{
bais.reset();
saxParser.parse( bais, (DefaultHandler) null );
//saxParser.parse( new File(argv[0]), handler );
}
// Parse the input
//saxParser = factory.newSAXParser();
for (int j = 0;j<10;j++){
a = System.currentTimeMillis();
for(int i=0;i<total;i++)
{
bais.reset();
saxParser.parse( bais, (DefaultHandler) null );
//saxParser.parse( new File(argv[0]), handler );
}
long l2 = System.currentTimeMillis();
lt = lt + (l2 -a);
}
System.out.println(" average parsing time ==> "+
((float)(lt)/total/10));
System.out.println(" performance ==> "+
( ((double)fl *1000 * total)/((lt/10)*(1<<20))));
}
catch (SAXException e)
{
System.out.println( e);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}
Thanks in advance
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
It looks like rubbish to me (even disregarding the fact that you didn't post it in code tags so it looks ugly because of that). However, the best strategy to find out why somebody wrote some particular code is to ask them. Why not try that?
|
 |
Venkata Pavan Kumar vemuri
Greenhorn
Joined: Feb 01, 2010
Posts: 14
|
|
|
Can you answer the first question please
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
This is something that is not possible from within a thread, or even within a process. This is something that has to be done by the hardware or done by the kernal. And it is usually done by measuring the amount of "ticks" counted for an application. The count of the times an application stack frame is the current frame when a timer interrupt occurs.
The Java process must get it from the kernal. For example, with unix, you can call "ps", or even look at the procfs stats file for the java application's pid. For windows, you can call "typeperf".
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
Paul Clapham wrote:It looks like rubbish to me (even disregarding the fact that you didn't post it in code tags so it looks ugly because of that). However, the best strategy to find out why somebody wrote some particular code is to ask them. Why not try that?
It looks like it was probably done by Statistics / Curve Fitting. Meaning take a bunch of computers, with different speeds. Measure how long they take to finish a known job. Measure the CPU utilization during those jobs. And curve fit all of those points to get a formula that will extrapolate the utilization based on the measured data.
Of course, I could be wrong, and the programmer could have just pulled the formula out of thin air.
Henry
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Henry Wong wrote:
Paul Clapham wrote:It looks like rubbish to me (even disregarding the fact that you didn't post it in code tags so it looks ugly because of that). However, the best strategy to find out why somebody wrote some particular code is to ask them. Why not try that?
It looks like it was probably done by Statistics / Curve Fitting. Meaning take a bunch of computers, with different speeds. Measure how long they take to finish a known job. Measure the CPU utilization during those jobs. And curve fit all of those points to get a formula that will extrapolate the utilization based on the measured data.
Of course, I could be wrong, and the programmer could have just pulled the formula out of thin air.
It looks to me like the whole mess is intended to end up with "total * fl" being close to some predetermined constant, so as to get a standardized test. But I would have done that with one line of division to come as close as possible to that constant, rather than a whole bunch of if-statements which sort of come close to it.
Sometimes I think this forum should be renamed "Questions about XML Parser performance". There seems to be a lot of people obsessing over that.
|
 |
 |
|
|
subject: CPU utilization
|
|
|