CSE 403 Lecture 18 Performance Testing Reading: Code Complete, Ch. 25-26 (McConnell) slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
Code Tuning Strategies and Techniques
Advertisements

Code Tuning Strategies and Techniques CS524 – Software Engineering Azusa Pacific University Dr. Sheldon X. Liang Mike Rickman.
CSE 303 Lecture 16 Multi-file (larger) programs
Programming Types of Testing.
MCTS GUIDE TO MICROSOFT WINDOWS 7 Chapter 10 Performance Tuning.
Copyright © 2003, SAS Institute Inc. All rights reserved. Where's Waldo Uncovering Hard-to-Find Application Killers Claire Cates SAS Institute, Inc
The Path to Multi-core Tools Paul Petersen. Multi-coreToolsThePathTo 2 Outline Motivation Where are we now What is easy to do next What is missing.
1 Lecture 6 Performance Measurement and Improvement.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
1 TCSS 360, Winter 2005 Lecture Notes Optimization and Profiling.
Chapter 14 Chapter 14: Server Monitoring and Optimization.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
27-Jun-15 Profiling code, Timing Methods. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the.
30-Jun-15 Profiling. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the experts say about.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
Hands-On Microsoft Windows Server 2008 Chapter 11 Server and Network Monitoring.
CH 13 Server and Network Monitoring. Hands-On Microsoft Windows Server Objectives Understand the importance of server monitoring Monitor server.
Page 1 © 2001 Hewlett-Packard Company Tools for Measuring System and Application Performance Introduction GlancePlus Introduction Glance Motif Glance Character.
1 1 Profiling & Optimization David Geldreich (DREAM)
1 CSE 403 Performance Profiling These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed.
CPU PROFILING FIND THE BOTTLENECK. WHAT? WHEN? HOW?
CSE 131 Computer Science 1 Module 1: (basics of Java)
1 TCSS 360, Spring 2005 Lecture Notes Performance Testing: Optimization and Profiling.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
MCTS Guide to Microsoft Windows 7
Java: Chapter 1 Computer Systems Computer Programming II.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Lecture 10 : Introduction to Java Virtual Machine
Ideas to Improve SharePoint Usage 4. What are these 4 Ideas? 1. 7 Steps to check SharePoint Health 2. Avoid common Deployment Mistakes 3. Analyze SharePoint.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Copyright © 2013, SAS Institute Inc. All rights reserved. MEMORY CACHE – PERFORMANCE CONSIDERATIONS CLAIRE CATES DISTINGUISHED DEVELOPER
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
1 Object Oriented Programming Lecture IX Some notes on Java Performance with aspects on execution time and memory consumption.
1 CSE 403 Performance Testing, Profiling, Optimization Reading: McConnell, Code Complete, Ch These lecture slides are copyright (C) Marty Stepp,
Profile and optimize your Java code Gabriel Laden CS 146 – Dr. Sin-Min Lee Spring 2004.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
Debugging and Profiling With some help from Software Carpentry resources.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
CS 3500 L Performance l Code Complete 2 – Chapters 25/26 and Chapter 7 of K&P l Compare today to 44 years ago – The Burroughs B1700 – circa 1974.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Performance Analysis & Code Profiling It’s 2:00AM -- do you know where your program counter is?
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
CS 177 Recitation Week 1 – Intro to Java. Questions?
ITP 109 Week 2 Trina Gregory Introduction to Java.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Profile, HAT, Wireless Toolkit’s Profile Sookmyung Women’s Univ. PSLAB Choi yoonjeong.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Beyond Application Profiling to System Aware Analysis Elena Laskavaia, QNX Bill Graham, QNX.
3 Important Performance tracking tools in an Android Application Development Workflow Here are 3 tools every Android application developer should familiarize.
Debugging with Eclipse
14 Compilers, Interpreters and Debuggers
Debugging Dwight Deugo
Process Management Presented By Aditya Gupta Assistant Professor
Memory leaks in Android. Introduction In our day-to da life many apps crash,and the reason one will point to it is memory leak In majority of the cases.
ColdFusion Performance Troubleshooting and Tuning
Android Topics UI Thread and Limited processing resources
IS 135 Business Programming
Presentation transcript:

CSE 403 Lecture 18 Performance Testing Reading: Code Complete, Ch (McConnell) slides created by Marty Stepp

2 Acceptance, performance acceptance testing: System is shown to the user / client / customer to make sure that it meets their needs. –A form of black-box system testing Performance is important. –Performance is a major aspect of program acceptance by users. –Your intuition about what's slow is often wrong.

3 What's wrong with this? (1) public class BuildBigString { public final static int REPS = 80000; // Builds/returns a big, important string. public static String makeString() { String str = ""; for (int n = 0; n < REPS; n++) { str += "more"; } return str; } public static void main(String[] args) { System.out.println(makeString()); }

4 What's wrong with this? (2) public class Fibonacci { public static void main(String[] args) { // print the first Fibonacci numbers for (int i = 1; i <= 10000; i++) { System.out.println(fib(i)); } // pre: n >= 1 public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 2) + fib(n - 1); }

5 What's wrong with this? (3) public class WordDictionary { // The set of words in our game. List words = new ArrayList (); public void add(String word) { words.add(word.toLowerCase()); } public boolean contains(String word) { for (String s : words) { if (s.toLowerCase().equals(word)) { return true; } return false; }

6 What's wrong with this? (4) public class BankManager { public static void main(String[] args) { Account[] a = Account.getAll(); for (int i = 0; i < Math.sqrt(895732); i++) { a[i].loadTaxData(); if (a.meetsComplexTaxCode(2020)) { a[i].fileTaxes(4 * 4096 * 17); } Account[] a2 = Account.getAll(); for (int i = 0; i < Math.sqrt(895732); i++) { if (a.meetsComplexTaxCode(2020)) { a2[i].setTaxRule(4 * 4096 * 17); a2[i].save(new File(a2.getName())); } } } }

7 The correct answers 1.Who cares? 2.Who cares? 3.Who cares? 4.Who cares? "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." -- Donald Knuth "We follow two rules in the matter of optimization: 1. Don't do it. 2. (for experts only) Don't do it yet." -- M. A. Jackson

8 Thinking about performance The app is only too slow if it doesn't meet your project's stated performance requirements. –If it meets them, DON'T optimize it! Which is more important, fast code or correct code? What are reasonable performance requirements? –What are the user's expectations? How slow is "acceptable" for this portion of the application? –How long do users wait for a web page to load? –Some tasks (admin updates database) can take longer

9 Optimization myths Myth: You should optimize your code as you write it. –No; makes code ugly, possibly incorrect, and not always faster. –Optimize later, only as needed. Myth: Having a fast program is as important as a correct one. –If it doesn't work, it doesn't matter how fast it's running! Myth: Certain operations are inherently faster than others. –x << 1 is faster to compute than x * 2 ? –This depends on many factors, such as language used. Don't write ugly code on the assumption that it will be faster. Myth: A program with fewer lines of code is faster.

10 Perceived performance "My app feels too slow. What should I do?" –possibly optimize it –And/or improve the app's perceived performance perceived performance: User's perception of your app's responsiveness. factors affecting perceived performance: –loading screens –multi-threaded UIs (GUI doesn't stall while something is happening in the background)

11 Optimization metrics runtime / CPU usage –what lines of code the program is spending the most time in –what call/invocation paths were used to get to these lines naturally represented as tree structures memory usage –what kinds of objects are on the heap –where were they allocated –who is pointing to them now –"memory leaks" (does Java have these?) web page load times, requests/minute, etc.

