Package com.higherfrequencytrading.chronicle.tcp

Examples of com.higherfrequencytrading.chronicle.tcp.InProcessChronicleSource


            }
        }, "central broker");
        cbt.start();

        Chronicle chronicle3 = new IndexedChronicle(basePath3);
        InProcessChronicleSource source3 = new InProcessChronicleSource(chronicle3, PORT3);
        source3.busyWaitTimeNS(2 * 1000 * 1000);
        EventsWriter writer3 = new EventsWriter(source3);

        Chronicle chronicle2r = new IndexedChronicle(basePath2);
        final EventsReader reader2 = new EventsReader(chronicle2r.createExcerpt(),
                new BrokerEvents(writer3), TimingStage.EngineRead, TimingStage.SinkWrite);
View Full Code Here


    public static void main(String... ignored) throws IOException, InterruptedException {
        String basePath = TMP + "/1-source";
        ChronicleTools.deleteOnExit(basePath);

        Chronicle chronicle = new IndexedChronicle(basePath);
        InProcessChronicleSource source = new InProcessChronicleSource(chronicle, PORT);
        source.busyWaitTimeNS(2 * 1000 * 1000);

        EventsWriter writer = new EventsWriter(source);

        Update update = new Update();
        System.out.println("Allowing connection.");
        Thread.sleep(1000);
        System.out.println("Warming up.");

        for (int i = -WARMUP; i <= MESSAGES; i += RATE) {
            if (i == 0)
                System.out.println("Sending messages.");
            Thread.sleep(1);
            for (int j = 0; j < RATE; j++) {
                update.resetLevels("EUR/USD");
                update.acquireLevel().init(1.3256, 1e6, 1.3257, 2e6);
                update.acquireLevel().init(1.3255, 2e6, 1.3258, 4e6);
                update.acquireLevel().init(1.3254, 3e6, 1.3259, 6e6);
                update.acquireLevel().init(1.3253, 4e6, 1.3260, 8e6);
                update.acquireLevel().init(1.3252, 5e6, 1.3261, 10e6);
                writer.onMarketData(null, update);
                pause(10 * 1000);
            }
        }
        System.out.println("Messages written");
        Thread.sleep(1000);
        source.close();
    }
View Full Code Here

        long start = System.nanoTime();
        int PORT = 12346;
        int size = 0;

        InProcessChronicleSource chronicle = new InProcessChronicleSource(new IndexedChronicle(name), PORT);
        DataStore dataStore = new DataStore(chronicle, ModelMode.MASTER);
        MapWrapper<String, String> strings = new MapWrapper<String, String>(dataStore, "strings", String.class, String.class, new LinkedHashMap<String, String>(), 16);
        MapWrapper<Integer, Integer> ints = new MapWrapper<Integer, Integer>(dataStore, "ints", Integer.class, Integer.class, new LinkedHashMap<Integer, Integer>(), 16);
        dataStore.start();
        ints.clear();
        strings.clear();

        InProcessChronicleSink chronicle2 = new InProcessChronicleSink(new IndexedChronicle(name2), "localhost", PORT);
        DataStore dataStore2 = new DataStore(chronicle2, ModelMode.READ_ONLY);
        MapWrapper<String, String> strings2 = new MapWrapper<String, String>(dataStore2, "strings", String.class, String.class, new LinkedHashMap<String, String>(), 16);
        MapWrapper<Integer, Integer> ints2 = new MapWrapper<Integer, Integer>(dataStore2, "ints", Integer.class, Integer.class, new LinkedHashMap<Integer, Integer>(), 16);

        final AtomicInteger sai = new AtomicInteger();
        MapListener<String, String> stringsListener = new AbstractMapListener<String, String>() {
            @Override
            public void update(String key, String oldValue, String newValue) {
//                System.out.println(key + " " + oldValue + " => " + newValue);
                sai.incrementAndGet();
            }

            @Override
            public void inSync() {
//                System.out.println("inSync");
            }
        };
        strings2.addListener(stringsListener);

        final AtomicInteger iai = new AtomicInteger();
        MapListener<Integer, Integer> intsListener = new AbstractMapListener<Integer, Integer>() {
            @Override
            public void update(Integer key, Integer oldValue, Integer newValue) {
//                System.out.println(key + " " + oldValue + " => " + newValue);
                iai.incrementAndGet();
            }
        };
        ints2.addListener(intsListener);
        dataStore2.start();

        int count = 0;
        for (int j = 0; j < 1000; j++) {
            int collectionSize = 1000;
            for (int i = 0; i < collectionSize; i++) {
                ints.put(i, i + j);
                strings.put(Integer.toString(i), Integer.toString(i + j));
            }
            size += Math.min(strings.size(), ints.size());
            for (int i = 0; i < collectionSize; i++) {
                ints.remove(i);
                strings.remove(Integer.toString(i));
            }
            count += 4 * collectionSize;
        }
        long mid = System.nanoTime();

