Skip to content

Commit

Permalink
Merge pull request #120 from Jigsaw-Code/bemasc-microbench
Browse files Browse the repository at this point in the history
Add microbenchmarks for download throughput
  • Loading branch information
Benjamin M. Schwartz authored Apr 22, 2022
2 parents c6485a6 + 4b3a1f5 commit aa13697
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
42 changes: 42 additions & 0 deletions shadowsocks/packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package shadowsocks

import (
"testing"
"time"
)

// Microbenchmark for the performance of Shadowsocks UDP encryption.
func BenchmarkPack(b *testing.B) {
b.StopTimer()
b.ResetTimer()

cipher := newTestCipher(b)
MTU := 1500
pkt := make([]byte, MTU)
plaintextBuf := pkt[cipher.SaltSize() : len(pkt)-cipher.TagSize()]

start := time.Now()
b.StartTimer()
for i := 0; i < b.N; i++ {
Pack(pkt, plaintextBuf, cipher)
}
b.StopTimer()
elapsed := time.Now().Sub(start)

megabits := float64(8*len(plaintextBuf)*b.N) * 1e-6
b.ReportMetric(megabits/(elapsed.Seconds()), "mbps")
}
30 changes: 29 additions & 1 deletion shadowsocks/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/crypto/chacha20poly1305"
)

func newTestCipher(t *testing.T) *Cipher {
func newTestCipher(t testing.TB) *Cipher {
cipher, err := NewCipher("chacha20-ietf-poly1305", "test secret")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -411,3 +411,31 @@ func TestLazyWriteConcurrentFlush(t *testing.T) {
t.Errorf("Wrong final content: %v", decrypted)
}
}

type nullIO struct{}

func (n *nullIO) Write(b []byte) (int, error) {
return len(b), nil
}

func (r *nullIO) Read(b []byte) (int, error) {
return len(b), nil
}

// Microbenchmark for the performance of Shadowsocks TCP encryption.
func BenchmarkWriter(b *testing.B) {
b.StopTimer()
b.ResetTimer()

cipher := newTestCipher(b)
writer := NewShadowsocksWriter(new(nullIO), cipher)

start := time.Now()
b.StartTimer()
io.CopyN(writer, new(nullIO), int64(b.N))
b.StopTimer()
elapsed := time.Now().Sub(start)

megabits := 8 * float64(b.N) * 1e-6
b.ReportMetric(megabits/(elapsed.Seconds()), "mbps")
}

0 comments on commit aa13697

Please sign in to comment.