pluotsorbet/bench/JITBenchmark.java

193 строки
4.1 KiB
Java
Исходник Обычный вид История

2015-01-30 02:06:20 +03:00
import java.lang.Object;
import java.lang.System;
2015-01-31 10:30:10 +03:00
import java.util.Hashtable;
2015-01-30 02:06:20 +03:00
import java.util.Vector;
import java.io.ByteArrayOutputStream;
import org.mozilla.internal.Sys;
2015-02-17 21:02:05 +03:00
import com.sun.cldchi.jvm.JVM;
2015-01-30 02:06:20 +03:00
class JITBenchmark {
2015-02-03 12:56:19 +03:00
static class A {}
static class B extends A {}
public static int size = 0;
public static long start = 0;
2015-01-30 02:06:20 +03:00
public static void createObjectArrays() {
Object array = null;
int count = size / 8;
for (int i = 0; i < count; i++) {
2015-01-30 02:06:20 +03:00
array = new Object[64];
array = new Object[128];
array = new Object[256];
array = new Object[512];
}
}
public static void createPrimitiveArrays() {
Object array = null;
int count = size / 8;
for (int i = 0; i < count; i++) {
2015-01-30 02:06:20 +03:00
array = new int[64];
array = new int[128];
array = new int[256];
array = new int[512];
}
}
public static void writeByteArrayOutputStream() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int count = size * 16;
for (int i = 0; i < count; i++) {
2015-01-30 02:06:20 +03:00
stream.write(i);
stream.write(i);
stream.write(i);
stream.write(i);
}
}
2015-01-31 10:30:10 +03:00
public static void concatStrings() {
String s = "";
int count = size / 16;
for (int i = 0; i < count; i++) {
s += "X";
}
}
public static void getBytes() {
String s = "getBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytesgetBytes";
int count = size / 4;
byte [] bytes = null;
for (int i = 0; i < count; i++) {
bytes = s.getBytes();
}
}
public static void synch() {
int count = size * 32;
2015-01-31 10:30:10 +03:00
byte [] bytes = null;
Object o = new Object();
int sum = 0;
for (int i = 0; i < count; i++) {
synchronized (o) {
sum++;
}
synchronized (o) {
sum++;
}
synchronized (o) {
sum++;
}
synchronized (o) {
sum++;
}
synchronized (o) {
sum++;
}
synchronized (o) {
sum++;
}
}
}
public static void hashtable() {
int count = size * 2;
Hashtable hash = new Hashtable();
Object o = new Object();
String [] names = {
"hello", "world", "hello1", "world2", "hello3", "world4", "hello5", "world6"
};
for (int i = 0; i < count; i++) {
String name = names[i % names.length];
hash.put(name, o);
hash.get(name);
hash.put(name, o);
hash.get(name);
hash.put(name, o);
hash.get(name);
hash.put(name, o);
hash.get(name);
}
}
2015-02-03 12:56:19 +03:00
public static void arrayTypeCheck() {
A [] array = new A [1024];
A a = new A();
B b = new B();
int count = size * 3;
2015-02-03 12:56:19 +03:00
for (int i = 0; i < count - 1; i++) {
array[i % 1024] = a;
array[i % 1024] = b;
}
}
public static void begin() {
System.gc();
2015-02-17 21:02:05 +03:00
start = JVM.monotonicTimeMillis();
}
public static void finish(String name) {
2015-02-17 21:02:05 +03:00
System.out.println(name + ": " + (JVM.monotonicTimeMillis() - start));
}
2015-01-30 02:06:20 +03:00
public static void main(String[] args) {
// Sys.eval("J2ME.emitCheckArrayStore = false;");
// Sys.eval("J2ME.emitCheckArrayBounds = false;");
size = 1024;
begin();
2015-02-17 21:02:05 +03:00
start = JVM.monotonicTimeMillis();
2015-01-31 10:30:10 +03:00
createObjectArrays();
createPrimitiveArrays();
writeByteArrayOutputStream();
concatStrings();
getBytes();
synch();
hashtable();
arrayTypeCheck();
finish("startup");
2015-01-30 02:06:20 +03:00
size = 1024 * 256;
2015-02-17 21:02:05 +03:00
long start = JVM.monotonicTimeMillis();
begin();
createObjectArrays();
finish("createObjectArrays");
begin();
2015-01-31 10:30:10 +03:00
createPrimitiveArrays();
finish("createPrimitiveArrays");
2015-01-30 02:06:20 +03:00
begin();
2015-01-30 02:06:20 +03:00
writeByteArrayOutputStream();
finish("writeByteArrayOutputStream");
2015-01-31 10:30:10 +03:00
begin();
2015-01-31 10:30:10 +03:00
concatStrings();
finish("concatStrings");
2015-01-31 10:30:10 +03:00
begin();
2015-01-31 10:30:10 +03:00
getBytes();
finish("getBytes");
2015-01-31 10:30:10 +03:00
begin();
2015-01-31 10:30:10 +03:00
synch();
finish("synchronize");
2015-01-31 10:30:10 +03:00
begin();
2015-01-31 10:30:10 +03:00
hashtable();
finish("hashtable");
2015-02-03 12:56:19 +03:00
begin();
2015-02-03 12:56:19 +03:00
arrayTypeCheck();
finish("arrayTypeCheck");
System.out.println();
2015-02-17 21:02:05 +03:00
System.out.println("Total: " + (JVM.monotonicTimeMillis() - start));
2015-01-30 02:06:20 +03:00
}
}