//        int timeout = 0;
        while (dataStore2.events() < count) {
//            if (timeout++ % 10000 == 0)
//                System.out.println(dataStore2.events());
            Thread.sleep(1);
        }

        long end = System.nanoTime();

        System.out.printf("Startup and write took %.2f us on average and read and shutdown took %.2f on average%n",
                (mid - start) / count / 1e3, (end - mid) / count / 1e3);

//        Thread.sleep(10000);

        chronicle.close();
        chronicle2.close();
    }
View Full Code Here

        ChronicleTools.deleteOnExit(name2);

        long start = System.nanoTime();
        int PORT = 12347;

        InProcessChronicleSource chronicle = new InProcessChronicleSource(new IndexedChronicle(name), PORT);
        DataStore dataStore = new DataStore(chronicle, ModelMode.MASTER);
        MapWrapper<String, String> strings = new MapWrapper<String, String>(dataStore, "strings", String.class, String.class, new LinkedHashMap<String, String>(), 16);
        MapWrapper<Integer, Integer> ints = new MapWrapper<Integer, Integer>(dataStore, "ints", Integer.class, Integer.class, new LinkedHashMap<Integer, Integer>(), 16);
        dataStore.start();
        ints.clear();
        strings.clear();

        InProcessChronicleSink chronicle2 = new InProcessChronicleSink(new IndexedChronicle(name2), "localhost", PORT);
        DataStore dataStore2 = new DataStore(chronicle2, ModelMode.READ_ONLY);
        MapWrapper<String, String> strings2 = new MapWrapper<String, String>(dataStore2, "strings", String.class, String.class, new LinkedHashMap<String, String>(), 16);
        MapWrapper<Integer, Integer> ints2 = new MapWrapper<Integer, Integer>(dataStore2, "ints", Integer.class, Integer.class, new LinkedHashMap<Integer, Integer>(), 16);

        final AtomicInteger sai = new AtomicInteger();
        MapListener<String, String> stringsListener = new AbstractMapListener<String, String>() {
            @Override
            public void update(String key, String oldValue, String newValue) {
//                System.out.println(key + " " + oldValue + " => " + newValue);
                sai.incrementAndGet();
            }
        };
        strings2.addListener(stringsListener);

        final AtomicInteger iai = new AtomicInteger();
        MapListener<Integer, Integer> intsListener = new AbstractMapListener<Integer, Integer>() {
            @Override
            public void update(Integer key, Integer oldValue, Integer newValue) {
//                System.out.println(key + " " + oldValue + " => " + newValue);
                iai.incrementAndGet();
            }
        };
        ints2.addListener(intsListener);
        dataStore2.start();

        Map<String, String> ssMap = new LinkedHashMap<String, String>();
        Map<Integer, Integer> iiMap = new LinkedHashMap<Integer, Integer>();
        int count = 0;
        int collectionSize = 2000;
        for (int i = 0; i < collectionSize; i++) {
            iiMap.put(i, i);
            ssMap.put(Integer.toString(i), Integer.toString(i));
        }
        for (int j = 0; j < 2500; j++) {
            strings.putAll(ssMap);
            ints.putAll(iiMap);
            strings.clear();
            ints.clear();
            count += 4;
        }
        long mid = System.nanoTime();

//        int timeout = 0;
        while (dataStore2.events() < count) {
            Thread.sleep(1);
        }

        long end = System.nanoTime();

        System.out.printf("Startup and write took %.2f us on average (per key) and read and shutdown took %.2f us on average (per key)%n",
                (mid - start) / count / collectionSize / 1e3, (end - mid) / count / collectionSize / 1e3);

        chronicle.close();
//        System.gc();
        chronicle2.close();
    }
