diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-06-08 04:25:26 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-06-08 04:25:26 -0400 |
commit | 5bfffe1756203cf89030072313057d8583163b4e (patch) | |
tree | 5323e7960bc6c57b4129b780f2932589c630fdd0 | |
parent | a8464ae0c913f0205aae3e60eb28f46f34eb4f21 (diff) | |
download | getwtxt-5bfffe1756203cf89030072313057d8583163b4e.tar.gz |
compositeStatusQuery() - executing queries concurrently
-rw-r--r-- | svc/query.go | 32 | ||||
-rw-r--r-- | svc/query_test.go | 56 |
2 files changed, 81 insertions, 7 deletions
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) + } + +} |