Other: Use msgpack instead of json in vault

msgpack is much faster at serializing and deserializing than json. Using
it in the vault gives a performance boost.
This commit is contained in:
James Houlahan
2022-10-20 16:36:53 +02:00
parent c7a5b8559c
commit d376b88cf0
7 changed files with 75 additions and 10 deletions

View File

@ -0,0 +1,58 @@
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package vault_test
import (
"bytes"
"testing"
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func BenchmarkVault(b *testing.B) {
vaultDir, gluonDir := b.TempDir(), b.TempDir()
// Create a new vault.
s, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"))
require.NoError(b, err)
require.False(b, corrupt)
// Add 10kB of cookies to the vault.
require.NoError(b, s.SetCookies(bytes.Repeat([]byte("a"), 10_000)))
// Create 10 vault users.
for idx := 0; idx < 10; idx++ {
user, err := s.AddUser(uuid.NewString(), "username", "authUID", "authRef", []byte("keyPass"))
require.NoError(b, err)
require.NoError(b, user.SetKeyPass([]byte("new key pass")))
}
b.ResetTimer()
// Time how quickly we can iterate through the users and get their key pass and bridge pass.
for i := 0; i < b.N; i++ {
require.NoError(b, s.ForUser(func(user *vault.User) error {
require.NotEmpty(b, user.KeyPass())
require.NotEmpty(b, user.BridgePass())
return nil
}))
}
}