2017-01-07 17:52:23 +03:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2019-01-16 21:54:43 +03:00
|
|
|
"errors"
|
2017-01-07 17:52:23 +03:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
glog "github.com/go-log/log"
|
|
|
|
"github.com/micro/go-micro/client"
|
2019-08-06 19:53:14 +03:00
|
|
|
proto "github.com/micro/go-micro/debug/proto"
|
2019-01-14 18:27:25 +03:00
|
|
|
"github.com/micro/go-micro/registry/memory"
|
2019-05-31 02:38:05 +03:00
|
|
|
"github.com/micro/go-micro/util/log"
|
2017-01-07 17:52:23 +03:00
|
|
|
)
|
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
func testShutdown(wg *sync.WaitGroup, cancel func()) {
|
|
|
|
// add 1
|
2017-01-07 17:52:23 +03:00
|
|
|
wg.Add(1)
|
2019-01-16 21:54:43 +03:00
|
|
|
// shutdown the service
|
|
|
|
cancel()
|
|
|
|
// wait for stop
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
func testService(ctx context.Context, wg *sync.WaitGroup, name string) Service {
|
|
|
|
// set no op logger
|
|
|
|
log.SetLogger(glog.DefaultLogger)
|
|
|
|
|
|
|
|
// add self
|
|
|
|
wg.Add(1)
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2019-11-16 13:55:11 +03:00
|
|
|
r := memory.NewRegistry(memory.Services(testData))
|
2019-01-14 18:27:25 +03:00
|
|
|
|
2017-01-07 17:52:23 +03:00
|
|
|
// create service
|
2019-01-16 21:54:43 +03:00
|
|
|
return NewService(
|
|
|
|
Name(name),
|
2017-01-07 17:52:23 +03:00
|
|
|
Context(ctx),
|
2019-01-14 18:27:25 +03:00
|
|
|
Registry(r),
|
2017-01-07 17:52:23 +03:00
|
|
|
AfterStart(func() error {
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}),
|
2019-01-16 21:54:43 +03:00
|
|
|
AfterStop(func() error {
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}),
|
2017-01-07 17:52:23 +03:00
|
|
|
)
|
2019-01-16 21:54:43 +03:00
|
|
|
}
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
func testRequest(ctx context.Context, c client.Client, name string) error {
|
|
|
|
// test call debug
|
|
|
|
req := c.NewRequest(
|
|
|
|
name,
|
|
|
|
"Debug.Health",
|
|
|
|
new(proto.HealthRequest),
|
|
|
|
)
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
rsp := new(proto.HealthResponse)
|
2018-03-13 14:12:42 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
err := c.Call(context.TODO(), req, rsp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-13 14:12:42 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
if rsp.Status != "ok" {
|
|
|
|
return errors.New("service response: " + rsp.Status)
|
|
|
|
}
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestService tests running and calling a service
|
|
|
|
func TestService(t *testing.T) {
|
|
|
|
// waitgroup for server start
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// cancellation context
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
// start test server
|
|
|
|
service := testService(ctx, &wg, "test.service")
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// wait for service to start
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// make a test call
|
|
|
|
if err := testRequest(ctx, service.Client(), "test.service"); err != nil {
|
|
|
|
t.Fatal(err)
|
2018-05-25 16:39:50 +03:00
|
|
|
}
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2018-05-25 16:39:50 +03:00
|
|
|
// shutdown the service
|
2019-01-16 21:54:43 +03:00
|
|
|
testShutdown(&wg, cancel)
|
2018-05-25 16:39:50 +03:00
|
|
|
}()
|
2017-01-07 17:52:23 +03:00
|
|
|
|
2019-01-16 21:54:43 +03:00
|
|
|
// start service
|
2018-05-25 16:39:50 +03:00
|
|
|
if err := service.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-07 17:52:23 +03:00
|
|
|
}
|
2019-01-16 21:54:43 +03:00
|
|
|
|
|
|
|
func benchmarkService(b *testing.B, n int, name string) {
|
|
|
|
// stop the timer
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
// waitgroup for server start
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// cancellation context
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// create test server
|
|
|
|
service := testService(ctx, &wg, name)
|
|
|
|
|
|
|
|
// start the server
|
|
|
|
go func() {
|
|
|
|
if err := service.Run(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for service to start
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// make a test call to warm the cache
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if err := testRequest(ctx, service.Client(), name); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the timer
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
// number of iterations
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
// for concurrency
|
|
|
|
for j := 0; j < n; j++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := testRequest(ctx, service.Client(), name)
|
|
|
|
wg.Done()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for test completion
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop the timer
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
// shutdown service
|
|
|
|
testShutdown(&wg, cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkService1(b *testing.B) {
|
|
|
|
benchmarkService(b, 1, "test.service.1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkService8(b *testing.B) {
|
|
|
|
benchmarkService(b, 8, "test.service.8")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkService16(b *testing.B) {
|
|
|
|
benchmarkService(b, 16, "test.service.16")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkService32(b *testing.B) {
|
|
|
|
benchmarkService(b, 32, "test.service.32")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkService64(b *testing.B) {
|
|
|
|
benchmarkService(b, 64, "test.service.64")
|
|
|
|
}
|