假設我在Server端有以下的Code
HashMap<String,String> map=new HashMap<String,String> ();
ObjectOutputStream oos=new ObjectOutputStream(clientsocket.getOutputStream);
while(true){
map.put(Long.toString(System.currentTimeMillis()),Long.toString(System.currentTimeMillis()));
oos.writeObject(map);
}
另外一端收到的會發現後面收到的物件都跟第一次收到的物件一樣,Why?因為在送出的時候都是指向同一個位址,因此必須要加上oos.reset();結果才會正確。
2011年2月25日 星期五
2011年2月7日 星期一
File Transfer via Network using C vs. JAVA
這次的測試分別比較了使用gcc /gcc -O2 編譯而成的與javac 1.6.0_23 的版本編譯而成的執行檔來做比較,測試的兩台機器是在Lan環境下。測試的設備與程式碼如下:
測試程式如下:
C (copy from http://content.edu.tw/primary/info_edu/cy_sa/report/more/8611a8.htm)
server.c
server.java
client.java
很詭異的結果,再研究看看。
CPU | Intel(R) Core(TM) i7 CPU 950 @ 3.07GHz |
MEMERY | 6G |
NETWORK | 1G |
TEST FILE | 466819755 byte (tar.bz2 file) |
測試程式如下:
C (copy from http://content.edu.tw/primary/info_edu/cy_sa/report/more/8611a8.htm)
server.c
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #define TRUE 1 main() { int sock, msgsock, length; struct sockaddr_in name; FILE *fp; int rval,wrval; char buf[1024],filename[1024]; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("opening stream socket"); exit(1); } /*-------------------------------------------------------*/ /* Create the acceptable connection host with Wildcard */ /*-------------------------------------------------------*/ name.sin_family = AF_INET; name.sin_addr.s_addr = INADDR_ANY; name.sin_port = 5000; if (bind(sock, (struct sockaddr *)&name, sizeof name) < 0) { perror ("binding stream socket"); exit(1); } listen(sock,5); do{ msgsock = accept(sock,(struct sockaddr *)0, (int *)0); if (msgsock == -1) perror("accept error !\\n "); else do { bzero(buf, sizeof(buf) ); /*--------------------------------------------------*/ /* Read filename from msgsock (sent from client) */ /*--------------------------------------------------*/ if ((rval = read(msgsock, buf,1024)) < 0) perror("reading stream message"); if (rval == 0) printf("Ending connection\\n"); else { if ((fp=fopen(buf,"r")) == NULL) { printf("file not found : %s !\\n ",buf); break; } printf("opening [%s] !!\\n ",buf); bzero(filename,sizeof(filename)); strcpy(filename, buf); /*--------------------------------------------------*/ /* Read file content & sent to msgsock ...> client */ /*--------------------------------------------------*/ while (fgets(buf,sizeof(buf),fp) != (char*)NULL) { if ((wrval=write(msgsock,buf,sizeof(buf))) <0) break; //printf("."); } fclose(fp); printf("\\n\\n closing [%s] ! \\n",filename); rval=0; } }while (rval != 0); close(msgsock); printf(" [%s]finish* !! \\n ",filename); }while (TRUE); close(sock); exit(0); }client.c
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #define DATA "The sea is clam ...." main(int argc, char *argv[] ) { int sock, rval, wrval; char buf[1024]; struct sockaddr_in server; struct hostent *hp, *gethostbyname(); FILE *fp; sock = socket(AF_INET,SOCK_STREAM,0); if (sock < 0) { perror("opening stream socket"); exit(1); } server.sin_family=AF_INET; hp=gethostbyname(argv[1]); if (hp == 0 ) { printf( "%s : unknown host\\n", argv[1]); exit(2); } bcopy((char*)hp->h_addr, (char *)&server.sin_addr, hp->h_length); server.sin_port = 5000; if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) { perror ("connecting stream socket"); exit(1); } if (write(sock, argv[3],strlen(argv[3])) < 0) perror ("writing stream socket"); sleep(1); printf("requiring [%s], ...\\n",argv[3]); fp=fopen(argv[4],"w+"); /*---------------------------------------------------------*/ /* Read file content from sock (sent from ftp_serv ) */ /* and Write to local file */ /*---------------------------------------------------------*/ while ( (rval=read(sock,buf,sizeof(buf) )) >1) { //printf("."); fputs(buf,fp); } fclose(fp); close(sock); printf("finish !! \\n"); printf("closing connection !! \\n"); exit(0); }JAVA
server.java
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class server { public static void main(String[] args) throws IOException { ServerSocket servsock = new ServerSocket(5000); File myFile = new File("hadoop.tar.bz2"); while (true) { Socket sock = servsock.accept(); byte[] mybytearray = new byte[(int) myFile.length()]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); bis.read(mybytearray, 0, mybytearray.length); OutputStream os = sock.getOutputStream(); os.write(mybytearray, 0, mybytearray.length); os.flush(); sock.close(); } } }
client.java
import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.Socket; public class client { public static void main(String[] argv) throws Exception { Socket sock = new Socket("192.168.254.1", 5000); byte[] mybytearray = new byte[1024]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("hadoop.tar.bz2"); BufferedOutputStream bos = new BufferedOutputStream(fos); int bytesRead ; while((bytesRead= is.read(mybytearray, 0, mybytearray.length))!=-1){ bos.write(mybytearray, 0, bytesRead); } bos.close(); sock.close(); } }測試結果如下:
Test Case | Time(s) |
C with -O2 | 20.539(s) |
C without -O2 | 20.286(s) |
JAVA | 4.923(s) |
2011年1月28日 星期五
Java的List, Set, Map比較
首先我主要比較了
List: ArrayList, LinkedList
Set: HashSet, TreeSet
Map: HashMap, TreeMap
並且針對了這幾個新增刪除以及取得(Get)來做比較,測試方式是先新增10000個字串物件,然後刪除字串為"5000”的物件,再來則是利用while loop及foreach來展開全部的物件。
實驗環境為
以下為數據結果:
程式碼如下
List: ArrayList, LinkedList
Set: HashSet, TreeSet
Map: HashMap, TreeMap
並且針對了這幾個新增刪除以及取得(Get)來做比較,測試方式是先新增10000個字串物件,然後刪除字串為"5000”的物件,再來則是利用while loop及foreach來展開全部的物件。
實驗環境為
CPU | Intel core i7 860 @2.80GHz |
OS | WIndows 7 X86_64 |
MEM | 6G |
JDK | Java SE 6 Update 23 |
以下為數據結果:
add(push) | delete(remove) | Get(foreach) | Get (while) | |
ArrayList | 10 | 1 | 2 | 4 |
LinkedList | 4 | 1 | 1 | 4 |
HashSet | 13 | 0 | 2 | 5 |
TreeSet | 38 | 0 | 4 | 6 |
HashMap | 3 | 0 | N/A | 4 |
TreeMap | 7 | 0 | N/A | 4 |
程式碼如下
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.TreeSet; public class PerformanceTest { private static void ArrayListTest(int size){ System.out.println("\nArrayListTest, Total size: "+size); ArrayList<String> list=new ArrayList<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void LinkedListTest(int size){ System.out.println("\nLinkedListTest, Total size: "+size); LinkedList<String> list=new LinkedList<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void HashSetTest(int size){ System.out.println("\nHashSetTest, Total size: "+size); HashSet<String> list=new HashSet<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void TreeSetTest(int size){ System.out.println("\nTreeSetTest, Total size: "+size); TreeSet<String> list=new TreeSet<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void HashMapTest(int size){ System.out.println("\nHashMapTest, Total size: "+size); HashMap<String,String> list=new HashMap<String,String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.put(Integer.toString(i),Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Collection collection = list.values(); Iterator iterator = collection.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void TreeMapTest(int size){ System.out.println("\nTreeMapTest, Total size: "+size); TreeMap<String,String> list=new TreeMap<String,String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.put(Integer.toString(i),Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Collection collection = list.values(); Iterator iterator = collection.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); } public static void main(String[] args) { ArrayListTest(10000); LinkedListTest(10000); HashSetTest(10000); TreeSetTest(10000); HashMapTest(10000); TreeMapTest(10000); } }
2011年1月13日 星期四
HDFS Performance Test
Ubuntu Software RAID 5
之前拿四顆硬碟分別是 Seagate的2T*1 1.5T*3,簡單測一下效能:
A.對照組:在其中一個1.5T的硬碟切一個266G的磁區格式化為EXT4
B.實驗組:把剩下的空間作成RAID5 所以有5.7T也是格式化為EXT4
實驗方式是以dd的指令來產生檔案,bs為512K,連續跑四次扣除最快和最慢的取平均。
1K B 512KB 1MB 10MB 100MB 1GB 10GB
A 48.9MB/s 700.5MB/s 822.5/MB/s 1091MB/s 1700MB/s 45MB/s 46MB/s
B 22.2 MB/s 553 MB/s 787MB/s 1331MB/s 1700MB/s 363MB/s 120MB/s
簡單的結論: Softraid 5 在檔案超過10MB後寫入效能會比單顆硬碟好。
A.對照組:在其中一個1.5T的硬碟切一個266G的磁區格式化為EXT4
B.實驗組:把剩下的空間作成RAID5 所以有5.7T也是格式化為EXT4
實驗方式是以dd的指令來產生檔案,bs為512K,連續跑四次扣除最快和最慢的取平均。
1K B 512KB 1MB 10MB 100MB 1GB 10GB
A 48.9MB/s 700.5MB/s 822.5/MB/s 1091MB/s 1700MB/s 45MB/s 46MB/s
B 22.2 MB/s 553 MB/s 787MB/s 1331MB/s 1700MB/s 363MB/s 120MB/s
簡單的結論: Softraid 5 在檔案超過10MB後寫入效能會比單顆硬碟好。
2011年1月5日 星期三
在Ubuntu解決使用lftp亂碼問題
lftp是一個很好用的工具,特別是對於我這個重度ftp的人!然而,有些人就是很喜歡使用Windows來架ftp偏偏又不支援utf-8.....結果lftp就亂碼了@@
解法是在登入後輸入:
解法是在登入後輸入:
set ftp:charset "big5"; set file:charset "utf-8"
就可以正確地顯示編碼了!
另外,介紹一下lftp:
登入: lftp user:pwd@site:port or lftp –u user site:port
我常用的指令除了cd 之外就是…mirror了!
mirror [-c 續傳] [-R 把本地mirror上去][-n 只下載新的檔案][-parallel=n 同時傳輸n個檔案] [remote 資料夾] [本地資料夾]
2011年1月4日 星期二
C2DM on Android 2.2
當測試C2DM的時候在Logcat看到這一行"emulatorUnable to start service Intent { act=com.google.android.c2dm.intent.REGISTER (has extras) }: not found"
解法是你的Android Emulator必須是使用Google APIs Add-On的版本!
解法是你的Android Emulator必須是使用Google APIs Add-On的版本!
訂閱:
文章 (Atom)