From f4f680be2222687e2af462c4effa4ed9fdb48086 Mon Sep 17 00:00:00 2001 From: Vrekt Date: Fri, 13 Jan 2017 18:36:38 -0600 Subject: [PATCH] Initial commit, v0.1 --- src/me/vrekt/jbench/JBench.java | 155 ++++++++++++++++++ .../vrekt/jbench/benchmark/BenchThread.java | 50 ++++++ 2 files changed, 205 insertions(+) create mode 100644 src/me/vrekt/jbench/JBench.java create mode 100644 src/me/vrekt/jbench/benchmark/BenchThread.java diff --git a/src/me/vrekt/jbench/JBench.java b/src/me/vrekt/jbench/JBench.java new file mode 100644 index 0000000..14199d9 --- /dev/null +++ b/src/me/vrekt/jbench/JBench.java @@ -0,0 +1,155 @@ +package me.vrekt.jbench; + +import java.awt.Color; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.BorderFactory; +import javax.swing.GroupLayout; +import javax.swing.GroupLayout.Alignment; +import javax.swing.JButton; +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.LayoutStyle.ComponentPlacement; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import javax.swing.text.SimpleAttributeSet; + +import me.vrekt.jbench.benchmark.BenchThread; + +public class JBench { + + private JFrame frame; + + private JButton startBenchmark; + private JButton stopBenchmark; + + private JEditorPane editorPane; + private GroupLayout groupLayout; + + private int processorCores = Runtime.getRuntime().availableProcessors(); + private int threadsFinished = 0; + + private List workerThreads = new ArrayList(); + private List threadTimes = new ArrayList(); + + private static JBench instance; + + public static void main(String[] args) { + instance = new JBench(); + instance.initialize(); + } + + public void initialize() { + frame = new JFrame("JBench: v0.1"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + frame.setLocationRelativeTo(null); + frame.setSize(400, 400); + frame.setResizable(false); + + /* + * startBenchmark = new JButton("Start Benchmark"); stopBenchmark = new + * JButton("Stop benchmark"); + * + * editorPane = new JEditorPane(); groupLayout = new + * GroupLayout(frame.getContentPane()); + */ + + startBenchmark = new JButton("Start Benchmark"); + stopBenchmark = new JButton("Stop Benchmark"); + + stopBenchmark.addActionListener(new ActionListener() { + @SuppressWarnings("deprecation") + @Override + public void actionPerformed(ActionEvent event) { + + workerThreads.forEach(thread -> thread.stop()); + workerThreads.clear(); + startBenchmark.setEnabled(true); + stopBenchmark.setEnabled(false); + + } + }); + + startBenchmark.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + workerThreads.clear(); + writeString("Detected " + processorCores + " CPU cores."); + + for (int i = 0; i < processorCores; i++) { + writeString("Thread #" + i + " has started."); + BenchThread thread = new BenchThread(instance, i); + workerThreads.add(thread); + thread.start(); + } + stopBenchmark.setEnabled(true); + startBenchmark.setEnabled(false); + + } + }); + + stopBenchmark.setEnabled(false); + editorPane = new JEditorPane(); + + editorPane.setFont(new Font("Corbel", Font.PLAIN, 14)); + editorPane.setEditable(false); + editorPane.setText(" Benchmark results will appear here"); + editorPane.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + groupLayout = new GroupLayout(frame.getContentPane()); + groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.TRAILING) + .addGroup(groupLayout.createSequentialGroup().addContainerGap() + .addComponent(editorPane, GroupLayout.PREFERRED_SIZE, 228, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.UNRELATED) + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false) + .addComponent(startBenchmark, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, + Short.MAX_VALUE) + .addComponent(stopBenchmark, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)) + .addContainerGap(22, Short.MAX_VALUE))); + groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING) + .addGroup(groupLayout.createSequentialGroup().addContainerGap() + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) + .addComponent(editorPane, GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE) + .addGroup(groupLayout.createSequentialGroup().addComponent(startBenchmark) + .addPreferredGap(ComponentPlacement.RELATED).addComponent(stopBenchmark))) + .addContainerGap())); + frame.getContentPane().setLayout(groupLayout); + + frame.setVisible(true); + } + + public void writeString(String text) { + Document doc = editorPane.getDocument(); + + try { + doc.insertString(doc.getLength(), "\n " + text, new SimpleAttributeSet()); + } catch (BadLocationException e) { + e.printStackTrace(); + } + } + + public void threadFinished(long time) { + threadTimes.add(time); + threadsFinished++; + if (threadsFinished >= processorCores) { + long total = 0; + for (int i = 0; i < threadTimes.size(); i++) { + total += threadTimes.get(i); + } + + long score = total / threadTimes.size(); + threadTimes.clear(); + + writeString("Your score was " + score); + + stopBenchmark.setEnabled(false); + startBenchmark.setEnabled(true); + threadsFinished = 0; + } + } + +} diff --git a/src/me/vrekt/jbench/benchmark/BenchThread.java b/src/me/vrekt/jbench/benchmark/BenchThread.java new file mode 100644 index 0000000..53953ee --- /dev/null +++ b/src/me/vrekt/jbench/benchmark/BenchThread.java @@ -0,0 +1,50 @@ +package me.vrekt.jbench.benchmark; + +import me.vrekt.jbench.JBench; + +public class BenchThread extends Thread { + + private String start = "aaaaaa"; + private long startTime; + + private boolean cc = false; + + private int ID; + private JBench instance; + + public BenchThread(JBench instance, int ID) { + this.instance = instance; + this.ID = ID; + } + + private boolean result(String pw) { + return pw.equals("zzzzzz"); + } + + private String next(String s) { + + int length = s.length(); + char c = s.charAt(length - 1); + + String i = s.substring(0, length - 1); + start = c == 'z' ? length > 1 ? next(i) + 'a' : "aa" : i + ++c; + return start; + } + + @Override + public void run() { + + startTime = System.currentTimeMillis(); + + while (!cc) { + cc = result(next(start)); + } + + long time = System.currentTimeMillis() - startTime; + long seconds = time / 1000; + instance.writeString("Thread #" + ID + " took about " + seconds + " seconds."); + instance.threadFinished(time); + + } + +}