测试了几种file copy的性能:
package mytest;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import org.apache.commons.io.IOUtils;
import com.taobao.hsf.tlog.proxy.appender.LogAppender;
import com.taobao.hsf.tlog.proxy.appender.impl.RollingFileAppender;
public class MyTest {
static BufferedReader read() throws FileNotFoundException {
InputStream input = new FileInputStream(
"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/input");
InputStreamReader reader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(reader);
return bufferedReader;
}
static BufferedInputStream input() throws FileNotFoundException {
InputStream input = new FileInputStream(
"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/input");
return new BufferedInputStream(input);
}
static FileInputStream inputFile() throws FileNotFoundException {
FileInputStream input = new FileInputStream(
"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/input");
return input;
}
static void test1() throws IOException {
BufferedReader bufferedReader = read();
long begin = System.currentTimeMillis();
FileOutputStream ostream = new FileOutputStream(
"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output1");
BufferedOutputStream bos = new BufferedOutputStream(ostream, 8192);
String line;
while ((line = bufferedReader.readLine()) != null) {
line += "\n";
bos.write(line.getBytes("utf-8"));
}
bos.flush();
long end = System.currentTimeMillis();
System.out.println("buffered stream cost: " + (end - begin) + " ms");
bufferedReader.close();
}
static void test2() throws IOException {
BufferedReader bufferedReader = read();
long begin = System.currentTimeMillis();
String line;
LogAppender newAppender = new RollingFileAppender(
"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output2", 1024 * 1024 * 100, false);
while ((line = bufferedReader.readLine()) != null) {
line += "\n";
newAppender.append(line);
}
newAppender.flush();
long end = System.currentTimeMillis();
System.out.println("RollingFileAppender cost: " + (end - begin) + " ms");
bufferedReader.close();
}
static void test3() throws IOException {
BufferedReader bufferedReader = read();
long begin = System.currentTimeMillis();
String line;
while ((line = bufferedReader.readLine()) != null) {
line += "\n";
}
long end = System.currentTimeMillis();
System.out.println("only read cost: " + (end - begin) + " ms");
}
static void test4() throws IOException {
BufferedReader bufferedReader = read();
long begin = System.currentTimeMillis();
FileOutputStream ostream = new FileOutputStream(
"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output3");
IOUtils.copy(bufferedReader, ostream);
long end = System.currentTimeMillis();
System.out.println("a.c.o.1 : " + (end - begin) + " ms");
}
static void test5() throws IOException {
InputStream bufferedReader = input();
long begin = System.currentTimeMillis();
FileOutputStream ostream = new FileOutputStream(
"/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output4");
IOUtils.copy(bufferedReader, ostream);
long end = System.currentTimeMillis();
System.out.println("a.c.o.2 : " + (end - begin) + " ms");
}
static void test6() throws IOException {
FileChannel input = inputFile().getChannel();
long begin = System.currentTimeMillis();
FileChannel out = new
FileOutputStream("/home/yunpeng/logs/proxy/10.125.48.147/home/yunpeng.jiangyp/logs/output5")
.getChannel();
input.transferTo(0, input.size(), out);
long end = System.currentTimeMillis();
System.out.println("chanel transfer : " + (end - begin) + " ms");
input.close();
out.close();
}
public static void main(String[] args) throws IOException {
for (int i = 0; i < 100; ++i) {
test1();
test2();
test3();
test4();
test5();
test6();
System.out.println("-------------------------");
}
}
}
测试结果:
写道
-------------------------
buffered stream cost: 110 ms
RollingFileAppender cost: 134 ms
only read cost: 54 ms
a.c.o.1 : 85 ms
a.c.o.2 : 55 ms
chanel transfer : 10 ms
-------------------------
buffered stream cost: 131 ms
RollingFileAppender cost: 137 ms
only read cost: 55 ms
a.c.o.1 : 175 ms
a.c.o.2 : 21 ms
chanel transfer : 10 ms
-------------------------
buffered stream cost: 121 ms
RollingFileAppender cost: 122 ms
only read cost: 56 ms
a.c.o.1 : 161 ms
a.c.o.2 : 31 ms
chanel transfer : 14 ms
-------------------------
buffered stream cost: 121 ms
RollingFileAppender cost: 120 ms
only read cost: 55 ms
a.c.o.1 : 89 ms
buffered stream cost: 110 ms
RollingFileAppender cost: 134 ms
only read cost: 54 ms
a.c.o.1 : 85 ms
a.c.o.2 : 55 ms
chanel transfer : 10 ms
-------------------------
buffered stream cost: 131 ms
RollingFileAppender cost: 137 ms
only read cost: 55 ms
a.c.o.1 : 175 ms
a.c.o.2 : 21 ms
chanel transfer : 10 ms
-------------------------
buffered stream cost: 121 ms
RollingFileAppender cost: 122 ms
only read cost: 56 ms
a.c.o.1 : 161 ms
a.c.o.2 : 31 ms
chanel transfer : 14 ms
-------------------------
buffered stream cost: 121 ms
RollingFileAppender cost: 120 ms
only read cost: 55 ms
a.c.o.1 : 89 ms