Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api - Parallelize the dashboard queries #6

Open
mohamedmansour opened this issue Jul 21, 2021 · 1 comment
Open

api - Parallelize the dashboard queries #6

mohamedmansour opened this issue Jul 21, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@mohamedmansour
Copy link
Member

Right now we are sequentially calling each query in the dashboard call. Would be nice to take go to its advantage and run all queries concurrently since they are independent from each other.

@mohamedmansour mohamedmansour added the enhancement New feature or request label Jul 21, 2021
@mohamedmansour
Copy link
Member Author

I tried taking a stab at this, by using channels and waitgroups, don't know if that is right. But I regressed:

Before:

[Wed Jul 21 2021 21:22:34 GMT-0700 (Pacific Daylight Time)] INFO Requests: 1341, requests per second: 269, mean latency: 33 ms
[Wed Jul 21 2021 21:22:39 GMT-0700 (Pacific Daylight Time)] INFO Requests: 2839, requests per second: 299, mean latency: 33 ms
[Wed Jul 21 2021 21:22:44 GMT-0700 (Pacific Daylight Time)] INFO Requests: 4343, requests per second: 301, mean latency: 34 ms

After:

[Wed Jul 21 2021 21:24:11 GMT-0700 (Pacific Daylight Time)] INFO Requests: 1345, requests per second: 269, mean latency: 16.4 ms
[Wed Jul 21 2021 21:24:16 GMT-0700 (Pacific Daylight Time)] INFO Requests: 2844, requests per second: 300, mean latency: 16.6 ms
[Wed Jul 21 2021 21:24:21 GMT-0700 (Pacific Daylight Time)] INFO Requests: 4343, requests per second: 300, mean latency: 16.8 ms

The way I did it was, maybe the speed latency increase was due to channel creation and close are expensive:

	clients := make(chan []client)
	languages := make(chan []client)
	operatingSystems := make(chan []client)
	versions := make(chan []client)

	var waitGroup sync.WaitGroup
	waitGroup.Add(4)

	go func() {
		data, err := clientQuery(a.db, topClientsQuery, whereArgs...)
		if err != nil {
			fmt.Println(err)
		}
		clients <- data
		defer waitGroup.Done()
	}()
	
	go func() {
		data, err := clientQuery(a.db, topLanguageQuery, whereArgs...)
		if err != nil {
			fmt.Println(err)
		}
		languages <- data
		defer waitGroup.Done()
	}()

	go func() {
		data, err := clientQuery(a.db, topOsQuery, whereArgs...)
		if err != nil {
			fmt.Println(err)
		}
		operatingSystems <- data
		defer waitGroup.Done()
	}()

	go func() {
		// When the filter has a name, we don't want to include that in the json response.
		if nameCountInQuery == 1 {
			data, err := clientQuery(a.db, topVersionQuery, whereArgs...)
			if err != nil {
				fmt.Println(err)
			}
			versions <- data
		}
		defer waitGroup.Done()
	}()

	go func() {
		waitGroup.Wait()
		close(clients)
		close(languages)
		close(operatingSystems)
		close(versions)
	}()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant