Performance
★Windowsでのツール
※Sysinternals Suite http://technet.microsoft.com/ja-jp/sysinternals/bb842062.aspx
★計測用のツール
★Linuxでのコマンド
ApacheBench
ab -n 1000 -c 50 http://localhost/
JMeter
Visual Studio
Analyze⇒Performance
xampp場合:xampp\apache\bin\ab.exe
-n リクエストの総回数
-c 同時にリクエストの回数
Windowsでの起動:apache-jmeter-2.8\bin\jmeter.bat
※スレッド数 = ユーザ数
※Ultimateバージョン必要
※JMeter http://jakarta.apache.org/jmeter/
★TEST INTERNET SPEED VIA COMMAND LINE
speedtest-cli
インストール方法1
sudo apt-get install python-pip
sudo pip install speedtest-cli
sudo pip install speedtest-cli --upgrade
インストール方法2
sudo wget https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
sudo chmod a+rx speedtest_cli.py
sudo mv speedtest_cli.py /usr/bin/speedtest-cli
★計測用のテストコード
C#
//時間
Stopwatch watch = new Stopwatch();
或いは
Stopwatch watch = Stopwatch.StartNew();
watch.Start();
...
watch.Stop();
Console.WriteLine(watch.Elapsed);
Console.WriteLine(watch.ElapsedMilliseconds);
watch.Reset();
watch.Restart();
...
watch.Stop();
Console.WriteLine(watch.Elapsed);
//メモリ
var start = GC.GetTotalMemory(true);
...
GC.Collect();
GC.WaitForFullGCComplete();
var end = GC.GetTotalMemory(true);
var useMemory = end - start;
Java
long start = System.currentTimeMillis(); // System.nanoTime();
...
long end = System.currentTimeMillis();
out.printf("time: %sms%n", end - start);
import com.google.common.base.Stopwatch;
Stopwatch stopwatch = Stopwatch.createStarted();
...
stopwatch.stop();
out.printf("time: %s%n", stopwatch);
Stopwatch stopwatch = Stopwatch.createStarted();
IntStream.range(1, 50).parallel().map(Test::fib).sum();
out.println("Parallel: " + stopwatch.elapsed(MILLISECONDS) + " ms");
stopwatch.reset();
stopwatch.start();
IntStream.range(1, 50).map(Test::fib).sum();
out.println("Sequential: " + stopwatch.elapsed(MILLISECONDS) + " ms");
private static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
Runtime runtime = Runtime.getRuntime();
runtime.gc();
long freeMemory = runtime.freeMemory();
long startTime = System.currentTimeMillis();
...
out.println("Used memory: " + (freeMemory - runtime.freeMemory()));
out.println("Used time: " + (System.currentTimeMillis() - startTime));
groovy
(1..10).each {
Thread.start {
"curl http://localhost:8080/xxx".execute()
}
}
Node.js
var start_ms = new Date().getTime();
...
var elapsed_ms = new Date().getTime() - start_ms;
console.log('処理時間:' + elapsed_ms);
或いは
var label = '処理時間:';
console.time(label);
...
console.timeEnd(label);
Python
import time
start = time.time()
...
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
★パフォーマンス対策
Component編
1.Buffer
Writer writer = new FileWriter(new File("test.txt"));
↓
Writer writer2 = new BufferedWriter( new FileWriter(new File("test.txt")) );
2.Cache
EHCache(Hibernate)など
3.Pool(thread, connection, object)
C3P0(Hibernate)など
4.Concurrent
5.Load Balance
Design Pattern編
1.Singleton Pattern
重いnew処理の回数を減らす、GC処理も改善
2.Proxy Pattern
遅延ロード
3.Flyweight Pattern
重複のnew処理を避ける、GC処理も改善
4.Decorator Pattern
機能(Bufferなど)を拡張
5.Observer Pattern
マルチスレッドを避ける