当前位置:编程学习 > JAVA >>

求助,性能测试时第一次的时间总是比其他的慢

public class TablePerformanceTest {

private Lattice table;

private void setTable(Lattice table) {
this.table = table;
}



private void performanceTest(List<Filter> setF, List<EndPointConnection> con) {

LogaritmicSequence sequence = new LogaritmicSequence(1, 2000, 9);

int j = 0;
/*
for (Long seq : sequence) {

System.out.print(seq);
this.addTable(setF.subList(j, j + seq.intValue()), con);
this.lesserTable(setF.subList(j, j + seq.intValue()), con);
this.upperTable(setF.subList(j, j + seq.intValue()), con);
this.removeTable(setF.subList(j, j + seq.intValue()), con);
System.out.println();
j = j + seq.intValue();

}
* *
*/
this.addTable(setF.subList(0, 1), con);
}
@Test
public void performance1() {

int sizeTest = 10000;
EndPointConnection con = new DummyEP();
SienaWorkload filerGene = new SienaWorkload();
Set<EndPointConnection> conns = new HashSet<EndPointConnection>(1);
conns.add(con);
List<Filter> setF = filerGene.createFilters(sizeTest, conns).get(con);

//only one conection

System.out.println("performance of Counting table");
this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));


/*
System.out.println("performance of Normal Lattice table");
this.setTable(new TableLattice());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

for(int i = 0;i <18 ; i++)
{
conns.add(new DummyEP());
}

//only twenty conections
System.out.println("performance of Normal Lattice table con 20 conections");
this.setTable(new TableLattice());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));
*/

}
@Test
public void performance() {

int sizeTest = 10000;
EndPointConnection con = new DummyEP();
SienaWorkload filerGene = new SienaWorkload();
Set<EndPointConnection> conns = new HashSet<EndPointConnection>(1);
conns.add(con);
List<Filter> setF = filerGene.createFilters(sizeTest, conns).get(con);

//only one conection

System.out.println("performance of Counting table");
this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));


/*
System.out.println("performance of Normal Lattice table");
this.setTable(new TableLattice());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

for(int i = 0;i <18 ; i++)
{
conns.add(new DummyEP());
}

//only twenty conections
System.out.println("performance of Normal Lattice table con 20 conections");
this.setTable(new TableLattice());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));
*/

}
@Test
public void performance2() {

int sizeTest = 10000;
EndPointConnection con = new DummyEP();
SienaWorkload filerGene = new SienaWorkload();
Set<EndPointConnection> conns = new HashSet<EndPointConnection>(1);
conns.add(con);
List<Filter> setF = filerGene.createFilters(sizeTest, conns).get(con);

//only one conection

System.out.println("performance of Counting table");
this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

this.setTable(new TableCounting());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));


/*
System.out.println("performance of Normal Lattice table");
this.setTable(new TableLattice());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));

for(int i = 0;i <18 ; i++)
{
conns.add(new DummyEP());
}

//only twenty conections
System.out.println("performance of Normal Lattice table con 20 conections");
this.setTable(new TableLattice());
performanceTest(setF, new ArrayList<EndPointConnection>(conns));
*/

}


public void addTable(List<Filter> setF, List<EndPointConnection> con) {

int filSize = setF.size() / con.size();
int sum = setF.size() % con.size();
int size = 0;

for (int i = 0; i < con.size(); i++) {
size = size + filSize;
if (i < sum) {
size++;
}
for (int j = 0; j < size; j++) {
long start = System.nanoTime();
this.table.add(setF.get(j), con.get(i));
long end = System.nanoTime();
long timeNano= end - start;
long us = TimeUnit.NANOSECONDS.toMicros(timeNano);
System.out.print(" add " + us);

}
}





}

private Long removeTable(List<Filter> setF, List<EndPointConnection> con) {

MyRun run = new MyRun(setF, con) {

@Override
protected void doWork(Filter filter,EndPointConnection con) {
table.remove(filter, con);
}
};
Long timeNano = measureTime(run);
long us = TimeUnit.NANOSECONDS.toMicros(timeNano);
System.out.print(" remove " + us);
return timeNano;
}

private Long upperTable(List<Filter> setF, List<EndPointConnection> con) {

MyRun run = new MyRun(setF, con) {

@Override
protected void doWork(Filter filter,EndPointConnection con) {
table.uppers(filter, con);
}
};
Long timeNano = measureTime(run);
long us = TimeUnit.NANOSECONDS.toMicros(timeNano);
System.out.print(" upper " + us);
return timeNano;
}

private Long lesserTable(List<Filter> setF, List<EndPointConnection> con) {

MyRun run = new MyRun(setF, con) {

@Override
protected void doWork(Filter filter,EndPointConnection con) {
table.lessers(filter, con);
}
};
Long timeNano = measureTime(run);
long us = TimeUnit.NANOSECONDS.toMicros(timeNano);
System.out.print(" lesser " + us);
return timeNano;
}

private long measureTime(Runnable run) {

long start = System.nanoTime();
run.run();

long end = System.nanoTime();

return end - start;
}

private static class MyRun implements Runnable {

private List<Filter> filterList;
private List<EndPointConnection> conn;

public MyRun(List<Filter> filterList, List<EndPointConnection> con) {

this.filterList = filterList;
this.conn = con;
}

@Override
public void run() {

int filSize = filterList.size() / conn.size();
int sum = filterList.size() % conn.size();
int size = 0;

for (int i = 0; i < conn.size(); i++) {
size = size + filSize;
if (i < sum) {
size++;
}
for (int j = 0; j < size; j++) {
doWork(filterList.get(j), conn.get(i));
}
}
}

protected void doWork(Filter filter, EndPointConnection conn) {
}
}

}

 add 2234 add 469 add 1274 add 346performance of Counting table
 add 493 add 428 add 414 add 1402performance of Counting table
 add 693 add 1147 add 632 add 1656T

performance of Counting table
 add 165 add 360 add 349 add 332performance of Counting table
 add 521 add 453 add 451 add 547performance of Counting table
 add 256 add 405 add 373 add 385
测试 performance --------------------编程问答-------------------- 你怎么跑的测试。虚拟机是一直开着,还是每次跑都重新开。
如何虚拟机开了后,跑了2次测试。第二次的速度肯定要比第一次的快的。
因为有一些对象的实例已经在jvm里面。 --------------------编程问答--------------------
引用 1 楼 duiduiaa 的回复:
你怎么跑的测试。虚拟机是一直开着,还是每次跑都重新开。
如何虚拟机开了后,跑了2次测试。第二次的速度肯定要比第一次的快的。
因为有一些对象的实例已经在jvm里面。

+++
重新创建对象也是需要时间的…… --------------------编程问答-------------------- 1 虚拟机一直开着 
2 问题在于类的加载 
3 java有没有预先加载所需类的方法
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,