public void can_compile_prelude_million_times()
 {
     _logger = s =>
     {
         if (!s.StartsWith("P:"))
         {
             _logged.Add(s);
         }
         else
         {
             _logDelegate(s);
         }
     };
     _projection = @"
         fromAll();
         on_raw(function(state, event, streamId, eventType, sequenceNumber, metadata) {
             emit('output-stream' + sequenceNumber, 'emitted-event' + sequenceNumber, {a: JSON.parse(event).a});
             return {};
         });
     ";
     for (var i = 0; i < 10000000; i++)
     {
         _logged = new List <string>();
         var preludeSource = DefaultV8ProjectionStateHandler.GetModuleSource("1Prelude");
         using (
             var prelude = new PreludeScript(
                 preludeSource.Item1, preludeSource.Item2, DefaultV8ProjectionStateHandler.GetModuleSource,
                 _logger))
         {
         }
     }
 }
Пример #2
0
        private static void Main(string[] args)
        {
            var preludeFileName = args[0];
            var preludeScript   = File.ReadAllText(preludeFileName);
            var queryFileName   = args[1];
            var queryScript     = File.ReadAllText(queryFileName);

            Func <string, Tuple <string, string> > loadModule =
                moduleName =>
            {
                var moduleFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, moduleName + ".js");
                return
                    (Tuple.Create(
                         File.ReadAllText(moduleFilename),
                         moduleFilename));
            };

            using (var prelude = new PreludeScript(preludeScript, preludeFileName, loadModule))
                using (var query = new QueryScript(prelude, queryScript, queryFileName))
                    using (var events = File.OpenText(args[2]))
                        using (var output = (args.Length >= 4) ? File.CreateText(args[3]) : Console.Out)
                        {
                            long totalMs = 0;
                            int  count   = 0;
                            query.Initialize();
                            if (output != null)
                            {
                                var capturedOutput = output;
                                query.Emit += s => capturedOutput.WriteLine(s.Trim());
                            }
                            var sw = new Stopwatch();
                            while (!events.EndOfStream)
                            {
                                var eventJson = events.ReadLine().Trim();
                                if (!string.IsNullOrWhiteSpace(eventJson))
                                {
                                    sw.Start();
                                    query.Push(eventJson, null);
                                    count++;
                                    sw.Stop();
                                }
                            }
                            totalMs = sw.ElapsedMilliseconds;
                            Console.WriteLine(query.GetState());
                            Console.WriteLine(query.GetStatistics());
                            Console.WriteLine("Total JS push processing time: {0,5:f2} ms", totalMs);
                            Console.WriteLine("Average time per 1000 pushes:  {0,5:f2} ms", 1000f * totalMs / (float)count);
                            Console.WriteLine("Pure JS events per second:     {0,5:f2} events", count * 1000f / totalMs);
                            Console.WriteLine("Total events processed:        {0} events", count);
                        }
        }
Пример #3
0
        public void long_execution_of_non_v8_code_does_not_crash()
        {
            Assert.Throws <Js1Exception>(() => {
                _cancelCallbackFactory = (timeout, action) => ThreadPool.QueueUserWorkItem(state =>
                {
                    Console.WriteLine("Calling a callback in " + timeout + "ms");
                    Thread.Sleep(timeout);
                    action();
                });
                Action <string, object[]> logger = (m, _) => Console.WriteLine(m);


                Func <string, Tuple <string, string> > getModuleSource = name =>
                {
                    var fullScriptFileName = Path.GetFullPath(Path.Combine(_jsPath, name + ".js"));
                    var scriptSource       = File.ReadAllText(fullScriptFileName, Helper.UTF8NoBom);
                    return(Tuple.Create(scriptSource, fullScriptFileName));
                };


                var preludeSource = getModuleSource("1Prelude");
                var prelude       = new PreludeScript(preludeSource.Item1, preludeSource.Item2, getModuleSource, _cancelCallbackFactory, logger);
                try
                {
                    //var cancelToken = 123;
                    prelude.ScheduleTerminateExecution();
                    Thread.Sleep(500);
                    _commandHandlerRegisteredCallback = (name, handle) => { };
                    _reverseCommandHandlerDelegate    = (name, body) => { };
                    Js1.CompileQuery(
                        prelude.GetHandle(), "log(1);", "fn", _commandHandlerRegisteredCallback,
                        _reverseCommandHandlerDelegate);

                    prelude.CancelTerminateExecution();
                }
                catch
                {
                    prelude.Dispose(); // clean up unmanaged resources if failed to create
                    throw;
                }
            });
        }
Пример #4
0
        public V8ProjectionStateHandler(
            string preludeName, string querySource, Func <string, Tuple <string, string> > getModuleSource,
            Action <string, object[]> logger, Action <int, Action> cancelCallbackFactory)
        {
            var preludeSource = getModuleSource(preludeName);
            var prelude       = new PreludeScript(preludeSource.Item1, preludeSource.Item2, getModuleSource,
                                                  cancelCallbackFactory, logger);
            QueryScript query;

            try {
                query       = new QueryScript(prelude, querySource, "POST-BODY");
                query.Emit += QueryOnEmit;
            } catch {
                prelude.Dispose();                 // clean up unmanaged resources if failed to create
                throw;
            }

            _prelude = prelude;
            _query   = query;
        }