Check for duplicate filteers/sorts and performance measurement code for SieveTests

This commit is contained in:
Biarity 2018-07-04 11:26:49 +10:00
parent c72175bf4a
commit 254eec529e
4 changed files with 49 additions and 4 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace Sieve.Models namespace Sieve.Models
@ -33,6 +34,8 @@ namespace Sieve.Models
var value = new List<TFilterTerm>(); var value = new List<TFilterTerm>();
foreach (var filter in Filters.Split(',')) foreach (var filter in Filters.Split(','))
{ {
if (string.IsNullOrWhiteSpace(filter)) continue;
if (filter.StartsWith("(")) if (filter.StartsWith("("))
{ {
var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1); var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1);
@ -41,8 +44,11 @@ namespace Sieve.Models
{ {
Filter = subfilters + filterOpAndVal Filter = subfilters + filterOpAndVal
}; };
if (!value.Any(f => f.Names.Any(n => filterTerm.Names.Any(n2 => n2 == n))))
{
value.Add(filterTerm); value.Add(filterTerm);
} }
}
else else
{ {
var filterTerm = new TFilterTerm var filterTerm = new TFilterTerm
@ -70,12 +76,17 @@ namespace Sieve.Models
var value = new List<TSortTerm>(); var value = new List<TSortTerm>();
foreach (var sort in Sorts.Split(',')) foreach (var sort in Sorts.Split(','))
{ {
if (string.IsNullOrWhiteSpace(sort)) continue;
var sortTerm = new TSortTerm() var sortTerm = new TSortTerm()
{ {
Sort = sort Sort = sort
}; };
if (!value.Any(s => s.Name == sortTerm.Name))
{
value.Add(sortTerm); value.Add(sortTerm);
} }
}
return value; return value;
} }
else else

View File

@ -6,7 +6,7 @@ namespace SieveTests.Services
{ {
public class SieveCustomFilterMethods : ISieveCustomFilterMethods public class SieveCustomFilterMethods : ISieveCustomFilterMethods
{ {
public IQueryable<Post> IsNew(IQueryable<Post> source) public IQueryable<Post> IsNew(IQueryable<Post> source, string op, string value)
=> source.Where(p => p.LikeCount < 100 && p.CommentCount < 5); => source.Where(p => p.LikeCount < 100 && p.CommentCount < 5);
} }
} }

View File

@ -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.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; 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. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ {
// TIME MEASUREMENT
var times = new List<long>();
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($"<!-- {text} -->");
});
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();

15
SieveTests/pyprofile.py Normal file
View File

@ -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()