View Full Code Here

        ChronicleTools.deleteOnExit(name2);

        long start = System.nanoTime();
        int PORT = 12348;

        InProcessChronicleSource chronicle = new InProcessChronicleSource(new IndexedChronicle(name), PORT);
        DataStore dataStore = new DataStore(chronicle, ModelMode.MASTER);
        MapWrapper<String, String> strings = new MapWrapper<String, String>(dataStore, "strings", String.class, String.class, new LinkedHashMap<String, String>(), 16);
        MapWrapper<Integer, Integer> ints = new MapWrapper<Integer, Integer>(dataStore, "ints", Integer.class, Integer.class, new LinkedHashMap<Integer, Integer>(), 16);
        dataStore.start();
        ints.clear();
        strings.clear();

        InProcessChronicleSink chronicle2 = new InProcessChronicleSink(new IndexedChronicle(name2), "localhost", PORT);
        DataStore dataStore2 = new DataStore(chronicle2, ModelMode.READ_ONLY);
        MapWrapper<String, String> strings2 = new MapWrapper<String, String>(dataStore2, "strings", String.class, String.class, new LinkedHashMap<String, String>(), 16);
        MapWrapper<Integer, Integer> ints2 = new MapWrapper<Integer, Integer>(dataStore2, "ints", Integer.class, Integer.class, new LinkedHashMap<Integer, Integer>(), 16);

        final AtomicInteger sai = new AtomicInteger();
        MapListener<String, String> stringsListener = new AbstractMapListener<String, String>() {
            @Override
            public void update(String key, String oldValue, String newValue) {
//                System.out.println(key + " " + oldValue + " => " + newValue);
                sai.incrementAndGet();
            }
        };
        strings2.addListener(stringsListener);

        final AtomicInteger iai = new AtomicInteger();
        MapListener<Integer, Integer> intsListener = new AbstractMapListener<Integer, Integer>() {
            @Override
            public void update(Integer key, Integer oldValue, Integer newValue) {
//                System.out.println(key + " " + oldValue + " => " + newValue);
                iai.incrementAndGet();
            }
        };
        ints2.addListener(intsListener);
        dataStore2.start();

        Map<String, String> ssMap = new LinkedHashMap<String, String>();
        Map<Integer, Integer> iiMap = new LinkedHashMap<Integer, Integer>();
        int count = 2; // one clear per collection
        int collectionSize = 2000;
        for (int i = 0; i < collectionSize; i++) {
            iiMap.put(i, i);
            ssMap.put(Integer.toString(i), Integer.toString(i));
        }

        strings.putAll(ssMap);
        ints.putAll(iiMap);
        count += 2;

        Thread.sleep(100);
//        int timeout = 0;
        while (dataStore2.events() < count || ints2.size() < collectionSize) {
//            if (timeout++ % 10000 == 0)
//                System.out.println(dataStore2.events());
            Thread.sleep(1);
        }

        assertEquals(collectionSize, strings.size());
        assertEquals(collectionSize, strings2.size());
        assertEquals(collectionSize, ints.size());
        assertEquals(collectionSize, ints2.size());
        System.out.println("=== performing get test ===");
        int gets = 0;
        for (int j = 0; j < 10000; j++) {
            for (String s : ssMap.keySet()) {
                String s1 = strings.get(s);
                String s2 = strings2.get(s);
                if (s1 == null)
                    assertNotNull(s1);
                if (!s1.equals(s2))
                    assertEquals(s1, s2);
            }
            gets += ssMap.size();
            for (Integer i : iiMap.keySet()) {
                Integer i1 = ints.get(i);
                Integer i2 = ints2.get(i);
                if (i1 == null)
                    assertNotNull(i1);
                if (!i1.equals(i2))
                    assertEquals(i1, i2);
            }
            gets += iiMap.size();
        }

        chronicle.close();
        chronicle2.close();
        long end = System.nanoTime();

        System.out.printf("Average get time including startup, bootstrap and shutdown, took %.3f us average per key%n",
                (end - start) / gets / 1e3);
