Effective Java Programming: Measurement As The Basis
Effective Java Programming: Measurement As The Basis
Programming
measurement as the basis
Structure
measurement as the basis
benchmarking
micro
macro
profiling
why you should do this?
profiling tools
Motto
"We should forget about small efficiencies,
say about 97% of the time: premature
optimization is the root of all evil."
Donald Knuth
The problem
Modern software is too complex
Even the wisest man is not suitable for
performance tuning
need for techniques and tools!
benchmarking
profiling
What is benchmarking?
it is a comparison of different solutions on
the basis of measurements (time, memory,
...)
different solutions, but giving the same
results
different data structures
different technologies (ORM, MVC)
various containers
the essence is to compare
single result does not say anything
results are useful when you can compare them
How to measure?
stopwatch
funny, but often appropriate
the most versatile technique
no interference with the code
does not require complicated tools
you do not always need the milliseconds
application start time
time to open a document
time scrolling through large tables
DB query execution time
How to measure?
measurements taken into account in the
code
calculation based on system time before and after
run
System.currentTimeMillis ()
precision in milliseconds (?)
interference with the code - can lead to failure
addition to each method is tedious
generic stopwatch class
Micro-benchmark
checks for a specific part of the system
often even a few lines of code, for example:
Draw a rectangle 100 000, read 10MB file from disk
Sorting an array of 100 000 elements, performance
of specific tasks
good
quick execution, focus on the problem
bad - does not represent the behavior of a
real application
JVM "warm up"
unrealistic interactions in the system
Macro-benchmark
Benchmark analysis
background processes
network traffic
GC
What is profiling?
RAM
CPU
network
Why profile?
premature optimization
optimization of the bad parts of the system
too thorough optimization
Method
Time [ms]
Method
Time [ms]
m1
m1
30
m2
m2
24
m3
m3
10
m4
m4
different views
sophisticated statistics
Problems
heap
for arrays and objects
error message: Java heap space
when: during class instantiation
non-heap
stack
for addresses, variables, class definitions and strings
error message: PermGen space
when: eg. loading class
native
managed by OS, used by native methods
error message: request <size> bytes for <reason>. Out of
swap space?
when: using native libraries
Problems
memory leaks
finalization
deadlock
synchronization is faulty
2 or more threads are simultaneously waiting for monitor
release
Problems
looped threads
identify bottlenecks
which methods cause most load?
Tools
Tools
Tools
bin\jvisualvm.exe
since Java 6 update 7
much better than jconsole
GUI
track many JVM at once
remote JVM tracking
profiling options
Profiling on Eclipse
TPTP
profiles
Eclipse plugins
local Java applications
complex applications running on multiple
machines and different platforms
Conclusions
what is benchmarking?
what is a benchmark?
how differs macro from micro benchmark?
what is profiling?
what are the problems with flat profiles?