blob: ae4d54537ff3303f919f8792ab8ad42bc44c052e [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 {
Jens Geyer98a23252022-01-09 16:50:52 +010031 private MemoryStream? _Stream;
32 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 {
Jens Geyer98a23252022-01-09 16:50:52 +010048 _Protocol?.Dispose();
Mikel Blanchard4b66a9d2020-03-05 00:46:21 +010049 }
50
51 [Benchmark]
52 public async Task WriteString()
53 {
Jens Geyer98a23252022-01-09 16:50:52 +010054 if ((_Protocol is null) || (_Stream is null))
55 throw new System.Exception("unexpected internal state");
56
Mikel Blanchard4b66a9d2020-03-05 00:46:21 +010057 for (int i = 0; i < NumberOfOperationsPerIteration; i++)
58 {
59 await _Protocol.WriteStringAsync("Thrift String Benchmark");
Mikel Blanchard4b66a9d2020-03-05 00:46:21 +010060 _Stream.Seek(0, SeekOrigin.Begin);
61 }
62 }
63
64 [Benchmark]
65 public async Task ReadString()
66 {
Jens Geyer98a23252022-01-09 16:50:52 +010067 if ((_Protocol is null) || (_Stream is null))
68 throw new System.Exception("unexpected internal state");
69
Mikel Blanchard4b66a9d2020-03-05 00:46:21 +010070 await _Protocol.WriteStringAsync("Thrift String Benchmark");
71
72 for (int i = 0; i < NumberOfOperationsPerIteration; i++)
73 {
74 _Stream.Seek(0, SeekOrigin.Begin);
Mikel Blanchard4b66a9d2020-03-05 00:46:21 +010075 await _Protocol.ReadStringAsync();
76 }
77 }
78 }
79}