Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
PerformancePanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 
21 package org.sleuthkit.autopsy.diagnostics;
22 
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.Arrays;
30 import java.util.Date;
31 import java.util.List;
32 import java.util.Random;
33 import java.util.concurrent.ExecutionException;
34 import javax.swing.JFrame;
35 import javax.swing.SwingUtilities;
36 import javax.swing.SwingWorker;
37 
38 import org.openide.util.NbBundle;
39 import org.openide.windows.WindowManager;
46 
47 public class PerformancePanel extends javax.swing.JDialog {
48 
52  public PerformancePanel() {
53  super((JFrame) WindowManager.getDefault().getMainWindow(),
54  NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.title"), true);
56  }
57 
63  @SuppressWarnings("unchecked")
64  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
65  private void initComponents() {
66 
67  jLabel1 = new javax.swing.JLabel();
68  imgReadLabel = new javax.swing.JLabel();
69  jLabel2 = new javax.swing.JLabel();
70  dbReadLabel = new javax.swing.JLabel();
71  jLabel4 = new javax.swing.JLabel();
72  fileReadLabel = new javax.swing.JLabel();
73  jLabel3 = new javax.swing.JLabel();
74  cpuTimeLabel = new javax.swing.JLabel();
75  startButton = new javax.swing.JButton();
76  statusLabel = new javax.swing.JLabel();
77  jLabel5 = new javax.swing.JLabel();
78 
79  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
80 
81  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.jLabel1.text")); // NOI18N
82 
83  org.openide.awt.Mnemonics.setLocalizedText(imgReadLabel, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.imgReadLabel.text")); // NOI18N
84 
85  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.jLabel2.text")); // NOI18N
86 
87  org.openide.awt.Mnemonics.setLocalizedText(dbReadLabel, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.dbReadLabel.text")); // NOI18N
88 
89  org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.jLabel4.text")); // NOI18N
90 
91  org.openide.awt.Mnemonics.setLocalizedText(fileReadLabel, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.fileReadLabel.text")); // NOI18N
92 
93  org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.jLabel3.text")); // NOI18N
94 
95  org.openide.awt.Mnemonics.setLocalizedText(cpuTimeLabel, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.cpuTimeLabel.text")); // NOI18N
96 
97  org.openide.awt.Mnemonics.setLocalizedText(startButton, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.startButton.text")); // NOI18N
98  startButton.addActionListener(new java.awt.event.ActionListener() {
99  public void actionPerformed(java.awt.event.ActionEvent evt) {
101  }
102  });
103 
104  org.openide.awt.Mnemonics.setLocalizedText(statusLabel, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.statusLabel.text")); // NOI18N
105 
106  org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(PerformancePanel.class, "PerformancePanel.jLabel5.text")); // NOI18N
107 
108  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
109  getContentPane().setLayout(layout);
110  layout.setHorizontalGroup(
111  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
112  .addGroup(layout.createSequentialGroup()
113  .addContainerGap()
114  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
115  .addGroup(layout.createSequentialGroup()
116  .addGap(10, 10, 10)
117  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
118  .addGroup(layout.createSequentialGroup()
119  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
120  .addComponent(jLabel1)
121  .addComponent(jLabel2)
122  .addComponent(jLabel3))
123  .addGap(31, 31, 31)
124  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
125  .addComponent(fileReadLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
126  .addComponent(dbReadLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
127  .addComponent(cpuTimeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
128  .addComponent(imgReadLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE))
129  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
130  .addGroup(layout.createSequentialGroup()
131  .addComponent(jLabel4)
132  .addGap(0, 0, Short.MAX_VALUE))))
133  .addGroup(layout.createSequentialGroup()
134  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
135  .addComponent(startButton)
136  .addComponent(jLabel5))
137  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
138  .addGroup(layout.createSequentialGroup()
139  .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 508, javax.swing.GroupLayout.PREFERRED_SIZE)
140  .addGap(0, 0, Short.MAX_VALUE))))
141  );
142  layout.setVerticalGroup(
143  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
144  .addGroup(layout.createSequentialGroup()
145  .addContainerGap()
146  .addComponent(jLabel5)
147  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
148  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
149  .addComponent(jLabel3)
150  .addComponent(cpuTimeLabel))
151  .addGap(18, 18, 18)
152  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
153  .addComponent(imgReadLabel)
154  .addComponent(jLabel1))
155  .addGap(19, 19, 19)
156  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
157  .addComponent(jLabel4)
158  .addComponent(fileReadLabel))
159  .addGap(21, 21, 21)
160  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
161  .addComponent(jLabel2)
162  .addComponent(dbReadLabel))
163  .addGap(18, 18, 18)
164  .addComponent(statusLabel)
165  .addGap(9, 9, 9)
166  .addComponent(startButton)
167  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
168  );
169 
170  pack();
171  }// </editor-fold>//GEN-END:initComponents
172 
173  private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
174  // TODO add your handling code here:
175  startButton.setEnabled(false);
176  SwingWorker<?,?> worker = new PerformanceTestWorker();
177  worker.execute();
178 
179  }//GEN-LAST:event_startButtonActionPerformed
180 
181 
182  // Variables declaration - do not modify//GEN-BEGIN:variables
183  private javax.swing.JLabel cpuTimeLabel;
184  private javax.swing.JLabel dbReadLabel;
185  private javax.swing.JLabel fileReadLabel;
186  private javax.swing.JLabel imgReadLabel;
187  private javax.swing.JLabel jLabel1;
188  private javax.swing.JLabel jLabel2;
189  private javax.swing.JLabel jLabel3;
190  private javax.swing.JLabel jLabel4;
191  private javax.swing.JLabel jLabel5;
192  private javax.swing.JButton startButton;
193  private javax.swing.JLabel statusLabel;
194  // End of variables declaration//GEN-END:variables
195 
196  class PerformanceTestWorker extends SwingWorker<Void, Void> {
197  private long cpuStats;
198  private long imgReadStats;
199  private long dbStats;
200  private long fileReadStats;
201 
202  @Override
203  protected Void doInBackground() throws Exception {
204  setCpuLabel("");
205  setImgLabel("");
206  setDbLabel("");
207  setFileReadLabel("");
208 
209  doCpuTest();
210  doImgTest();
211  doFileReadTest();
212  doDbTest();
213 
214  return null;
215  }
216 
217  private void setCpuLabel(final String msg) {
218  SwingUtilities.invokeLater(new Runnable() {
219  @Override
220  public void run() {
221  cpuTimeLabel.setText(msg);
222  }
223  });
224  }
225 
226  private void setImgLabel(final String msg) {
227  SwingUtilities.invokeLater(new Runnable() {
228  @Override
229  public void run() {
230  imgReadLabel.setText(msg);
231  }
232  });
233  }
234 
235  private void setFileReadLabel(final String msg) {
236  SwingUtilities.invokeLater(new Runnable() {
237  @Override
238  public void run() {
239  fileReadLabel.setText(msg);
240  }
241  });
242  }
243 
244  private void setDbLabel(final String msg) {
245  SwingUtilities.invokeLater(new Runnable() {
246  @Override
247  public void run() {
248  dbReadLabel.setText(msg);
249  }
250  });
251  }
252 
253  private void setStatusMsg(final String msg) {
254  SwingUtilities.invokeLater(new Runnable() {
255  @Override
256  public void run() {
257  statusLabel.setText(msg);
258  }
259  });
260  }
261 
262 
263  private void doCpuTest() {
264  final String msg = NbBundle.getMessage(this.getClass(), "PerformancePanel.cpuTest.basemsg");
265 
266  MessageDigest md;
267  long start = new Date().getTime();
268  try {
269  md = MessageDigest.getInstance("MD5"); // NON-NLS
270  } catch (NoSuchAlgorithmException ex) {
271  setCpuLabel(
272  NbBundle.getMessage(this.getClass(), "PerformancePanel.cpuTest.cpuLabel.md5AlgNotFound.text"));
273  return;
274  }
275 
276  byte[] buf = new byte[256 * 1024];
277  long bytesRead = 0;
278  for (int a = 0; a < 50; a++) {
279  if (a % 10 == 0)
280  setStatusMsg(msg + " " + a * 100 / 50 + "%");
281  for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
282  Arrays.fill(buf, b);
283  md.update(buf);
284  bytesRead += buf.length;
285  }
286  }
287  md.digest();
288 
289  long end = new Date().getTime();
290  cpuStats = (bytesRead / (1024 * 1024)) / ((end - start) / 1000);
291 
292  setCpuLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.cpuTest.cpuLabel.MBHashedPerSec.text",
293  cpuStats));
294  setStatusMsg("");
295  }
296 
297  private void doImgTest() {
298  imgReadStats = 0;
299  setStatusMsg(
300  NbBundle.getMessage(this.getClass(), "PerformancePanel.imgTest.statusMsg.runningImgReadTest.text"));
301 
302  Case curCase;
303  try {
304  curCase = Case.getCurrentCase();
305  }
306  catch (Exception e) {
307  setImgLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.label.caseNotOpen.text"));
308  setStatusMsg("");
309  return;
310  }
311 
312  List<Content> dataSources;
313  try {
314  dataSources = curCase.getDataSources();
315  } catch (TskCoreException ex) {
316  setImgLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.label.noImgInCase.text"));
317  setStatusMsg("");
318  return;
319  }
320  Image image = null;
321  for (Content c : dataSources) {
322  if (c instanceof Image) {
323  image = (Image)c;
324  }
325  }
326  if (image == null) {
327  setImgLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.label.noImgInCase.text"));
328  setStatusMsg("");
329  return;
330  }
331 
332  long start = new Date().getTime();
333 
334  byte[] buf = new byte[4096];
335  long bytesRead = 0;
336 
337  // random starting point to prevent caching from effecting it
338  Random rand = new Random();
339  long curOffset = rand.nextLong();
340  if (curOffset < 0) {
341  curOffset *= -1;
342  }
343  curOffset = curOffset % (image.getSize() / 2);
344  curOffset = 512 * ((curOffset + 511) / 512);
345 
346  //long curOffset = 0;
347  while (bytesRead < 1000 * 1024 * 1024 ) {
348  long read;
349  try {
350  read = image.read(buf, curOffset, buf.length);
351  } catch (TskCoreException ex) {
352  break;
353  }
354  if (read <= 0)
355  break;
356  bytesRead += read;
357  curOffset += read;
358  }
359  long end = new Date().getTime();
360  long elapsed = (end - start) / 1000;
361  if (elapsed > 0)
362  imgReadStats = (bytesRead / (1024 * 1024)) / elapsed;
363  else
364  imgReadStats = 0;
365  setImgLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.ImgTest.imgLabel.MBReadPerSec.text",
366  imgReadStats, bytesRead));
367  setStatusMsg("");
368  }
369 
370  private void doFileReadTest() {
371  fileReadStats = 0;
372 
373  // TODO: this is always true. Why display a "Skipped" label and then go on to run the test?
374  if (true) {
375  setFileReadLabel(
376  NbBundle.getMessage(this.getClass(), "PerformancePanel.FileReadTest.fileReadLabel.skipped.text"));
377  }
378 
379  setStatusMsg(NbBundle.getMessage(this.getClass(),
380  "PerformancePanel.FileReadTest.statusMsg.runningFileReadTest.text"));
381 
382  Case curCase;
383  try {
384  curCase = Case.getCurrentCase();
385  }
386  catch (Exception e) {
387  setFileReadLabel(
388  NbBundle.getMessage(this.getClass(), "PerformancePanel.label.caseNotOpen.text"));
389  setStatusMsg("");
390  return;
391  }
392 
393  List<Content> dataSources;
394  try {
395  dataSources = curCase.getDataSources();
396  } catch (TskCoreException ex) {
397  setFileReadLabel(
398  NbBundle.getMessage(this.getClass(), "PerformancePanel.label.noImgInCase.text"));
399  setStatusMsg("");
400  return;
401  }
402  Image image = null;
403  for (Content c : dataSources) {
404  if (c instanceof Image) {
405  image = (Image)c;
406  }
407  }
408  if (image == null) {
409  setFileReadLabel(
410  NbBundle.getMessage(this.getClass(), "PerformancePanel.label.noImgInCase.text"));
411  setStatusMsg("");
412  return;
413  }
414 
415  File file = new File(image.getPaths()[0]);
416  if (file.exists() == false) {
417  setFileReadLabel(
418  NbBundle.getMessage(this.getClass(), "PerformancePanel.fileReadLabel.imgPathNotExist.text"));
419  setStatusMsg("");
420  return;
421  }
422 
423  FileReader fileReader;
424  try {
425  fileReader = new FileReader(file);
426  } catch (FileNotFoundException ex) {
427  setFileReadLabel(
428  NbBundle.getMessage(this.getClass(), "PerformancePanel.fileReadLabel.errMakeFileReader.text"));
429  setStatusMsg("");
430  return;
431  }
432 
433  long start = new Date().getTime();
434  // random starting point to prevent caching from effecting it
435  // make RandomAccessFile instad
436  /*Random rand = new Random();
437  long curOffset = rand.nextLong();
438  if (curOffset < 0) {
439  curOffset *= -1;
440  }
441  curOffset = curOffset % (file.length());
442  curOffset = 512 * ((curOffset + 511) / 512);
443  try {
444  fileReader.skip(curOffset);
445  } catch (IOException ex) {
446  setFileReadLabel("Error seeking: " + curOffset);
447  return;
448  }*/
449 
450  char[] buf = new char[4096];
451  int bytesRead = 0;
452  while (bytesRead < 1000 * 1024 * 1024 ) {
453  long read;
454  try {
455  read = fileReader.read(buf, 0, buf.length);
456  } catch (IOException ex) {
457  break;
458  }
459  if (read <= 0)
460  break;
461  bytesRead += read;
462  }
463  long end = new Date().getTime();
464  long elapsed = (end - start) / 1000;
465  if (elapsed > 0)
466  fileReadStats = (bytesRead / (1024 * 1024)) / elapsed;
467  else
468  fileReadStats = 0;
469  setFileReadLabel(
470  NbBundle.getMessage(this.getClass(), "PerformancePanel.ImgTest.fileReadLabel.MBReadPerSec.text",
471  fileReadStats, bytesRead));
472  setStatusMsg("");
473  }
474 
475  private void doDbTest() {
476  dbStats = 0;
477  setStatusMsg(NbBundle.getMessage(this.getClass(), "PerformancePanel.dbTest.status.running"));
478 
479  Case curCase;
480  try {
481  curCase = Case.getCurrentCase();
482  }
483  catch (Exception e) {
484  setDbLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.label.caseNotOpen.text"));
485  return;
486  }
487 
488  try {
489  SleuthkitCase tskCase = curCase.getSleuthkitCase();
490  long start = new Date().getTime();
491 
492  List<AbstractFile> files = tskCase.findAllFilesWhere("obj_id < 50000"); // NON-NLS
493 
494  long end = new Date().getTime();
495  long elapsed = (end - start) / 1000;
496  if (elapsed > 0)
497  dbStats = files.size() / elapsed;
498  else
499  dbStats = 0;
500 
501  setDbLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.dbTest.dbLabel.recordsPerSec.text",
502  dbStats));
503  } catch (TskCoreException ex) {
504  setDbLabel(NbBundle.getMessage(this.getClass(), "PerformancePanel.dbTest.dbLabel.errPerformQuery.text"));
505  }
506 
507  setStatusMsg("");
508  }
509 
510  @Override
511  protected void done() {
512  try {
513  get();
514  } catch (InterruptedException | ExecutionException ex) {
515  setStatusMsg(NbBundle.getMessage(this.getClass(), "PerformancePanel.done.statusMsg.err.text",
516  ex.getMessage()));
517  }
518  startButton.setEnabled(true);
519  }
520  }
521 }
522 
List< Content > getDataSources()
Definition: Case.java:806
int read(byte[] buf, long offset, long len)
List< AbstractFile > findAllFilesWhere(String sqlWhereClause)
void startButtonActionPerformed(java.awt.event.ActionEvent evt)

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.