12 Benchmarking, optimization benchmarking: Measuring the absolute performance of your app on a particular platform (coarse-grained measurement). optimization: Refactoring and enhancing to speed up code. –I/O routines accessing the console (print statements) files, network access, database queries exec() / system calls –Lazy evaluation saves you from computing/loading don't read / compute things until you need them –Hashing, caching save you from reloading resources combine multiple database queries into one query save I/O / query results in memory for later

13 Optimizing memory access Non-contiguous memory access (bad): for (int col = 0; col < NUM_COLS; col++) { for (int row = 0; row < NUM_ROWS; row++) { table[row][column] = bulkyMethodCall(); } } Contiguous memory access (good): for (int row = 0; row < NUM_ROWS; row++) { for (int col = 0; col < NUM_COLS; col++) { table[row][column] = bulkyMethodCall(); } } –switches rows NUM_ROWS times, not NUM_ROWS * NUM_COLS

14 Optimizing data structures Take advantage of hash-based data structures –searching an ArrayList (contains, indexOf) is O(N) –searching a HashMap/HashSet is O(1) Getting around limitations of hash data structures –need to keep elements in sorted order?Use TreeMap/TreeSet –need to keep elements in insertion order?Use LinkedHashSet

15 Avoiding computations Stop computing when you know the answer: found = false; for (i = 0; i < reallyBigNumber; i++) { if (inputs[i].isTheOneIWant()) { found = true; break; } Hoist expensive loop-invariant code outside the loop: double taxThreshold = reallySlowTaxFunction(); for (i = 0; i < reallyBigNumber; i++) { accounts[i].applyTax(taxThreshold); }

16 Lookup tables Figuring out the number of days in a month: if (m == 9 || m == 4 || m == 6 || m == 11) { return 30; } else if (month == 2) { return 28; } else { return 31; } Days in a month, using a lookup table: DAYS_PER_MONTH = {-1, 31, 28, 31, 30, 31, 30,..., 31};... return DAYS_PER_MONTH[month]; –Probably not worth the speedup with this particular example...

17 Optimization is deceptive int sum = 0; for (int row = 0; row < NUM_ROWS; row++) { for (int col = 0; col < NUM_COLS; col++) { sum += matrix[row][column]; } Optimized code: int sum = 0; Cell* p = matrix; Cell* end = &matrix[NUM_ROWS - 1][NUM_COLS - 1]; while (p != end) { sum += *p++; } Speed-up observed: NONE. –Compiler was already optimizing the original into the second!

18 Dynamic programming public static boolean isPrime(int n) { double sqrt = Math.sqrt(n); for (int i = 2; i <= sqrt; i++) if (n % i == 0) { return false; } return true; } dynamic programming: Caching previous results. private static Map PRIME =...; public static boolean isPrime2(int n) { if (!PRIME.containsKey(n)) PRIME.put(n, isPrime(n)); return PRIME.get(n); }

19 Optimization tips Pareto Principle, aka the "80-20 Rule" –80% of a program's execution occurs within 20% of its code. –You can get 80% results with 20% of the work. "The best is the enemy of the good." –You don't need to optimize all your app's code. –Find the worst bottlenecks and fix them. Leave the rest.

20 Profiling profiling: Measuring relative system statistics (fine-grained). –Where is the most time being spent? ("classical" profiling) Which method takes the most time? Which method is called the most? –How is memory being used? What kind of objects are being created? This in especially applicable in OO, GCed environments. –Profiling is not the same as benchmarking or optimizing.

21 Types of profiling insertion: placing special profiling code into your program (manually or automatically) –pros: can be used across a variety of platforms; accurate –cons: requires recompiling; profiling code may affect performance sampling: monitoring CPU or VM at regular intervals and saving a snapshot of CPU and/or memory state –pros: no modification of app is necessary –cons: less accurate; varying sample interval leads to a time/accuracy trade-off; small methods may be missed; cannot easily monitor memory usage

22 Android Traceview Traceview: – –Debug class generates *.trace files to be viewed Debug.startMethodTracing();... Debug.stopMethodTracing(); –timeline panel: describes when each thread/method start/stops –profile panel: summary of what happened inside a method

23 Android profiling DDMS Dalvik Debug Monitor Server (DDMS): – Eclipse: Window  Open Perspective  Other...  DDMS console: run ddms from tools/ directory –On Devices tab, select process that you want to profile Click Start Method Profiling Interact with application to run and profile its code.

24 Java profiling tools Many free Java profiling/optimization tools available: –TPTP profiler extension for Eclipse –Extensible Java Profiler (EJP) - open source, CPU tracing only –Eclipse Profiler plugin –Java Memory Profiler (JMP) –Mike's Java Profiler (MJP) –JProbe Profiler - uses an instrumented VM hprof ( java -Xrunhprof ) –comes with JDK from Sun, free –good enough for anything I've ever needed

25 Using hprof usage: java -Xrunhprof:[help]|[ =,...] Option Name and Value Description Default heap=dump|sites|all heap profiling all cpu=samples|times|old CPU usage off monitor=y|n monitor contention n format=a|b text(txt) or binary output a file= write data to file off depth= stack trace depth 4 interval= sample interval in ms 10 cutoff= output cutoff point lineno=y|n line number in traces? Y thread=y|n thread in traces? N doe=y|n dump on exit? Y msa=y|n Solaris micro state accounting n force=y|n force output to y verbose=y|n print messages about dumps y

26 Sample hprof usage java -Xrunhprof:cpu=samples,depth=6,heap=sites or java -Xrunhprof:cpu=old,thread=y,depth=10,cutoff=0,format=a ClassName –Takes samples of CPU execution –Record call traces that include the last 6/10 levels on the stack –Only record "sites" used on heap (to keep output file small) java -Xrunhprof ClassName –Takes samples of memory/object usage After execution, open the text file java.hprof.txt in the current directory with a text editor

27 hprof visualization tools CPU samples –critical to see traces to modify code –hard to read - far from the traces in the file –HPjmeter analyzes java.hprof.txt visually –another good tool called PerfAnal builds and navigates the invocation tree –download PerfAnal.jar, and: java -jar PerfAnal.jar./java.hprof.txt Heap dump –critical to see what objects are there, and who points to them –HPjmeter or HAT:

28 TPTP a free extension to Eclipse for Java profiling –easier to interpret than raw hprof results –has add-ons for profiling web applications (J2EE)

29 Profiler results What to do with profiler results: –observe which methods are being called the most These may not necessarily be the "slowest" methods! –observe which methods are taking most time relative to others Warnings –CPU profiling slows down your code (a lot) design your profiling tests to be very short –CPU samples don't measure everything doesn't record object creation and garbage collection time –Output files are very large, esp. if there is a heap dump

30 Garbage collection garbage collector: A memory manager that reclaims objects that are not reachable from a root-set root set: all objects with an immediate reference –all reference variables in each frame of every thread's stack –all static reference fields in all loaded classes Size Heap Size Time GC Total size of reachable objects Total size of allocated objects

31 Profiling Web languages HTML/CSS –YSlow: JavaScript –Firebug: Ruby on Rails –ruby-prof: –ruby-prof --printer=graph_html --file=myoutput.html myscript.rb JSP –x.Link: PHP –Xdebug:

32 JavaScript optimization JavaScript is ~1000x slower than C code. Modifying a page using the DOM can be expensive. var ul = document.getElementById("myUL"); for (var i = 0; i < 2000; i++) { ul.appendChild(document.createElement("li")); } –Faster code that modifies DOM objects "offline": var ul = document.getElementById("myUL"); var li = document.createElement("li"); var parent = ul.parentNode; parent.removeChild(ul); for (var i = 0; i < 2000; i++) { ul.appendChild(li.cloneNode(true)); } parent.appendChild(ul);