good morning!!!!

Skip to content
Snippets Groups Projects
benchmark_test.go 2.21 KiB
Newer Older
a's avatar
a committed
package jrpc

import (
	"testing"

	"golang.org/x/sync/errgroup"
)

func BenchmarkClientHTTPEcho(b *testing.B) {
	server := newTestServer()
	defer server.Stop()
	client, hs := httpTestClient(server, "http", nil)
	defer hs.Close()
	defer client.Close()

	// Launch concurrent requests.
	wantBack := map[string]any{
		"one": map[string]any{"two": "three"},
		"e":   map[string]any{"two": "three"},
		"oe":  map[string]any{"two": "three"},
		"on":  map[string]any{"two": "three"},
	}

	b.StartTimer()
	for n := 0; n < b.N; n++ {
		eg := &errgroup.Group{}
		for i := 0; i < 1000; i++ {
			eg.Go(func() error {
				return client.Call(nil, "test_echoAny", []any{1, 2, 3, 4, 56, 6, wantBack, wantBack, wantBack})
			})
		}
		eg.Wait()
	}
}
func BenchmarkClientHTTPEchoEmpty(b *testing.B) {
	server := newTestServer()
	defer server.Stop()
	client, hs := httpTestClient(server, "http", nil)
	defer hs.Close()
	defer client.Close()

	// Launch concurrent requests.

	b.StartTimer()
	for n := 0; n < b.N; n++ {
		eg := &errgroup.Group{}
		for i := 0; i < 1000; i++ {
			eg.Go(func() error {
				return client.Call(nil, "test_echoAny", 0)
			})
		}
		eg.Wait()
	}
}
func BenchmarkClientWebsocketEcho(b *testing.B) {
	server := newTestServer()
	defer server.Stop()
	client, hs := httpTestClient(server, "ws", nil)
	defer hs.Close()
	defer client.Close()

	// Launch concurrent requests.
	wantBack := map[string]any{
		"one": map[string]any{"two": "three"},
		"e":   map[string]any{"two": "three"},
		"oe":  map[string]any{"two": "three"},
		"on":  map[string]any{"two": "three"},
	}

a's avatar
a committed
	payload := []any{1, 2, 3, 4, 56, 6, wantBack, wantBack, wantBack}
a's avatar
a committed
	b.StartTimer()
	for n := 0; n < b.N; n++ {
		eg := &errgroup.Group{}
		for i := 0; i < 1000; i++ {
			eg.Go(func() error {
a's avatar
a committed
				return client.Call(nil, "test_echoAny", payload)
a's avatar
a committed
			})
		}
		eg.Wait()
	}

}
func BenchmarkClientWebsocketEchoEmpty(b *testing.B) {
	server := newTestServer()
	defer server.Stop()
	client, hs := httpTestClient(server, "ws", nil)
	defer hs.Close()
	defer client.Close()

	// Launch concurrent requests.
	b.StartTimer()

	for n := 0; n < b.N; n++ {
		eg := &errgroup.Group{}
		for i := 0; i < 1000; i++ {
			eg.Go(func() error {
				return client.Call(nil, "test_echoAny", 0)
			})
		}
		eg.Wait()
	}
}