BenchmarkServiceImpl.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #region Copyright notice and license
  2. // Copyright 2015-2016 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using Google.Protobuf;
  21. using Grpc.Core;
  22. using Grpc.Core.Utils;
  23. namespace Grpc.Testing
  24. {
  25. /// <summary>
  26. /// Implementation of BenchmarkService server
  27. /// </summary>
  28. public class BenchmarkServiceImpl : BenchmarkService.BenchmarkServiceBase
  29. {
  30. public BenchmarkServiceImpl()
  31. {
  32. }
  33. public override Task<SimpleResponse> UnaryCall(SimpleRequest request, ServerCallContext context)
  34. {
  35. var response = new SimpleResponse { Payload = CreateZerosPayload(request.ResponseSize) };
  36. return Task.FromResult(response);
  37. }
  38. public override async Task StreamingCall(IAsyncStreamReader<SimpleRequest> requestStream, IServerStreamWriter<SimpleResponse> responseStream, ServerCallContext context)
  39. {
  40. await requestStream.ForEachAsync(async request =>
  41. {
  42. var response = new SimpleResponse { Payload = CreateZerosPayload(request.ResponseSize) };
  43. await responseStream.WriteAsync(response);
  44. });
  45. }
  46. private static Payload CreateZerosPayload(int size)
  47. {
  48. return new Payload { Body = ByteString.CopyFrom(new byte[size]) };
  49. }
  50. }
  51. }