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

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.