Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System.IO; |
| 19 | using System.Threading.Tasks; |
| 20 | |
| 21 | using BenchmarkDotNet.Attributes; |
| 22 | |
| 23 | using Thrift.Protocol; |
| 24 | using Thrift.Transport.Client; |
| 25 | |
| 26 | namespace Thrift.Benchmarks |
| 27 | { |
| 28 | [MemoryDiagnoser] |
| 29 | public class CompactProtocolBenchmarks |
| 30 | { |
| 31 | private MemoryStream _Stream; |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 32 | private TProtocol _Protocol; |
Mikel Blanchard | 4b66a9d | 2020-03-05 00:46:21 +0100 | [diff] [blame] | 33 | |
| 34 | [Params(10000)] |
| 35 | public int NumberOfOperationsPerIteration { get; set; } |
| 36 | |
| 37 | [GlobalSetup] |
| 38 | public void GlobalSetup() |
| 39 | { |
| 40 | _Stream = new MemoryStream(); |
| 41 | var transport = new TStreamTransport(_Stream, _Stream, null); |
| 42 | _Protocol = new TCompactProtocol(transport); |
| 43 | } |
| 44 | |
| 45 | [GlobalCleanup] |
| 46 | public void GlobalCleanup() |
| 47 | { |
| 48 | _Protocol.Dispose(); |
| 49 | } |
| 50 | |
| 51 | [Benchmark] |
| 52 | public async Task WriteString() |
| 53 | { |
| 54 | for (int i = 0; i < NumberOfOperationsPerIteration; i++) |
| 55 | { |
| 56 | await _Protocol.WriteStringAsync("Thrift String Benchmark"); |
| 57 | |
| 58 | _Stream.Seek(0, SeekOrigin.Begin); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | [Benchmark] |
| 63 | public async Task ReadString() |
| 64 | { |
| 65 | await _Protocol.WriteStringAsync("Thrift String Benchmark"); |
| 66 | |
| 67 | for (int i = 0; i < NumberOfOperationsPerIteration; i++) |
| 68 | { |
| 69 | _Stream.Seek(0, SeekOrigin.Begin); |
| 70 | |
| 71 | await _Protocol.ReadStringAsync(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |