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

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.