blob: 16dcc766ef8001a5d87df90a5b94aaf258fe0253 [file] [log] [blame]
Mikel Blanchard4b66a9d2020-03-05 00:46:21 +01001// 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
18using System.IO;
19using System.Threading.Tasks;
20
21using BenchmarkDotNet.Attributes;
22
23using Thrift.Protocol;
24using Thrift.Transport.Client;
25
26namespace Thrift.Benchmarks
27{
28 [MemoryDiagnoser]
29 public class CompactProtocolBenchmarks
30 {
31 private MemoryStream _Stream;
Jens Geyerdce22992020-05-16 23:02:27 +020032 private TProtocol _Protocol;
Mikel Blanchard4b66a9d2020-03-05 00:46:21 +010033
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}