add java speed test
authorpmf <pmf@04897980-b3bd-0310-b5e0-8ef037075253>
Thu, 7 Aug 2008 21:33:55 +0000 (21:33 +0000)
committerpmf <pmf@04897980-b3bd-0310-b5e0-8ef037075253>
Thu, 7 Aug 2008 21:33:55 +0000 (21:33 +0000)
git-svn-id: http://ltt.polymtl.ca/svn@2993 04897980-b3bd-0310-b5e0-8ef037075253

trunk/masters-pmf/java_speed_test/basic_record.dat [new file with mode: 0644]
trunk/masters-pmf/java_speed_test/bench/bench.py [new file with mode: 0644]
trunk/masters-pmf/java_speed_test/c/Makefile [new file with mode: 0644]
trunk/masters-pmf/java_speed_test/c/main.c [new file with mode: 0644]
trunk/masters-pmf/java_speed_test/java/Makefile [new file with mode: 0644]
trunk/masters-pmf/java_speed_test/java/read_trace.java [new file with mode: 0644]
trunk/masters-pmf/java_speed_test/make_trace.sh [new file with mode: 0644]

diff --git a/trunk/masters-pmf/java_speed_test/basic_record.dat b/trunk/masters-pmf/java_speed_test/basic_record.dat
new file mode 100644 (file)
index 0000000..93b5f35
Binary files /dev/null and b/trunk/masters-pmf/java_speed_test/basic_record.dat differ
diff --git a/trunk/masters-pmf/java_speed_test/bench/bench.py b/trunk/masters-pmf/java_speed_test/bench/bench.py
new file mode 100644 (file)
index 0000000..b83fd55
--- /dev/null
@@ -0,0 +1,119 @@
+runs=10
+
+class Task:
+       name = "unnamed"
+       pre_cmd = ""
+       cmd = ""
+       post_cmd = ""
+       results = 0
+       remain = 10
+
+       def __init__(self):
+               self.results = []
+
+tasks = []
+
+t1 = Task()
+t1.name = "C version (without print)"
+t1.pre_cmd = ""
+t1.cmd = "pushd ../c >/dev/null; ./main; popd >/dev/null;"
+t1.post_cmd = ""
+tasks.append(t1)
+
+t3 = Task()
+t3.name = "C version (with print)"
+t3.pre_cmd = ""
+t3.cmd = "pushd ../c >/dev/null; ./main -p; popd >/dev/null;"
+t3.post_cmd = ""
+tasks.append(t3)
+
+t5 = Task()
+t5.name = "C version (with print, but sent to /dev/null)"
+t5.pre_cmd = ""
+t5.cmd = "pushd ../c >/dev/null; ./main -p >/dev/null; popd >/dev/null;"
+t5.post_cmd = ""
+tasks.append(t5)
+
+t2 = Task()
+t2.name = "Java version (without print)"
+t2.pre_cmd = ""
+t2.cmd = "pushd ../java >/dev/null; java read_trace; popd >/dev/null;"
+t2.post_cmd = ""
+tasks.append(t2)
+
+t4 = Task()
+t4.name = "Java version (with print)"
+t4.pre_cmd = ""
+t4.cmd = "pushd ../java >/dev/null; java read_trace -p; popd >/dev/null;"
+t4.post_cmd = ""
+tasks.append(t4)
+
+t6 = Task()
+t6.name = "Java version (with print, but sent to /dev/null)"
+t6.pre_cmd = ""
+t6.cmd = "pushd ../java >/dev/null; java read_trace -p >/dev/null; popd >/dev/null;"
+t6.post_cmd = ""
+tasks.append(t6)
+
+import os
+import time
+
+def average(lst):
+       sum = 0
+       count = 0
+
+       for i in lst:
+               sum += i
+               count += 1
+       
+       if count == 0:
+               return 0
+       else:
+               return sum/count
+
+def min(lst):
+       if len(lst) == 0:
+               return 0
+               
+       found = lst[0]
+
+       for i in lst:
+               if i < found:
+                       found = i
+
+       return found
+
+def max(lst):
+       if len(lst) == 0:
+               return 0
+               
+       found = lst[0]
+
+       for i in lst:
+               if i > found:
+                       found = i
+
+       return found
+
+def main():
+       for task in tasks:
+               while task.remain > 0:
+                       os.system(task.pre_cmd)
+                       t_start = time.time()
+                       os.system(task.cmd)
+                       t_end = time.time()
+                       os.system(task.post_cmd)
+                       task.remain-=1
+                       task.results.append(t_end-t_start)
+               print(task.results)
+
+       print "------------------------------------"
+       for task in tasks:
+               print "RESULTS for " + task.name
+               print "Runs: " + str(len(task.results))
+               print "Average: " + str(round(average(task.results), 3))
+               print "Min: " + str(round(min(task.results), 3))
+               print "Max: " + str(round(max(task.results), 3))
+               print ""
+
+main()
diff --git a/trunk/masters-pmf/java_speed_test/c/Makefile b/trunk/masters-pmf/java_speed_test/c/Makefile
new file mode 100644 (file)
index 0000000..f0035b5
--- /dev/null
@@ -0,0 +1,5 @@
+main: main.c
+       gcc -o main -O3 main.c
+
+clean:
+       rm main
diff --git a/trunk/masters-pmf/java_speed_test/c/main.c b/trunk/masters-pmf/java_speed_test/c/main.c
new file mode 100644 (file)
index 0000000..e7bac62
--- /dev/null
@@ -0,0 +1,67 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main()
+{
+       int result;
+       int fd;
+
+       result = fd = open("../trace.dat", O_RDONLY);
+       if(result == -1) {
+               perror("open");
+               return 1;
+       }
+
+       while(1) {
+               unsigned long timestamp;
+               unsigned short id;
+               unsigned char arglen;
+               char *args;
+
+               result = read(fd, &timestamp, 4);
+               if(result == 0)
+                       break;
+               if(result < 4) {
+                       perror("read");
+                       return 1;
+               }
+
+               result = read(fd, &id, 2);
+               if(result < 2) {
+                       perror("read");
+                       return 1;
+               }
+
+               result = read(fd, &arglen, 1);
+               if(result < 1) {
+                       perror("read");
+                       return 1;
+               }
+
+               args = malloc(arglen);
+
+               result = read(fd, args, arglen);
+               if(result < arglen) {
+                       perror("read");
+                       return 1;
+               }
+
+               unsigned short arg1;
+               char *arg2;
+
+               arg1 = *(unsigned short *)args;
+               arg2 = args+2;
+
+               printf("timestamp %u id %hu args=(arg1=%hu arg2=\"%s\")\n", timestamp, id, arg1, arg2);
+
+               free(args);
+       }
+       close(fd);
+
+       return 0;
+}
diff --git a/trunk/masters-pmf/java_speed_test/java/Makefile b/trunk/masters-pmf/java_speed_test/java/Makefile
new file mode 100644 (file)
index 0000000..3bc3f13
--- /dev/null
@@ -0,0 +1,2 @@
+read_trace.class: read_trace.java
+       javac -source 1.5 read_trace.java
diff --git a/trunk/masters-pmf/java_speed_test/java/read_trace.java b/trunk/masters-pmf/java_speed_test/java/read_trace.java
new file mode 100644 (file)
index 0000000..80a3bc1
--- /dev/null
@@ -0,0 +1,53 @@
+import java.io.*;
+
+public class read_trace {
+       public static void main(String[] args) {
+               int tmp = 0;
+
+               try {
+                       FileInputStream file_input = new FileInputStream ("../trace.dat");
+                       DataInputStream data_input = new DataInputStream (file_input);
+                       
+                       int timestamp;
+                       short id;
+                       byte length;
+
+                       while(true) {
+                               timestamp = data_input.readInt();
+                               id = data_input.readShort();
+                               length = data_input.readByte();
+
+                               byte[] buf = new byte[length];
+                               data_input.readFully(buf, 0, length);
+
+
+                               ByteArrayInputStream eargs = new ByteArrayInputStream(buf);
+                               DataInputStream eargs_data = new DataInputStream (eargs);
+
+                               /* read arg 1 (short) */
+                               short arg1 = eargs_data.readShort();
+
+                               /* read arg 2 (string) */
+                               eargs_data.mark(10000);
+                               int strlen=0;
+                               while(eargs_data.readByte() != 0)
+                                       strlen++;
+                               eargs_data.reset();
+                               byte[] arg2 = new byte[strlen];
+                               eargs_data.readFully(arg2, 0, strlen);
+
+                               //System.out.printf("timestamp %d id %d args=(short=%d string=\"%s\") %n", timestamp, id, arg1, new String(arg2));
+                               String strout = String.format("timestamp %d id %d args=(short=%d string=\"%s\") %n", timestamp, id, arg1, new String(arg2));
+                               tmp += strout.length();
+                       }
+
+               }
+               catch(IOException e) {
+                       //System.out.println ("IO exception = " + e );
+                       //e.printStackTrace();
+               }
+
+               //file_input.close();
+               System.out.println(tmp);
+       }
+}
diff --git a/trunk/masters-pmf/java_speed_test/make_trace.sh b/trunk/masters-pmf/java_speed_test/make_trace.sh
new file mode 100644 (file)
index 0000000..53e7217
--- /dev/null
@@ -0,0 +1,2 @@
+(for i in $(seq 1000); do cat basic_record.dat; done) >tmp.dat
+(for i in $(seq 10); do cat tmp.dat; done) >trace.dat
This page took 0.028885 seconds and 4 git commands to generate.