Skip to content

Performance

pmartin7 edited this page Aug 22, 2016 · 1 revision

Summary

This page contains notes from performance analysis and opportunities to speed up the code

Data Access

Thought of multi-threading the processing of the response.

Tried this approach on GetAllArtists, using Parallel.ForEach and locking an object to avoid thread collisions when adding artists to the output list.

Turns out that the tax of setting up mutliple thread is not worth, even when getting and processing all artist nodes. Single threaded function turns out to take about 1.03 s, multithreaded function takes about 2 s. (quad core)

Sample multi-threaded code

public List<Artist> GetAllArtists_parallel()
        {
            List<Artist> response = new List<Artist>();

            using (var session = driver.Session())
            {
                try
                {
                    object sync = new Object();

                    //Get All Genres
                    var result = session.Run(
                            "MATCH (a:Artist)"
                          + "RETURN DISTINCT a");

                    //It ***should*** be OK to keep response as a list and use the non thread safe IResult because we are
                    //not modifying anythin in IResult result
                    Parallel.ForEach(result, record =>
                        {
                            lock (sync)
                            {
                                response.Add(Helpers.DeserializeNode(record["a"].As<INode>(), new Artist()));
                            }
                        });
                }
                catch (Exception e) { throw e; }
            }

            return response;
        }

Below the test function

[TestMethod]
        public void GAA_DiffAndPerf()
        {
            try
            {
                DataAccess dal = DataAccess.Instance;
                
                //single threaded foreach
                DateTime start = DateTime.Now;
                List<Artist> response = dal.GetAllArtists();
                DateTime end = DateTime.Now;

                //multi-threaded foreach
                DateTime startp = DateTime.Now;
                List<Artist> responsep = dal.GetAllArtists_parallel();
                DateTime endp = DateTime.Now;

                //display times
                System.Diagnostics.Debug.WriteLine("Single threaded compute time: {0}", end-start);
                System.Diagnostics.Debug.WriteLine("Multi threaded compute time: {0}", endp - startp);

                //compare lists
                Assert.IsTrue( (response.Count == responsep.Count) );
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }

Clone this wiki locally