View Full Code Here

    @Test
    public void testTCP() throws IOException, InterruptedException {
        int port = 65432;
        String masterPath = TMP + "/master";
        ChronicleTools.deleteOnExit(masterPath);
        InProcessChronicleSource masterC = new InProcessChronicleSource(new IndexedChronicle(masterPath), port);
        DataStore master = new DataStore(masterC, ModelMode.MASTER);
        ExampleDataModel masterModel = new ExampleDataModel();
        master.inject(masterModel);
        master.start();
View Full Code Here

    static class Repeater implements Runnable {
        private final InProcessChronicleSink chronicle;

        public Repeater(int test, int port, String hostname2, int port2) throws IOException {
            IndexedChronicle store = new IndexedChronicle(DIR + "/repeater-" + test);
            InProcessChronicleSource source = new InProcessChronicleSource(store, port);
            chronicle = new InProcessChronicleSink(source, hostname2, port2);
        }
View Full Code Here

        public Sender(int test, String hostname, int port, int port2) throws IOException {
            IndexedChronicle receiverStore = new IndexedChronicle(DIR + "/receiver-" + test);
            IndexedChronicle senderStore = new IndexedChronicle(DIR + "/sender-" + test);
            sink = new InProcessChronicleSink(receiverStore, hostname, port);
            source = new InProcessChronicleSource(senderStore, port2);
            future = reader.submit(new Reader(sink));
        }
View Full Code Here

        String baseDir = System.getProperty("java.io.tmpdir");
        String srcBasePath = baseDir + "/IPCT.testOverTCP.source";
        ChronicleTools.deleteOnExit(srcBasePath);
        // NOTE: the sink and source must have different chronicle files.
        final int messages = 3000000;
        final Chronicle source = new InProcessChronicleSource(new IndexedChronicle(srcBasePath), PORT + 1);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Excerpt excerpt = source.createExcerpt();
                    for (int i = 1; i <= messages; i++) {
                        // use a size which will cause mis-alignment.
                        excerpt.startExcerpt(9);
                        excerpt.writeLong(i);
                        excerpt.writeByte(i);
                        excerpt.finish();
//                        Thread.sleep(1);
                    }
                    System.out.println(System.currentTimeMillis() + ": Finished writing messages");
                } catch (Exception e) {
                    throw new AssertionError(e);
                }

            }
        });

        String snkBasePath = baseDir + "/IPCT.testOverTCP.sink";
        ChronicleTools.deleteOnExit(snkBasePath);
        Chronicle sink = new InProcessChronicleSink(new IndexedChronicle(snkBasePath), "localhost", PORT + 1);

        long start = System.nanoTime();
        t.start();
        Excerpt excerpt = sink.createExcerpt();
        int count = 0;
        for (int i = 1; i <= messages; i++) {
            while (!excerpt.nextIndex())
                count++;
            long n = excerpt.readLong();
            assertEquals(i, n);
            excerpt.finish();
        }
        sink.close();
        System.out.println("There were " + count + " misses");
        t.join();
        source.close();
        long time = System.nanoTime() - start;
        System.out.printf("Messages per second %,d%n", (int) (messages * 1e9 / time));
    }
View Full Code Here

    @Test
    public void testPricePublishing() throws IOException, InterruptedException {
        String baseDir = System.getProperty("java.io.tmpdir");
        String sourceName = baseDir + "/price.source";
        ChronicleTools.deleteOnExit(sourceName);
        Chronicle source = new InProcessChronicleSource(new IndexedChronicle(sourceName), PORT + 2);
        PriceWriter pw = new PriceWriter(source.createExcerpt());

        String sinkName = baseDir + "/price.sink";
        ChronicleTools.deleteOnExit(sinkName);
        Chronicle sink = new InProcessChronicleSink(new IndexedChronicle(sinkName), "localhost", PORT + 2);

        final AtomicInteger count = new AtomicInteger();
        PriceReader reader = new PriceReader(sink.createExcerpt(), new PriceListener() {
            @Override
            public void onPrice(long timeInMicros, String symbol, double bp, int bq, double ap, int aq) {
                count.incrementAndGet();
            }
        });
        pw.onPrice(1, "symbol", 99.9, 1, 100.1, 2);
        reader.read();

        long start = System.nanoTime();
        int prices = 12000000;
        for (int i = 1; i <= prices; i++) {
            pw.onPrice(i, "symbol", 99.9, i, 100.1, i + 1);
        }

        long mid = System.nanoTime();
        while (count.get() < prices)
            reader.read();

        long end = System.nanoTime();
        System.out.printf("Took an average of %.2f us to write and %.2f us to read%n",
                (mid - start) / prices / 1e3, (end - mid) / prices / 1e3);


        source.close();
        sink.close();
    }
View Full Code Here

TOP

Related Classes of com.higherfrequencytrading.chronicle.tcp.InProcessChronicleSource

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.