Ejemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldPerform500ConcurrentRequests() throws InterruptedException, java.util.concurrent.ExecutionException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual void shouldPerform500ConcurrentRequests()
        {
            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.apache.http.impl.client.CloseableHttpClient httpClient = org.apache.http.impl.client.HttpClients.custom().setConnectionManager(cm).build();
            CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

            Callable <string> performRequest = new CallableAnonymousInnerClass(this, httpClient);

            int             requestsCount = 500;
            ExecutorService service       = Executors.newFixedThreadPool(requestsCount);

            IList <Callable <string> > requests = new List <Callable <string> >();

            for (int i = 0; i < requestsCount; i++)
            {
                requests.Add(performRequest);
            }

            IList <Future <string> > futures = service.invokeAll(requests);

            service.shutdown();
            service.awaitTermination(1, TimeUnit.HOURS);

            foreach (Future <string> future in futures)
            {
                assertEquals(future.get(), "[]");
            }
        }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void testConcurrentIsUserAuthorized() throws Exception
        public virtual void testConcurrentIsUserAuthorized()
        {
            int             threadCount     = 2;
            int             invocationCount = 500;
            ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

            try
            {
                List <Callable <Exception> > callables = new List <Callable <Exception> >();

                for (int i = 0; i < invocationCount; i++)
                {
                    callables.Add(new CallableAnonymousInnerClass(this));
                }

                IList <Future <Exception> > futures = executorService.invokeAll(callables);

                foreach (Future <Exception> future in futures)
                {
                    Exception exception = future.get();
                    if (exception != null)
                    {
                        fail("No exception expected: " + exception.Message);
                    }
                }
            }
            finally
            {
                // reset original logging level
                executorService.shutdownNow();
                executorService.awaitTermination(10, TimeUnit.SECONDS);
            }
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(timeout = TIMEOUT_MS) public void shouldDiscoverCompleteTargetSetWithoutDeadlocks() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldDiscoverCompleteTargetSetWithoutDeadlocks()
        {
            // given
            ExecutorService es = Executors.newCachedThreadPool();

            long endTimeMillis = DateTimeHelper.CurrentUnixTimeMillis() + RUN_TIME_MS;

            while (endTimeMillis > DateTimeHelper.CurrentUnixTimeMillis())
            {
                ISet <MemberId> members = new HashSet <MemberId>();
                for (int i = 0; i < 3; i++)
                {
                    members.Add(new MemberId(System.Guid.randomUUID()));
                }

                DiscoveryServiceFactory sharedService = new SharedDiscoveryServiceFactory();

                IList <Callable <Void> > discoveryJobs = new List <Callable <Void> >();
                foreach (MemberId member in members)
                {
                    discoveryJobs.Add(CreateDiscoveryJob(member, sharedService, members));
                }

                IList <Future <Void> > results = es.invokeAll(discoveryJobs);
                foreach (Future <Void> result in results)
                {
                    result.get(TIMEOUT_MS, MILLISECONDS);
                }
            }
        }
Ejemplo n.º 4
0
        public static float[] getMedianErrorRates(LangDescriptor language, int maxNumFiles, int trials)
        {
            SubsetValidator       validator = new SubsetValidator(language.corpusDir, language);
            IList <InputDocument> documents = Tool.load(validator.allFiles, language);

            float[] medians = new float[Math.Min(documents.Count, maxNumFiles) + 1];

            int ncpu = Runtime.Runtime.availableProcessors();

            if (FORCE_SINGLE_THREADED)
            {
                ncpu = 2;
            }
            ExecutorService          pool = Executors.newFixedThreadPool(ncpu - 1);
            IList <Callable <Void> > jobs = new List <Callable <Void> >();

            for (int i = 1; i <= Math.Min(validator.allFiles.Count, maxNumFiles); i++)
            {             // i is corpus subset size
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int corpusSubsetSize = i;
                int             corpusSubsetSize = i;
                Callable <Void> job = () =>
                {
                    try
                    {
                        IList <float?> errorRates = new List <float?>();
                        for (int trial = 1; trial <= trials; trial++)
                        {                 // multiple trials per subset size
                            org.antlr.codebuff.misc.Pair <InputDocument, IList <InputDocument> > sample = validator.selectSample(documents, corpusSubsetSize);
                            Triple <Formatter, float?, float?> results = validate(language, sample.b, sample.a, true, false);
//					System.out.println(sample.a.fileName+" n="+corpusSubsetSize+": error="+results.c);
//				System.out.println("\tcorpus =\n\t\t"+Utils.join(sample.b.iterator(), "\n\t\t"));
                            errorRates.Add(results.c);
                        }
                        errorRates.Sort();
                        int   n      = errorRates.Count;
                        float median = errorRates[n / 2].Value;
                        Console.WriteLine("median " + language.name + " error rate for n=" + corpusSubsetSize + " is " + median);
                        medians[corpusSubsetSize] = median;
                    }
                    catch (Exception t)
                    {
                        t.printStackTrace(System.err);
                    }
                    return(null);
                };
                jobs.Add(job);
            }

            pool.invokeAll(jobs);
            pool.shutdown();
            bool terminated = pool.awaitTermination(60, TimeUnit.MINUTES);

            return(medians);
        }
Ejemplo n.º 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldBeConsistentAfterConcurrentWritesAndForces() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldBeConsistentAfterConcurrentWritesAndForces()
        {
            ExecutorService executorService = Executors.newCachedThreadPool();

            try
            {
                for (int attempt = 0; attempt < 100; attempt++)
                {
                    using (EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction())
                    {
                        File aFile = new File("contendedFile");

                        ICollection <Callable <Void> > workers = new List <Callable <Void> >();
                        for (int i = 0; i < 100; i++)
                        {
                            workers.Add(() =>
                            {
                                try
                                {
                                    StoreChannel channel = fs.Open(aFile, OpenMode.READ_WRITE);
                                    channel.position(channel.size());
                                    WriteLong(channel, 1);
                                }
                                catch (IOException e)
                                {
                                    throw new Exception(e);
                                }
                                return(null);
                            });

                            workers.Add(() =>
                            {
                                StoreChannel channel = fs.Open(aFile, OpenMode.READ_WRITE);
                                channel.force(true);
                                return(null);
                            });
                        }

                        IList <Future <Void> > futures = executorService.invokeAll(workers);
                        foreach (Future <Void> future in futures)
                        {
                            future.get();
                        }

                        fs.Crash();
                        VerifyFileIsFullOfLongIntegerOnes(fs.Open(aFile, OpenMode.READ_WRITE));
                    }
                }
            }
            finally
            {
                executorService.shutdown();
            }
        }
Ejemplo n.º 6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldWorkWhileHavingHeavyConcurrentUpdates() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldWorkWhileHavingHeavyConcurrentUpdates()
        {
            // given some initial data
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long[] nodes = repeatCreateNamedPeopleFor(NAMES.length * CREATION_MULTIPLIER);
            long[] nodes        = RepeatCreateNamedPeopleFor(_names.Length * _creationMultiplier);
            int    initialNodes = nodes.Length;
            int    threads      = 5;

            _indexOnlineMonitor.initialize(threads);
            ExecutorService executorService = Executors.newFixedThreadPool(threads);

            // when populating while creating
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.internal.kernel.api.IndexReference index = createPersonNameIndex();
            IndexReference index = CreatePersonNameIndex();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.Collection<java.util.concurrent.Callable<UpdatesTracker>> jobs = new java.util.ArrayList<>(threads);
            ICollection <Callable <UpdatesTracker> > jobs = new List <Callable <UpdatesTracker> >(threads);

            for (int i = 0; i < threads; i++)
            {
                jobs.Add(() => ExecuteCreationsDeletionsAndUpdates(nodes, _creationMultiplier));
            }

            IList <Future <UpdatesTracker> > futures = executorService.invokeAll(jobs);
            // sum result into empty result
            UpdatesTracker result = new UpdatesTracker();

            result.NotifyPopulationCompleted();
            foreach (Future <UpdatesTracker> future in futures)
            {
                result.Add(future.get());
            }
            AwaitIndexesOnline();

            executorService.shutdown();
            assertTrue(executorService.awaitTermination(1, TimeUnit.MINUTES));

            // then
            AssertIndexedNodesMatchesStoreNodes();
            int    seenWhilePopulating = initialNodes + result.CreatedDuringPopulation() - result.DeletedDuringPopulation();
            double expectedSelectivity = UNIQUE_NAMES / seenWhilePopulating;

            AssertCorrectIndexSelectivity(expectedSelectivity, IndexSelectivity(index));
            AssertCorrectIndexSize("Tracker had " + result, seenWhilePopulating, IndexSize(index));
            int expectedIndexUpdates = result.DeletedAfterPopulation() + result.CreatedAfterPopulation() + result.UpdatedAfterPopulation();

            AssertCorrectIndexUpdates("Tracker had " + result, expectedIndexUpdates, IndexUpdates(index));
        }
Ejemplo n.º 7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void execute(java.util.List<RecordStresser> recordStressers) throws InterruptedException, java.util.concurrent.ExecutionException
        private void Execute(IList <RecordStresser> recordStressers)
        {
            ExecutorService executorService = Executors.newFixedThreadPool(_numberOfThreads, r =>
            {
                Thread thread = Executors.defaultThreadFactory().newThread(r);
                thread.Daemon = true;
                return(thread);
            });
            IList <Future <Void> > futures = executorService.invokeAll(recordStressers);

            foreach (Future <Void> future in futures)
            {
                future.get();
            }
            executorService.shutdown();
            assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
        }
Ejemplo n.º 8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRunSimpleStatement() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRunSimpleStatement()
        {
            // Given
            int numWorkers  = 5;
            int numRequests = 1_000;

            IList <Callable <Void> > workers = CreateWorkers(numWorkers, numRequests);
            ExecutorService          exec    = Executors.newFixedThreadPool(numWorkers);

            try
            {
                // When & then
                foreach (Future <Void> f in exec.invokeAll(workers))
                {
                    f.get(60, TimeUnit.SECONDS);
                }
            }
            finally
            {
                exec.shutdownNow();
                exec.awaitTermination(30, TimeUnit.SECONDS);
            }
        }
Ejemplo n.º 9
0
        public static void Main(string[] args)
        {
            LangDescriptor[] languages = new LangDescriptor[] { Tool.ANTLR4_DESCR };

            int          MAX_K     = 98; // should be odd
            int          OUTLIER_K = 99;
            IList <int?> ks        = new List <int?>();

            for (int i = 1; i <= MAX_K; i += 2)
            {
                ks.Add(i);
            }
            ks.Add(OUTLIER_K);
            // track medians[language][k]
            float[][] medians = new float[languages.Length + 1][];

            int ncpu = 1;

            if (FORCE_SINGLE_THREADED)
            {
                ncpu = 2;
            }
            ExecutorService          pool = Executors.newFixedThreadPool(ncpu - 1);
            IList <Callable <Void> > jobs = new List <Callable <Void> >();

            for (int i = 0; i < languages.Length; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.antlr.codebuff.misc.LangDescriptor language = languages[i];
                LangDescriptor language = languages[i];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int langIndex = i;
                int langIndex = i;
                Log.WriteLine(language.name);
                foreach (int k in ks)
                {
                    medians[langIndex] = new float?[OUTLIER_K + 1];
                    Callable <Void> job = () =>
                    {
                        try
                        {
                            TestK          tester     = new TestK(language.corpusDir, language, k);
                            IList <float?> errorRates = tester.scoreDocuments();
                            errorRates.Sort();
                            int   n      = errorRates.Count;
                            float median = errorRates[n / 2].Value;
//						double var = BuffUtils.varianceFloats(errorRates);
//						String display = String.format("%5.4f, %5.4f, %5.4f, %5.4f, %5.4f", min, quart, median, quart3, max);
                            medians[langIndex][k] = median;
                        }
                        catch (Exception t)
                        {
                            t.printStackTrace(System.err);
                        }
                        return(null);
                    };
                    jobs.Add(job);
                }
            }

            pool.invokeAll(jobs);
            pool.shutdown();
            bool terminated = pool.awaitTermination(60, TimeUnit.MINUTES);

            writePython(languages, ks, medians);
        }
Ejemplo n.º 10
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long[] repeatCreateNamedPeopleFor(int totalNumberOfPeople) throws Exception
        private long[] RepeatCreateNamedPeopleFor(int totalNumberOfPeople)
        {
            // Parallelize the creation of persons
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long[] nodes = new long[totalNumberOfPeople];
            long[]    nodes   = new long[totalNumberOfPeople];
            const int threads = 100;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int peoplePerThread = totalNumberOfPeople / threads;
            int peoplePerThread = totalNumberOfPeople / threads;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.ExecutorService service = java.util.concurrent.Executors.newFixedThreadPool(threads);
            ExecutorService service = Executors.newFixedThreadPool(threads);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicReference<org.neo4j.internal.kernel.api.exceptions.KernelException> exception = new java.util.concurrent.atomic.AtomicReference<>();
            AtomicReference <KernelException> exception = new AtomicReference <KernelException>();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.List<java.util.concurrent.Callable<Void>> jobs = new java.util.ArrayList<>(threads);
            IList <Callable <Void> > jobs = new List <Callable <Void> >(threads);

            // Start threads that creates these people, relying on batched writes to speed things up
            for (int i = 0; i < threads; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int finalI = i;
                int finalI = i;

                jobs.Add(() =>
                {
                    int offset = finalI * peoplePerThread;
                    while (offset < (finalI + 1) * peoplePerThread)
                    {
                        try
                        {
                            offset += CreateNamedPeople(nodes, offset);
                        }
                        catch (KernelException e)
                        {
                            exception.compareAndSet(null, e);
                            throw new Exception(e);
                        }
                    }
                    return(null);
                });
            }

//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: for (java.util.concurrent.Future<?> job : service.invokeAll(jobs))
            foreach (Future <object> job in service.invokeAll(jobs))
            {
                job.get();
            }

            service.awaitTermination(1, TimeUnit.SECONDS);
            service.shutdown();

            // Make any KernelException thrown from a creation thread visible in the main thread
            Exception ex = exception.get();

            if (ex != null)
            {
                throw ex;
            }

            return(nodes);
        }