Skip to content

Commit

Permalink
Merge 9d58823 into 04a1356
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov authored Jun 1, 2023
2 parents 04a1356 + 9d58823 commit 9e272c1
Show file tree
Hide file tree
Showing 16 changed files with 2,646 additions and 0 deletions.
118 changes: 118 additions & 0 deletions examples/experimental/grpc/client_streaming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Client, Stream } from 'k6/experimental/grpc';
import { sleep } from 'k6';

const COORD_FACTOR = 1e7;
// to run this sample, you need to start the grpc server first.
// to start the grpc server, run the following command in k6 repository's root:
// go run -mod=mod examples/grpc_server/*.go
// (golang should be installed)
const GRPC_ADDR = __ENV.GRPC_ADDR || '127.0.0.1:10000';
const GRPC_PROTO_PATH = __ENV.GRPC_PROTO_PATH || '../../grpc_server/route_guide.proto';

let client = new Client();
client.load([], GRPC_PROTO_PATH);

// a sample DB of points
const DB = [
{
location: { latitude: 407838351, longitude: -746143763 },
name: 'Patriots Path, Mendham, NJ 07945, USA',
},
{
location: { latitude: 408122808, longitude: -743999179 },
name: '101 New Jersey 10, Whippany, NJ 07981, USA',
},
{
location: { latitude: 413628156, longitude: -749015468 },
name: 'U.S. 6, Shohola, PA 18458, USA',
},
{
location: { latitude: 419999544, longitude: -740371136 },
name: '5 Conners Road, Kingston, NY 12401, USA',
},
{
location: { latitude: 414008389, longitude: -743951297 },
name: 'Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA',
},
{
location: { latitude: 419611318, longitude: -746524769 },
name: '287 Flugertown Road, Livingston Manor, NY 12758, USA',
},
{
location: { latitude: 406109563, longitude: -742186778 },
name: '4001 Tremley Point Road, Linden, NJ 07036, USA',
},
{
location: { latitude: 416802456, longitude: -742370183 },
name: '352 South Mountain Road, Wallkill, NY 12589, USA',
},
{
location: { latitude: 412950425, longitude: -741077389 },
name: 'Bailey Turn Road, Harriman, NY 10926, USA',
},
{
location: { latitude: 412144655, longitude: -743949739 },
name: '193-199 Wawayanda Road, Hewitt, NJ 07421, USA',
},
];

// to run this sample, you need to start the grpc server first.
// to start the grpc server, run the following command in k6 repository's root:
// go run -mod=mod examples/grpc_server/*.go
// (golang should be installed)

// the example below is based on the original GRPC client streaming example
//
// It sends several randomly chosen points from the pre-generated
// feature database with a variable delay in between. Prints the
// statistics when they are sent from the server.
export default () => {
if (__ITER == 0) {
client.connect(GRPC_ADDR, { plaintext: true });
}

const stream = new Stream(client, 'main.RouteGuide/RecordRoute');

stream.on('data', (stats) => {
console.log('Finished trip with', stats.pointCount, 'points');
console.log('Passed', stats.featureCount, 'features');
console.log('Travelled', stats.distance, 'meters');
console.log('It took', stats.elapsedTime, 'seconds');
});

stream.on('error', (err) => {
console.log('Stream Error: ' + JSON.stringify(err));
});

stream.on('end', () => {
client.close();
console.log('All done');
})

// send 5 random items
for (var i = 0; i < 5; i++) {
let point = DB[Math.floor(Math.random() * DB.length)];
pointSender(stream, point);
}

// close the client stream
stream.end();

sleep(1);
};

const pointSender = (stream, point) => {
console.log(
'Visiting point ' +
point.name +
' ' +
point.location.latitude / COORD_FACTOR +
', ' +
point.location.longitude / COORD_FACTOR
);

// send the location to the server
stream.write(point.location);

sleep(0.5);
};
56 changes: 56 additions & 0 deletions examples/experimental/grpc/server_streaming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Client, Stream } from 'k6/experimental/grpc';
import { sleep } from 'k6';

