#175999: Adding sampling to GoToType when it took more than 3s sampling-profile-175999
authorPavel Flaska <pflaska@netbeans.org>
Fri, 20 Nov 2009 12:29:08 +0100
branchsampling-profile-175999
changeset 154393efd62a072dae
parent 153959 9a3d2b7733d9
child 154394 8fa7210452c2
#175999: Adding sampling to GoToType when it took more than 3s
jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java
     1.1 --- a/jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java	Fri Nov 20 07:50:23 2009 +0100
     1.2 +++ b/jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java	Fri Nov 20 12:29:08 2009 +0100
     1.3 @@ -56,6 +56,8 @@
     1.4  import java.awt.event.ActionListener;
     1.5  import java.awt.event.WindowAdapter;
     1.6  import java.awt.event.WindowEvent;
     1.7 +import java.io.ByteArrayOutputStream;
     1.8 +import java.io.DataOutputStream;
     1.9  import java.io.IOException;
    1.10  import java.util.ArrayList;
    1.11  import java.util.Arrays;
    1.12 @@ -63,9 +65,11 @@
    1.13  import java.util.Collections;
    1.14  import java.util.Comparator;
    1.15  import java.util.List;
    1.16 +import java.util.logging.Level;
    1.17  import java.util.logging.Logger;
    1.18  import java.util.regex.Pattern;
    1.19  import javax.swing.AbstractAction;
    1.20 +import javax.swing.Action;
    1.21  import javax.swing.DefaultListModel;
    1.22  import javax.swing.JButton;
    1.23  import javax.swing.ListCellRenderer;
    1.24 @@ -90,6 +94,7 @@
    1.25  import org.openide.filesystems.FileObject;
    1.26  import org.openide.filesystems.FileUtil;
    1.27  import org.openide.nodes.Node;
    1.28 +import org.openide.util.Exceptions;
    1.29  import org.openide.util.HelpCtx;
    1.30  import org.openide.util.ImageUtilities;
    1.31  import org.openide.util.Lookup;
    1.32 @@ -389,40 +394,48 @@
    1.33              this.text = text;
    1.34              this.createTime = System.currentTimeMillis();
    1.35              LOGGER.fine( "Worker for " + text + " - created after " + ( System.currentTimeMillis() - panel.time ) + " ms."  );                
    1.36 -       }
    1.37 -        
    1.38 +        }
    1.39 +
    1.40          public void run() {
    1.41 -            
    1.42 -            LOGGER.fine( "Worker for " + text + " - started " + ( System.currentTimeMillis() - createTime ) + " ms."  );                
    1.43 -            
    1.44 -            final List<? extends TypeDescriptor> types = getTypeNames( text );
    1.45 -            if ( isCanceled ) {
    1.46 -                LOGGER.fine( "Worker for " + text + " exited after cancel " + ( System.currentTimeMillis() - createTime ) + " ms."  );                                
    1.47 -                return;
    1.48 +            Profile profile = initializeProfiling();
    1.49 +            try {
    1.50 +                LOGGER.fine( "Worker for " + text + " - started " + ( System.currentTimeMillis() - createTime ) + " ms."  );
    1.51 +
    1.52 +                final List<? extends TypeDescriptor> types = getTypeNames( text );
    1.53 +                if ( isCanceled ) {
    1.54 +                    LOGGER.fine( "Worker for " + text + " exited after cancel " + ( System.currentTimeMillis() - createTime ) + " ms."  );
    1.55 +                    return;
    1.56 +                }
    1.57 +                ListModel model = Models.fromList(types);
    1.58 +                if (typeFilter != null) {
    1.59 +                    model = LazyListModel.create(model, GoToTypeAction.this, 0.1, "Not computed yet");
    1.60 +                }
    1.61 +                final ListModel fmodel = model;
    1.62 +                if ( isCanceled ) {
    1.63 +                    LOGGER.fine( "Worker for " + text + " exited after cancel " + ( System.currentTimeMillis() - createTime ) + " ms."  );
    1.64 +                    return;
    1.65 +                }
    1.66 +
    1.67 +                if ( !isCanceled && fmodel != null ) {
    1.68 +                    LOGGER.fine( "Worker for text " + text + " finished after " + ( System.currentTimeMillis() - createTime ) + " ms."  );
    1.69 +                    SwingUtilities.invokeLater(new Runnable() {
    1.70 +                        public void run() {
    1.71 +                            panel.setModel(fmodel);
    1.72 +                            if (okButton != null && !types.isEmpty()) {
    1.73 +                                okButton.setEnabled (true);
    1.74 +                            }
    1.75 +                        }
    1.76 +                    });
    1.77 +                }
    1.78 +            } finally {
    1.79 +                if (profile != null) {
    1.80 +                    try {
    1.81 +                        profile.stop();
    1.82 +                    } catch (Exception ex) {
    1.83 +                        LOGGER.log(Level.INFO, "Cannot stop profiling", ex);
    1.84 +                    }
    1.85 +                }
    1.86              }
    1.87 -            ListModel model = Models.fromList(types);
    1.88 -            if (typeFilter != null) {
    1.89 -                model = LazyListModel.create(model, GoToTypeAction.this, 0.1, "Not computed yet");
    1.90 -            }
    1.91 -            final ListModel fmodel = model;
    1.92 -            if ( isCanceled ) {            
    1.93 -                LOGGER.fine( "Worker for " + text + " exited after cancel " + ( System.currentTimeMillis() - createTime ) + " ms."  );                                
    1.94 -                return;
    1.95 -            }
    1.96 -            
    1.97 -            if ( !isCanceled && fmodel != null ) {                
    1.98 -                LOGGER.fine( "Worker for text " + text + " finished after " + ( System.currentTimeMillis() - createTime ) + " ms."  );                
    1.99 -                SwingUtilities.invokeLater(new Runnable() {
   1.100 -                    public void run() {
   1.101 -                        panel.setModel(fmodel);
   1.102 -                        if (okButton != null && !types.isEmpty()) {
   1.103 -                            okButton.setEnabled (true);
   1.104 -                        }
   1.105 -                    }
   1.106 -                });
   1.107 -            }
   1.108 -            
   1.109 -            
   1.110          }
   1.111          
   1.112          public void cancel() {
   1.113 @@ -716,5 +729,61 @@
   1.114          return s1.compareTo( s2 );
   1.115      }
   1.116      
   1.117 +    private Profile initializeProfiling() {
   1.118 +        FileObject fo = FileUtil.getConfigFile("Actions/Profile/org-netbeans-modules-profiler-actions-SelfSamplerAction.instance");
   1.119 +        if (fo == null) {
   1.120 +            return null;
   1.121 +        }
   1.122 +        Action a = (Action)fo.getAttribute("delegate"); // NOI18N
   1.123 +        if (a == null) {
   1.124 +            return null;
   1.125 +        }
   1.126 +        return new Profile(a.getValue("logger-jumpto")); // NOI18N
   1.127 +    }
   1.128 +
   1.129 +    private class Profile implements Runnable {
   1.130 +        Object profiler;
   1.131 +        boolean profiling;
   1.132 +        private final long time;
   1.133 +
   1.134 +        public Profile(Object profiler) {
   1.135 +            time = System.currentTimeMillis();
   1.136 +            this.profiler = profiler;
   1.137 +            RequestProcessor.getDefault().post(this, 3000); // 3s
   1.138 +        }
   1.139 +
   1.140 +        public synchronized void run() {
   1.141 +            profiling = true;
   1.142 +            if (profiler instanceof Runnable) {
   1.143 +                Runnable r = (Runnable)profiler;
   1.144 +                r.run();
   1.145 +            }
   1.146 +        }
   1.147 +
   1.148 +        private synchronized void stop() throws Exception {
   1.149 +            long delta = System.currentTimeMillis() - time;
   1.150 +
   1.151 +            ActionListener ss = (ActionListener)profiler;
   1.152 +            profiler = null;
   1.153 +            if (!profiling) {
   1.154 +                return;
   1.155 +            }
   1.156 +            try {
   1.157 +                ByteArrayOutputStream out = new ByteArrayOutputStream();
   1.158 +                DataOutputStream dos = new DataOutputStream(out);
   1.159 +                ss.actionPerformed(new ActionEvent(dos, 0, "write")); // NOI18N
   1.160 +                dos.close();
   1.161 +                if (dos.size() > 0) {
   1.162 +                    Object[] params = new Object[]{out.toByteArray(), delta };
   1.163 +                    Logger.getLogger("org.netbeans.ui.performance").log(Level.CONFIG, "Slowness detected", params);
   1.164 +                } else {
   1.165 +                    LOGGER.log(Level.WARNING, "no snapshot taken"); // NOI18N
   1.166 +                }
   1.167 +            } catch (Exception ex) {
   1.168 +                Exceptions.printStackTrace(ex);
   1.169 +            }
   1.170 +        }
   1.171 +
   1.172 +    }
   1.173  
   1.174  }