From 254eec529e3955aeb794c92b4c548f992bdbde1f Mon Sep 17 00:00:00 2001 From: Biarity Date: Wed, 4 Jul 2018 11:26:49 +1000 Subject: [PATCH] Check for duplicate filteers/sorts and performance measurement code for SieveTests --- Sieve/Models/SieveModel.cs | 15 +++++++++++-- .../Services/SieveCustomFilterMethods.cs | 2 +- SieveTests/Startup.cs | 21 ++++++++++++++++++- SieveTests/pyprofile.py | 15 +++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 SieveTests/pyprofile.py diff --git a/Sieve/Models/SieveModel.cs b/Sieve/Models/SieveModel.cs index 1094ecc..7c912a0 100644 --- a/Sieve/Models/SieveModel.cs +++ b/Sieve/Models/SieveModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Runtime.Serialization; namespace Sieve.Models @@ -33,6 +34,8 @@ namespace Sieve.Models var value = new List(); foreach (var filter in Filters.Split(',')) { + if (string.IsNullOrWhiteSpace(filter)) continue; + if (filter.StartsWith("(")) { var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1); @@ -41,7 +44,10 @@ namespace Sieve.Models { Filter = subfilters + filterOpAndVal }; - value.Add(filterTerm); + if (!value.Any(f => f.Names.Any(n => filterTerm.Names.Any(n2 => n2 == n)))) + { + value.Add(filterTerm); + } } else { @@ -70,11 +76,16 @@ namespace Sieve.Models var value = new List(); foreach (var sort in Sorts.Split(',')) { + if (string.IsNullOrWhiteSpace(sort)) continue; + var sortTerm = new TSortTerm() { Sort = sort }; - value.Add(sortTerm); + if (!value.Any(s => s.Name == sortTerm.Name)) + { + value.Add(sortTerm); + } } return value; } diff --git a/SieveTests/Services/SieveCustomFilterMethods.cs b/SieveTests/Services/SieveCustomFilterMethods.cs index cf620a6..0111402 100644 --- a/SieveTests/Services/SieveCustomFilterMethods.cs +++ b/SieveTests/Services/SieveCustomFilterMethods.cs @@ -6,7 +6,7 @@ namespace SieveTests.Services { public class SieveCustomFilterMethods : ISieveCustomFilterMethods { - public IQueryable IsNew(IQueryable source) + public IQueryable IsNew(IQueryable source, string op, string value) => source.Where(p => p.LikeCount < 100 && p.CommentCount < 5); } } diff --git a/SieveTests/Startup.cs b/SieveTests/Startup.cs index 2124d5b..c5b9e3e 100644 --- a/SieveTests/Startup.cs +++ b/SieveTests/Startup.cs @@ -1,5 +1,10 @@ -using Microsoft.AspNetCore.Builder; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -37,6 +42,20 @@ namespace SieveTests // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { + // TIME MEASUREMENT + var times = new List(); + app.Use(async (context, next) => + { + var sw = new Stopwatch(); + sw.Start(); + await next.Invoke(); + sw.Stop(); + times.Add(sw.ElapsedMilliseconds); + var text = $"AVG: {(int)times.Average()}ms; AT {sw.ElapsedMilliseconds}; COUNT: {times.Count()}"; + Console.WriteLine(text); + await context.Response.WriteAsync($""); + }); + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); diff --git a/SieveTests/pyprofile.py b/SieveTests/pyprofile.py new file mode 100644 index 0000000..ab42a6e --- /dev/null +++ b/SieveTests/pyprofile.py @@ -0,0 +1,15 @@ +import urllib.request +import time + +for run in range(500): + contents = urllib.request.urlopen("http://localhost:6500/api/posts/getall").read() + print(contents[-50:]) + + +#for run in range(50): +# contents = urllib.request.urlopen("http://localhost:6500/api/posts/getallWithSieve?filters=IsNew&sorts=popularity").read() +# print(contents[-50:]) +# time.sleep(1) + +print("done baseline") +input()