const COORD_FACTOR = 1e7;
// to run this sample, you need to start the grpc server first.
// to start the grpc server, run the following command in k6 repository's root:
// go run -mod=mod examples/grpc_server/*.go
// (golang should be installed)
const GRPC_ADDR = __ENV.GRPC_ADDR || '127.0.0.1:10000';
const GRPC_PROTO_PATH = __ENV.GRPC_PROTO_PATH || '../../grpc_server/route_guide.proto';

let client = new Client();

client.load([], GRPC_PROTO_PATH);

export default () => {
client.connect(GRPC_ADDR, { plaintext: true });

const stream = new Stream(client, 'main.FeatureExplorer/ListFeatures', null);

stream.on('data', function (feature) {
console.log(
'Found feature called "' +
feature.name +
'" at ' +
feature.location.latitude / COORD_FACTOR +
', ' +
feature.location.longitude / COORD_FACTOR
);
});

stream.on('end', function () {
// The server has finished sending
client.close();
console.log('All done');
});

stream.on('error', function (e) {
// An error has occurred and the stream has been closed.
console.log('Error: ' + JSON.stringify(e));
});

// send a message to the server
stream.write({
lo: {
latitude: 400000000,
longitude: -750000000,
},
hi: {
latitude: 420000000,
longitude: -730000000,
},
});

sleep(0.5);
};
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/golang/protobuf v1.5.3
github.com/gorilla/websocket v1.5.0
github.com/grafana/xk6-browser v0.9.0
github.com/grafana/xk6-grpc v0.1.2
github.com/grafana/xk6-output-prometheus-remote v0.2.1
github.com/grafana/xk6-redis v0.1.1
github.com/grafana/xk6-timers v0.1.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/xk6-browser v0.9.0 h1:lEPSM/hgzIO+dvLbrf+O6flLgNH3d0i+GVkOzPi9HSs=
github.com/grafana/xk6-browser v0.9.0/go.mod h1:ax6OHARpNEu9hSGYOAI4grAwiRapsNPi9TBQxDYurKw=
github.com/grafana/xk6-grpc v0.1.2 h1:gNN3PYV2dIPoq1zTVz8YOxrWhl1D15jhRR0EA9ZYhBw=
github.com/grafana/xk6-grpc v0.1.2/go.mod h1:iq6qHN64XgAEmDHKf0OXZ4mvoqF4Udr22fiCIXNpXA0=
github.com/grafana/xk6-output-prometheus-remote v0.2.1 h1:/JOMHwByfCkFe17iokUDKCIjh8e5g0gqqnrg8zSnxf4=
github.com/grafana/xk6-output-prometheus-remote v0.2.1/go.mod h1:JWUOn1fY8Yp3dM+IaPSNMM6t+sF3aJ2AA7Qzs6lKGww=
github.com/grafana/xk6-redis v0.1.1 h1:rvWnLanRB2qzDwuY6NMBe6PXei3wJ3kjYvfCwRJ+q+8=
Expand Down
2 changes: 2 additions & 0 deletions js/jsmodules.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.k6.io/k6/js/modules/k6/metrics"
"go.k6.io/k6/js/modules/k6/ws"

expGrpc "github.com/grafana/xk6-grpc/grpc"
"github.com/grafana/xk6-redis/redis"
"github.com/grafana/xk6-timers/timers"
"github.com/grafana/xk6-webcrypto/webcrypto"
Expand All @@ -33,6 +34,7 @@ func getInternalJSModules() map[string]interface{} {
"k6/experimental/redis": redis.New(),
"k6/experimental/webcrypto": webcrypto.New(),
"k6/experimental/websockets": &expws.RootModule{},
"k6/experimental/grpc": expGrpc.New(),
"k6/experimental/timers": timers.New(),
"k6/experimental/tracing": tracing.New(),
"k6/experimental/browser": browser.New(),
Expand Down
Loading

0 comments on commit 9e272c1

Please sign in to comment.