Package com.higherfrequencytrading.chronicle.impl

Examples of com.higherfrequencytrading.chronicle.impl.IndexedChronicle


    private static final String TMP = System.getProperty("java.io.tmpdir");

    public static void main(String[] args) throws IOException {
        String basePath = TMP + "/DataStorePerfTest";
        ChronicleTools.deleteOnExit(basePath);
        IndexedChronicle chronicle = new IndexedChronicle(basePath);
        chronicle.useUnsafe(true);
        DataStore dataStore = new DataStore(chronicle, ModelMode.MASTER);
        MapWrapper<String, byte[]> map = new MapWrapper<String, byte[]>(dataStore, "map",
                String.class, byte[].class, new LinkedHashMap<String, byte[]>(), 160);
        dataStore.add("map", map);
        dataStore.start();

        // generate keys and values
        int keyCount = 100000;
        String[] keys = new String[keyCount];
        byte[][] bytes = new byte[keyCount][];
        Random rand = new Random();
        for (int i = 0; i < keyCount; i++) {
            StringBuilder key = new StringBuilder();
            do {
                key.append(Integer.toString(rand.nextInt() & Integer.MAX_VALUE, 36));
            } while (key.length() < 16);
            key.setLength(16);
            keys[i] = key.toString();
            bytes[i] = new byte[100];
        }
        Collections.sort(Arrays.asList(keys));

        for (int t = 0; t < 10; t++) {
            map.clear();

            long time0 = System.nanoTime();
            // Start with sequential writes
            for (int i = 0; i < keyCount; i++) {
                map.put(keys[i], bytes[i]);
            }
            long time1 = System.nanoTime();
            // Sequential reads
            for (int i = 0; i < keyCount; i++) {
                byte[] bytes0 = map.get(keys[i]);
            }
            long time2 = System.nanoTime();
            String[] keys2 = keys.clone();
            Collections.shuffle(Arrays.asList(keys2));
            long time3 = System.nanoTime();
            // random writes
            for (int i = 0; i < keyCount; i++) {
                map.put(keys[i], bytes[i]);
            }
            long time4 = System.nanoTime();
            // random reads
            for (int i = 0; i < keyCount; i++) {
                byte[] bytes0 = map.get(keys[i]);
            }
            long time5 = System.nanoTime();
            System.out.printf("Seq write: %,d K/s, Seq read: %,d K/s, Rnd write: %,d K/s, Rnd read: %,d K/s%n",
                    keyCount * 1000000L / (time1 - time0),
                    keyCount * 1000000L / (time2 - time1),
                    keyCount * 1000000L / (time4 - time3),
                    keyCount * 1000000L / (time5 - time4)
            );
        }
        chronicle.close();
    }
View Full Code Here


        //Write
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final IndexedChronicle chronicle = new IndexedChronicle(basePath);
                    chronicle.useUnsafe(true); // for benchmarks.
                    final Excerpt excerpt = chronicle.createExcerpt();
                    for (int i = -warmup; i < repeats; i++) {
                        doSomeThinking();
                        excerpt.startExcerpt(8 + 4 + 4 * consolidates.length);
                        excerpt.writeLong(System.nanoTime());
                        excerpt.writeUnsignedShort(consolidates.length);
                        for (final int consolidate : consolidates) {
                            excerpt.writeStopBit(consolidate);
                        }
                        excerpt.finish();
                    }
                    chronicle.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            private void doSomeThinking() {
                // real programs do some work between messages
                // this has an impact on the worst case latencies.
                Thread.yield();
            }
        });
        t.start();
        //Read
        final IndexedChronicle chronicle = new IndexedChronicle(basePath);
        chronicle.useUnsafe(true); // for benchmarks.
        final Excerpt excerpt = chronicle.createExcerpt();
        int[] times = new int[repeats];
        for (int count = -warmup; count < repeats; count++) {
            do {
            /* busy wait */
            } while (!excerpt.nextIndex());
            final long timestamp = excerpt.readLong();
            long time = System.nanoTime() - timestamp;
            if (count >= 0)
                times[count] = (int) time;
            final int nbConsolidates = excerpt.readUnsignedShort();
            assert nbConsolidates == consolidates.length;
            for (int i = 0; i < nbConsolidates; i++) {
                excerpt.readStopBit();
            }
            excerpt.finish();
        }
        Arrays.sort(times);
        for (double perc : new double[]{50, 90, 99, 99.9, 99.99}) {
            System.out.printf("%s%% took %.2f µs, ", perc, times[((int) (repeats * perc / 100))] / 1000.0);
        }
        System.out.printf("worst took %d µs%n", times[times.length - 1] / 1000);
        chronicle.close();
    }
View Full Code Here

            if (path.startsWith("/."))
                path = path.substring(1);
            final Chronicle chronicle;
            switch (mode) {
                case MASTER:
                    chronicle = new InProcessChronicleSource(new IndexedChronicle(path), uri.getPort());
                    break;

                case READ_ONLY:
                    chronicle = new InProcessChronicleSink(new IndexedChronicle(path + "/" + nodeName), uri.getHost(), uri.getPort());
                    break;

                default:
                    throw new AssertionError("Unknown ModelMode " + mode);
            }
View Full Code Here

TOP

Related Classes of com.higherfrequencytrading.chronicle.impl.IndexedChronicle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.