diff options
Diffstat (limited to 'src/main/java/org/linaro/benchmarks/benchmarksgame/fastaredux.java')
-rw-r--r-- | src/main/java/org/linaro/benchmarks/benchmarksgame/fastaredux.java | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/src/main/java/org/linaro/benchmarks/benchmarksgame/fastaredux.java b/src/main/java/org/linaro/benchmarks/benchmarksgame/fastaredux.java new file mode 100644 index 0000000..a0a86dd --- /dev/null +++ b/src/main/java/org/linaro/benchmarks/benchmarksgame/fastaredux.java @@ -0,0 +1,204 @@ +/* + * This benchmark has been ported from "The Computer Language Benchmarks Game" suite and modified + * to fit the benchmarking framework. + * The original benchmarks printed long strings to the stdout. This print was overrided to do + * nothing to fit the framework. These action can cause difference in behaviour of the original and + * changed benchmarks; it hasn't been estimated yet. + * + * The original file is `fastaredux/fastaredux.java-3.java` from the archive + * available at + * http://benchmarksgame.alioth.debian.org/download/benchmarksgame-sourcecode.zip. + * See LICENSE file in the same folder (BSD 3-clause). + * + * The Computer Language Benchmarks Game + * http://benchmarksgame.alioth.debian.org/ + * + * modified by Enotus + * + */ + +/* + * Description: Generate and write random DNA sequences. + * Main Focus: TODO + * + */ + +package org.linaro.benchmarks.benchmarksgame; + +import java.io.*; +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) + +// CHECKSTYLE.OFF: .* +public class fastaredux { + + static final int LINE_LENGTH = 60; + static final int OUT_BUFFER_SIZE = 256*1024; + static final int LOOKUP_SIZE = 4*1024; + static final double LOOKUP_SCALE = LOOKUP_SIZE - 1; + + static final class Freq { + byte c; + double p; + Freq(char cc, double pp) {c = (byte) cc;p = pp;} + } + + static final String ALU = + "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" + + "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" + + "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" + + "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" + + "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" + + "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" + + "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"; + static final Freq[] IUB = { + new Freq('a', 0.27), + new Freq('c', 0.12), + new Freq('g', 0.12), + new Freq('t', 0.27), + new Freq('B', 0.02), + new Freq('D', 0.02), + new Freq('H', 0.02), + new Freq('K', 0.02), + new Freq('M', 0.02), + new Freq('N', 0.02), + new Freq('R', 0.02), + new Freq('S', 0.02), + new Freq('V', 0.02), + new Freq('W', 0.02), + new Freq('Y', 0.02)}; + static final Freq[] HomoSapiens = { + new Freq('a', 0.3029549426680), + new Freq('c', 0.1979883004921), + new Freq('g', 0.1975473066391), + new Freq('t', 0.3015094502008)}; + + static void sumAndScale(Freq[] a) { + double p = 0; + for (int i = 0; i < a.length; i++) + a[i].p = (p += a[i].p) * LOOKUP_SCALE; + a[a.length - 1].p = LOOKUP_SCALE; + } + + static final class Random { + + static final int IM = 139968; + static final int IA = 3877; + static final int IC = 29573; + static final double SCALE = LOOKUP_SCALE / IM; + static int last = 42; + + static double next() { + return SCALE * (last = (last * IA + IC) % IM); + } + } + + static final class Out { + + static byte buf[] = new byte[OUT_BUFFER_SIZE]; + static final int lim = OUT_BUFFER_SIZE - 2*LINE_LENGTH - 1; + static int ct = 0; + static OutputStream stream; + + static void checkFlush() throws IOException { + if (ct >= lim) { stream.write(buf, 0, ct); ct = 0;} + } + + static void close() throws IOException { + stream.write(buf, 0, ct);ct = 0; + stream.close(); + } + } + + static final class RandomFasta { + + static final Freq[] lookup=new Freq[LOOKUP_SIZE]; + + static void makeLookup(Freq[] a) { + for (int i = 0, j = 0; i < LOOKUP_SIZE; i++) { + while (a[j].p < i) j++; + lookup[i] = a[j]; + } + } + + static void addLine(int bytes) throws IOException{ + Out.checkFlush(); + int lct=Out.ct; + while(lct<Out.ct+bytes){ + double r = Random.next(); // Problematic for AOT! See fastaredux_revised.java. + int ai = (int) r; while (lookup[ai].p < r) ai++; + Out.buf[lct++] = lookup[ai].c; + } + Out.buf[lct++] = (byte)'\n'; + Out.ct=lct; + } + + static void make(String desc, Freq[] a, int n) throws IOException { + makeLookup(a); + + System.arraycopy(desc.getBytes(), 0, Out.buf, Out.ct, desc.length()); + Out.ct+=desc.length(); + + while (n > 0) { + int bytes = Math.min(LINE_LENGTH, n); + addLine(bytes); + n -= bytes; + } + } + } + + static final class RepeatFasta { + + static void make(String desc, byte[] alu, int n) throws IOException { + System.arraycopy(desc.getBytes(), 0, Out.buf, Out.ct, desc.length()); + Out.ct+=desc.length(); + + byte buf[] = new byte[alu.length + LINE_LENGTH]; + for (int i = 0; i < buf.length; i += alu.length) + System.arraycopy(alu, 0, buf, i, Math.min(alu.length, buf.length - i)); + + int pos = 0; + while (n > 0) { + int bytes = Math.min(LINE_LENGTH, n); + Out.checkFlush(); + System.arraycopy(buf, pos, Out.buf, Out.ct, bytes); Out.ct+=bytes; + Out.buf[Out.ct++] = (byte)'\n'; + pos = (pos + bytes) % alu.length; + n -= bytes; + } + } + } + + private void old_main() throws IOException { + int n = 1000; + + RepeatFasta.make(">ONE Homo sapiens alu\n", ALU.getBytes(), n * 2); + RandomFasta.make(">TWO IUB ambiguity codes\n", IUB, n * 3); + RandomFasta.make(">THREE Homo sapiens frequency\n", HomoSapiens, n * 5); + } + // CHECKSTYLE.ON: .* + + /** Writes to nowhere */ + public class NullOutputStream extends OutputStream { + @Override + public void write(int b) throws IOException { + } + } + + @Setup + public void setup() { + sumAndScale(IUB); + sumAndScale(HomoSapiens); + + Out.stream = new NullOutputStream(); + } + + @Benchmark + public void jmhTimeFastaRedux() throws IOException { + old_main(); + } +} |