From 5bfffe1756203cf89030072313057d8583163b4e Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Sat, 8 Jun 2019 04:25:26 -0400 Subject: compositeStatusQuery() - executing queries concurrently --- svc/query.go | 32 ++++++++++++++++++++++++------- svc/query_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) (limited to 'svc') diff --git a/svc/query.go b/svc/query.go index f2696dd..25f0f44 100644 --- a/svc/query.go +++ b/svc/query.go @@ -7,6 +7,7 @@ import ( "net/http" "strconv" "strings" + "sync" "github.com/getwtxt/registry" "github.com/gorilla/mux" @@ -127,18 +128,35 @@ func joinQueryOuts(data ...[]string) []string { } func compositeStatusQuery(query string, r *http.Request) []string { + var wg sync.WaitGroup + var out, out2, out3 []string + var err, err2, err3 error + + wg.Add(3) + query = strings.ToLower(query) - out, err := twtxtCache.QueryInStatus(query) - apiErrCheck(err, r) + go func(query string) { + out, err = twtxtCache.QueryInStatus(query) + wg.Done() + }(query) query = strings.Title(query) - out2, err := twtxtCache.QueryInStatus(query) - apiErrCheck(err, r) + go func(query string) { + out2, err2 = twtxtCache.QueryInStatus(query) + wg.Done() + }(query) query = strings.ToUpper(query) - out3, err := twtxtCache.QueryInStatus(query) + go func(query string) { + out3, err3 = twtxtCache.QueryInStatus(query) + wg.Done() + }(query) + + wg.Wait() + apiErrCheck(err, r) + apiErrCheck(err2, r) + apiErrCheck(err3, r) - final := joinQueryOuts(out, out2, out3) - return final + return joinQueryOuts(out, out2, out3) } diff --git a/svc/query_test.go b/svc/query_test.go index a01de90..477808b 100644 --- a/svc/query_test.go +++ b/svc/query_test.go @@ -73,3 +73,59 @@ func Benchmark_parseQueryOut(b *testing.B) { } } + +func Test_compositeStatusQuery(t *testing.T) { + initTestConf() + statuses, _, err := registry.GetTwtxt("https://gbmor.dev/twtxt.txt") + if err != nil { + t.Errorf("%v\n", err) + } + parsed, err := registry.ParseUserTwtxt(statuses, "gbmor", "https://gbmor.dev/twtxt.txt") + if err != nil { + t.Errorf("%v\n", err) + } + err = twtxtCache.AddUser("gbmor", "https://gbmor.dev/twtxt.txt", "1", net.ParseIP("127.0.0.1"), parsed) + if err != nil { + t.Errorf("%v\n", err) + } + + t.Run("Composite Query Test", func(t *testing.T) { + out1, err := twtxtCache.QueryInStatus("sqlite") + if err != nil { + t.Errorf("%v\n", err) + } + out2, err := twtxtCache.QueryInStatus("Sqlite") + if err != nil { + t.Errorf("%v\n", err) + } + out3, err := twtxtCache.QueryInStatus("SQLITE") + if err != nil { + t.Errorf("%v\n", err) + } + + outro := make([]string, 0) + outro = append(outro, out1...) + outro = append(outro, out2...) + outro = append(outro, out3...) + out := dedupe(outro) + + data := compositeStatusQuery("sqlite", nil) + + if !reflect.DeepEqual(out, data) { + t.Errorf("Returning different data.\nManual: %v\nCompositeQuery: %v\n", out, data) + } + }) +} + +func Benchmark_compositeStatusQuery(b *testing.B) { + initTestConf() + statuses, _, _ := registry.GetTwtxt("https://gbmor.dev/twtxt.txt") + parsed, _ := registry.ParseUserTwtxt(statuses, "gbmor", "https://gbmor.dev/twtxt.txt") + _ = twtxtCache.AddUser("gbmor", "https://gbmor.dev/twtxt.txt", "1", net.ParseIP("127.0.0.1"), parsed) + b.ResetTimer() + + for i := 0; i < b.N; i++ { + compositeStatusQuery("sqlite", nil) + } + +} -- cgit 1.4.1-2-gfad0