From 74e354c12eb842c237b68f19e8306e34366c938c Mon Sep 17 00:00:00 2001 From: Nelson Tavares de Sousa <stu103017@mail.uni-kiel.de> Date: Fri, 10 Oct 2014 11:35:03 +0200 Subject: [PATCH] #36 added two configs for test purposes CipherConfiguration encrypts, compresses a file and vice-versa TokenizerConfiguration decrypts, decompresses and reads tokens from a given file updated pipe-factories.conf file --- .gitignore | 1 + conf/pipe-factories.conf | 8 +- pom.xml | 2 +- .../teetime/framework/pipe/PipeFactory.java | 31 +++++++ .../teetime/stage/io/ByteArray2String.java | 18 ++++ .../teetime/stage/io/ByteArrayFileWriter.java | 28 ++++++ .../teetime/stage/io/CipherByteArray.java | 72 +++++++++++++++ .../java/teetime/stage/io/File2ByteArray.java | 39 ++++++++ src/main/java/teetime/stage/io/Tokenizer.java | 29 ++++++ .../java/teetime/stage/io/ZipByteArray.java | 88 +++++++++++++++++++ .../examples/cipher/CipherConfiguration.java | 52 +++++++++++ .../teetime/examples/cipher/CipherTest.java | 50 +++++++++++ .../LoopStageAnalysisConfiguration.java | 2 + .../tokenizer/TokenizerConfiguration.java | 59 +++++++++++++ .../examples/tokenizer/TokenizerTest.java | 43 +++++++++ 15 files changed, 517 insertions(+), 5 deletions(-) create mode 100644 src/main/java/teetime/stage/io/ByteArray2String.java create mode 100644 src/main/java/teetime/stage/io/ByteArrayFileWriter.java create mode 100644 src/main/java/teetime/stage/io/CipherByteArray.java create mode 100644 src/main/java/teetime/stage/io/File2ByteArray.java create mode 100644 src/main/java/teetime/stage/io/Tokenizer.java create mode 100644 src/main/java/teetime/stage/io/ZipByteArray.java create mode 100644 src/performancetest/java/teetime/examples/cipher/CipherConfiguration.java create mode 100644 src/performancetest/java/teetime/examples/cipher/CipherTest.java create mode 100644 src/performancetest/java/teetime/examples/tokenizer/TokenizerConfiguration.java create mode 100644 src/performancetest/java/teetime/examples/tokenizer/TokenizerTest.java diff --git a/.gitignore b/.gitignore index 934e0e06..c920a075 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bin /target +/testdata/ diff --git a/conf/pipe-factories.conf b/conf/pipe-factories.conf index 97c2e631..50f869a0 100644 --- a/conf/pipe-factories.conf +++ b/conf/pipe-factories.conf @@ -1,4 +1,4 @@ -teetime.variant.methodcallWithPorts.framework.core.pipe.SingleElementPipeFactory -teetime.variant.methodcallWithPorts.framework.core.pipe.OrderedGrowableArrayPipeFactory -teetime.variant.methodcallWithPorts.framework.core.pipe.UnorderedGrowablePipeFactory -teetime.variant.methodcallWithPorts.framework.core.pipe.SpScPipeFactory +teetime.framework.pipe.SingleElementPipeFactory +teetime.framework.pipe.OrderedGrowableArrayPipeFactory +teetime.framework.pipe.UnorderedGrowablePipeFactory +teetime.framework.pipe.SpScPipeFactory diff --git a/pom.xml b/pom.xml index 189fdb91..c6d98d0e 100644 --- a/pom.xml +++ b/pom.xml @@ -101,7 +101,7 @@ <version>2.5</version> </plugin> - + <!-- <plugin> --> <!-- <groupId>org.apache.maven.plugins</groupId> --> diff --git a/src/main/java/teetime/framework/pipe/PipeFactory.java b/src/main/java/teetime/framework/pipe/PipeFactory.java index 57de4c11..87a7e84a 100644 --- a/src/main/java/teetime/framework/pipe/PipeFactory.java +++ b/src/main/java/teetime/framework/pipe/PipeFactory.java @@ -54,11 +54,31 @@ public class PipeFactory { return this.create(tc, PipeOrdering.QUEUE_BASED, true, 1); } + /** + * + * @param tc + * @param ordering + * @param growable + * @param capacity + * @return + */ public IPipe create(final ThreadCommunication tc, final PipeOrdering ordering, final boolean growable, final int capacity) { IPipeFactory pipeFactory = getPipeFactory(tc, ordering, growable); return pipeFactory.create(capacity); } + /** + * Returns a PipeFactory Instance + * + * @param tc + * Communication type between two connected stages + * @param ordering + * Specifies the ordering behavior of the pipe + * @param growable + * Whether the queue size is fixed or not + * @return + * A PipeFactory, which provides suitable pipes + */ public IPipeFactory getPipeFactory(final ThreadCommunication tc, final PipeOrdering ordering, final boolean growable) { String key = this.buildKey(tc, ordering, growable); IPipeFactory pipeFactory = this.pipeFactories.get(key); @@ -68,12 +88,23 @@ public class PipeFactory { return pipeFactory; } + /** + * + * @param pipeFactory + */ public void register(final IPipeFactory pipeFactory) { String key = this.buildKey(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable()); this.pipeFactories.put(key, pipeFactory); LOGGER.info("Registered pipe factory: " + pipeFactory.getClass().getCanonicalName()); } + /** + * + * @param tc + * @param ordering + * @param growable + * @return + */ private String buildKey(final ThreadCommunication tc, final PipeOrdering ordering, final boolean growable) { return tc.toString() + ordering.toString() + growable; } diff --git a/src/main/java/teetime/stage/io/ByteArray2String.java b/src/main/java/teetime/stage/io/ByteArray2String.java new file mode 100644 index 00000000..2a132fbc --- /dev/null +++ b/src/main/java/teetime/stage/io/ByteArray2String.java @@ -0,0 +1,18 @@ +package teetime.stage.io; + +import teetime.framework.ConsumerStage; +import teetime.framework.OutputPort; + +public class ByteArray2String extends ConsumerStage<byte[]> { + + private final OutputPort<String> outputPort = this.createOutputPort(); + + @Override + protected void execute(final byte[] element) { + this.send(outputPort, new String(element)); + } + + public OutputPort<? extends String> getOutputPort() { + return outputPort; + } +} diff --git a/src/main/java/teetime/stage/io/ByteArrayFileWriter.java b/src/main/java/teetime/stage/io/ByteArrayFileWriter.java new file mode 100644 index 00000000..a41872e9 --- /dev/null +++ b/src/main/java/teetime/stage/io/ByteArrayFileWriter.java @@ -0,0 +1,28 @@ +package teetime.stage.io; + +import java.io.File; +import java.io.FileOutputStream; + +import teetime.framework.ConsumerStage; + +public class ByteArrayFileWriter extends ConsumerStage<byte[]> { + + private final File file; + + public ByteArrayFileWriter(final File file) { + this.file = file; + } + + @Override + protected void execute(final byte[] element) { + FileOutputStream fo; + // TODO check if file exists, otherwise create file + try { + fo = new FileOutputStream(this.file); + fo.write(element); + fo.close(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } +} diff --git a/src/main/java/teetime/stage/io/CipherByteArray.java b/src/main/java/teetime/stage/io/CipherByteArray.java new file mode 100644 index 00000000..4300802d --- /dev/null +++ b/src/main/java/teetime/stage/io/CipherByteArray.java @@ -0,0 +1,72 @@ +package teetime.stage.io; + +import java.security.spec.KeySpec; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; + +import teetime.framework.ConsumerStage; +import teetime.framework.OutputPort; + +public class CipherByteArray extends ConsumerStage<byte[]> { + + private final OutputPort<byte[]> outputPort = this.createOutputPort(); + private Cipher cipher = null; + private final byte[] salt = { 't', 'e', 's', 't' }; + private SecretKeySpec skeyspec = null; + + public enum CipherMode { + ENCRYPT, DECRYPT + } + + public CipherByteArray(final String password, final CipherMode mode) { + + KeySpec keySpec = new PBEKeySpec(password.toCharArray(), + salt, + 1024, 128); + + SecretKey secretKey = null; + + try { + secretKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"). + generateSecret(keySpec); + } catch (Exception e) { + e.printStackTrace(); + } + + skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES"); + + try { + cipher = Cipher.getInstance(skeyspec.getAlgorithm()); + if (mode == CipherMode.ENCRYPT) { + cipher.init(Cipher.ENCRYPT_MODE, skeyspec); + } else { + cipher.init(Cipher.DECRYPT_MODE, skeyspec); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + protected void execute(final byte[] element) { + + byte[] output = null; + + try { + output = cipher.doFinal(element); + } catch (Exception e) { + e.printStackTrace(); + } + + this.send(outputPort, output); + } + + public OutputPort<? extends byte[]> getOutputPort() { + return outputPort; + } + +} diff --git a/src/main/java/teetime/stage/io/File2ByteArray.java b/src/main/java/teetime/stage/io/File2ByteArray.java new file mode 100644 index 00000000..051f0499 --- /dev/null +++ b/src/main/java/teetime/stage/io/File2ByteArray.java @@ -0,0 +1,39 @@ +package teetime.stage.io; + +import java.io.File; +import java.io.IOException; + +import teetime.framework.ConsumerStage; +import teetime.framework.HeadStage; +import teetime.framework.OutputPort; + +import com.google.common.io.Files; + +public class File2ByteArray extends ConsumerStage<File> implements HeadStage { + + private final OutputPort<byte[]> outputPort = this.createOutputPort(); + + @Override + protected void execute(final File element) { + try { + byte[] cache = Files.toByteArray(element); + this.send(outputPort, cache); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + public OutputPort<? extends byte[]> getOutputPort() { + return outputPort; + } + + @Override + public boolean shouldBeTerminated() { + return false; + } + + @Override + public void terminate() { + + } +} diff --git a/src/main/java/teetime/stage/io/Tokenizer.java b/src/main/java/teetime/stage/io/Tokenizer.java new file mode 100644 index 00000000..e40aebef --- /dev/null +++ b/src/main/java/teetime/stage/io/Tokenizer.java @@ -0,0 +1,29 @@ +package teetime.stage.io; + +import java.util.StringTokenizer; + +import teetime.framework.ConsumerStage; +import teetime.framework.OutputPort; + +public class Tokenizer extends ConsumerStage<String> { + + private final OutputPort<String> outputPort = this.createOutputPort(); + private final String regex; + + public Tokenizer(final String regex) { + this.regex = regex; + } + + @Override + protected void execute(final String element) { + StringTokenizer st = new StringTokenizer(element, regex); + while (st.hasMoreTokens()) { + this.send(outputPort, st.nextToken()); + } + } + + public OutputPort<? extends String> getOutputPort() { + return outputPort; + } + +} diff --git a/src/main/java/teetime/stage/io/ZipByteArray.java b/src/main/java/teetime/stage/io/ZipByteArray.java new file mode 100644 index 00000000..a4720218 --- /dev/null +++ b/src/main/java/teetime/stage/io/ZipByteArray.java @@ -0,0 +1,88 @@ +package teetime.stage.io; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.DataFormatException; +import java.util.zip.Deflater; +import java.util.zip.Inflater; + +import teetime.framework.ConsumerStage; +import teetime.framework.OutputPort; + +/** + * A stage to compress and decompress byte arrays + * + * @author Nelson Tavares de Sousa + * + */ +public class ZipByteArray extends ConsumerStage<byte[]> { + + private final OutputPort<byte[]> outputPort = this.createOutputPort(); + private final ZipMode mode; + + public enum ZipMode { + COMP, DECOMP + } + + public ZipByteArray(final ZipMode mode) { + this.mode = mode; + } + + @Override + protected void execute(final byte[] element) { + byte[] cache = null; + try { + if (mode == ZipMode.COMP) { + cache = compress(element); + } else { + cache = decompress(element); + } + } catch (Exception e) { + e.printStackTrace(); + } + this.send(outputPort, cache); + } + + public static byte[] compress(final byte[] data) throws IOException { + Deflater deflater = new Deflater(); + deflater.setInput(data); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); + + deflater.finish(); + byte[] buffer = new byte[1024]; + while (!deflater.finished()) { + int count = deflater.deflate(buffer); // returns the generated code... index + outputStream.write(buffer, 0, count); + } + outputStream.close(); + byte[] output = outputStream.toByteArray(); + + deflater.end(); + + return output; + } + + public static byte[] decompress(final byte[] data) throws IOException, DataFormatException { + Inflater inflater = new Inflater(); + inflater.setInput(data); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); + byte[] buffer = new byte[1024]; + while (!inflater.finished()) { + int count = inflater.inflate(buffer); + outputStream.write(buffer, 0, count); + } + outputStream.close(); + byte[] output = outputStream.toByteArray(); + + inflater.end(); + + return output; + } + + public OutputPort<? extends byte[]> getOutputPort() { + return outputPort; + } + +} diff --git a/src/performancetest/java/teetime/examples/cipher/CipherConfiguration.java b/src/performancetest/java/teetime/examples/cipher/CipherConfiguration.java new file mode 100644 index 00000000..917d5dcc --- /dev/null +++ b/src/performancetest/java/teetime/examples/cipher/CipherConfiguration.java @@ -0,0 +1,52 @@ +package teetime.examples.cipher; + +import java.io.File; + +import teetime.framework.AnalysisConfiguration; +import teetime.framework.pipe.PipeFactory; +import teetime.framework.pipe.PipeFactory.PipeOrdering; +import teetime.framework.pipe.PipeFactory.ThreadCommunication; +import teetime.stage.InitialElementProducer; +import teetime.stage.io.ByteArrayFileWriter; +import teetime.stage.io.CipherByteArray; +import teetime.stage.io.CipherByteArray.CipherMode; +import teetime.stage.io.File2ByteArray; +import teetime.stage.io.ZipByteArray; +import teetime.stage.io.ZipByteArray.ZipMode; + +public class CipherConfiguration extends AnalysisConfiguration { + + private final PipeFactory pipeFactory = PipeFactory.INSTANCE; + private final File input, output; + private final String password; + + public CipherConfiguration(final String inputFile, final String outputFile, final String password) { + this.input = new File(inputFile); + this.output = new File(outputFile); + this.password = password; + + InitialElementProducer<File> init = new InitialElementProducer<File>(this.input); + File2ByteArray f2b = new File2ByteArray(); + CipherByteArray enc = new CipherByteArray(this.password, CipherMode.ENCRYPT); + ZipByteArray comp = new ZipByteArray(ZipMode.COMP); + ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP); + CipherByteArray decrypt = new CipherByteArray(this.password, CipherMode.DECRYPT); + ByteArrayFileWriter writer = new ByteArrayFileWriter(output); + + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(init.getOutputPort(), f2b.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(f2b.getOutputPort(), enc.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(enc.getOutputPort(), comp.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(comp.getOutputPort(), decomp.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(decomp.getOutputPort(), decrypt.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(decrypt.getOutputPort(), writer.getInputPort()); + + this.getFiniteProducerStages().add(init); + + } +} diff --git a/src/performancetest/java/teetime/examples/cipher/CipherTest.java b/src/performancetest/java/teetime/examples/cipher/CipherTest.java new file mode 100644 index 00000000..0021944a --- /dev/null +++ b/src/performancetest/java/teetime/examples/cipher/CipherTest.java @@ -0,0 +1,50 @@ +package teetime.examples.cipher; + +import java.io.File; +import java.io.IOException; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import teetime.framework.Analysis; +import teetime.framework.AnalysisConfiguration; + +import com.google.common.io.Files; + +public class CipherTest { + + static String inputFile = "testdata/dependencies.html"; + static String outputFile = "testdata/dependencies.html"; + static String password = "Password"; + static long start; + long stop; + + static AnalysisConfiguration configuration = new CipherConfiguration(inputFile, outputFile, password); + + final static Analysis analysis = new Analysis(configuration); + + @BeforeClass + public static void beforeClass() { + analysis.init(); + start = System.currentTimeMillis(); + } + + @Test + public void executeTest() { + analysis.start(); + } + + @AfterClass + public static void afterClass() { + System.out.println("It took " + (System.currentTimeMillis() - start) + " Milliseconds"); + boolean bool = false; + try { + bool = Files.equal(new File(inputFile), new File(outputFile)); + } catch (IOException e) { + e.printStackTrace(); + } + Assert.assertTrue(bool); + } +} diff --git a/src/performancetest/java/teetime/examples/loopStage/LoopStageAnalysisConfiguration.java b/src/performancetest/java/teetime/examples/loopStage/LoopStageAnalysisConfiguration.java index 6f577b41..31f63113 100644 --- a/src/performancetest/java/teetime/examples/loopStage/LoopStageAnalysisConfiguration.java +++ b/src/performancetest/java/teetime/examples/loopStage/LoopStageAnalysisConfiguration.java @@ -4,6 +4,8 @@ import teetime.framework.AnalysisConfiguration; import teetime.framework.pipe.PipeFactory; import teetime.framework.pipe.PipeFactory.ThreadCommunication; +import teetime.examples.loopStage.Countdown; + public class LoopStageAnalysisConfiguration extends AnalysisConfiguration { public LoopStageAnalysisConfiguration() { diff --git a/src/performancetest/java/teetime/examples/tokenizer/TokenizerConfiguration.java b/src/performancetest/java/teetime/examples/tokenizer/TokenizerConfiguration.java new file mode 100644 index 00000000..538a5560 --- /dev/null +++ b/src/performancetest/java/teetime/examples/tokenizer/TokenizerConfiguration.java @@ -0,0 +1,59 @@ +package teetime.examples.tokenizer; + +import java.io.File; + +import teetime.framework.AnalysisConfiguration; +import teetime.framework.pipe.PipeFactory; +import teetime.framework.pipe.PipeFactory.PipeOrdering; +import teetime.framework.pipe.PipeFactory.ThreadCommunication; +import teetime.stage.InitialElementProducer; +import teetime.stage.io.ByteArray2String; +import teetime.stage.io.CipherByteArray; +import teetime.stage.io.CipherByteArray.CipherMode; +import teetime.stage.io.File2ByteArray; +import teetime.stage.io.Printer; +import teetime.stage.io.Tokenizer; +import teetime.stage.io.ZipByteArray; +import teetime.stage.io.ZipByteArray.ZipMode; + +public class TokenizerConfiguration extends AnalysisConfiguration { + + private final PipeFactory pipeFactory = PipeFactory.INSTANCE; + private final File input; + private final String password; + + public TokenizerConfiguration(final String inputFile, final String password) { + this.input = new File(inputFile); + this.password = password; + + InitialElementProducer<File> init = new InitialElementProducer<File>(this.input); + File2ByteArray f2b = new File2ByteArray(); + CipherByteArray enc = new CipherByteArray(this.password, CipherMode.ENCRYPT); + ZipByteArray comp = new ZipByteArray(ZipMode.COMP); + ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP); + CipherByteArray decrypt = new CipherByteArray(this.password, CipherMode.DECRYPT); + ByteArray2String b2s = new ByteArray2String(); + Tokenizer tokenizer = new Tokenizer(" "); + Printer<String> pt = new Printer<String>(); + + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(init.getOutputPort(), f2b.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(f2b.getOutputPort(), enc.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(enc.getOutputPort(), comp.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(comp.getOutputPort(), decomp.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(decomp.getOutputPort(), decrypt.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(decrypt.getOutputPort(), b2s.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(b2s.getOutputPort(), tokenizer.getInputPort()); + this.pipeFactory.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) + .create(tokenizer.getOutputPort(), pt.getInputPort()); + + this.getFiniteProducerStages().add(init); + + } +} diff --git a/src/performancetest/java/teetime/examples/tokenizer/TokenizerTest.java b/src/performancetest/java/teetime/examples/tokenizer/TokenizerTest.java new file mode 100644 index 00000000..c5363323 --- /dev/null +++ b/src/performancetest/java/teetime/examples/tokenizer/TokenizerTest.java @@ -0,0 +1,43 @@ +package teetime.examples.tokenizer; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import teetime.framework.Analysis; +import teetime.framework.AnalysisConfiguration; + +public class TokenizerTest { + + static String inputFile = "testdata/dependencies.html"; + static String password = "Password"; + static long start; + long stop; + + static AnalysisConfiguration configuration = new TokenizerConfiguration(inputFile, password); + + final static Analysis analysis = new Analysis(configuration); + + @BeforeClass + public static void beforeClass() { + analysis.init(); + start = System.currentTimeMillis(); + } + + @Test + public void executeTest() { + analysis.start(); + } + + @AfterClass + public static void afterClass() { + System.out.println("It took " + (System.currentTimeMillis() - start) + " Milliseconds"); + // boolean bool = false; + // try { + // bool = Files.equal(new File(inputFile), new File(outputFile)); + // } catch (IOException e) { + // e.printStackTrace(); + // } + // Assert.assertTrue(bool); + } +} -- GitLab