File: expunge.go

package info (click to toggle)
golang-github-protonmail-gluon 0.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,020 kB
  • sloc: sh: 55; makefile: 5
file content (122 lines) | stat: -rw-r--r-- 3,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package imap_benchmarks

import (
	"context"
	"flag"
	"fmt"
	"net"

	"github.com/ProtonMail/gluon/benchmarks/gluon_bench/benchmark"
	"github.com/ProtonMail/gluon/benchmarks/gluon_bench/flags"
	"github.com/bradenaw/juniper/xslices"
	"github.com/emersion/go-imap"
	"github.com/emersion/go-imap/client"
)

var (
	expungeCountFlag    = flag.Uint("imap-expunge-count", 0, "Total number of messages to expunge during expunge benchmarks.")
	expungeSameMBoxFlag = flag.Bool("imap-expunge-same-mbox", false, "When true run all the expunge test on the same inbox rather than separate ones in parallel.")
	expungeListFlag     = flag.String("imap-expunge-list", "", "Use a list of predefined sequences to expunge rather than random generated. Only works when -expunge-same-mbox is not set.")
	expungeAllFlag      = flag.Bool("imap-expunge-all", false, "If set, perform a expunge of the all messages. Only works when -expunge-same-mbox is not set.")
)

type Expunge struct {
	*stateTracker
	seqSets  *ParallelSeqSet
	mboxInfo []MailboxInfo
}

func NewExpunge() benchmark.Benchmark {
	return NewIMAPBenchmarkRunner(&Expunge{stateTracker: newStateTracker()})
}

func (*Expunge) Name() string {
	return "imap-expunge"
}

func (e *Expunge) Setup(ctx context.Context, addr net.Addr) error {
	return WithClient(addr, func(cl *client.Client) error {
		if *expungeSameMBoxFlag {
			if _, err := e.createAndFillRandomMBox(cl); err != nil {
				return err
			}

			expungeCount := uint32(*expungeCountFlag)
			if expungeCount == 0 {
				expungeCount = uint32(*flags.IMAPMessageCount)
			}

			e.seqSets = NewParallelSeqSetExpunge(expungeCount,
				*flags.IMAPParallelClients,
				*flags.IMAPRandomSeqSetIntervals,
				*flags.IMAPUIDMode,
			)

			e.mboxInfo = make([]MailboxInfo, *flags.IMAPParallelClients)
			for i := 0; i < len(e.mboxInfo); i++ {
				e.mboxInfo[i] = MailboxInfo{Name: e.MBoxes[0], ReadOnly: false}
			}
		} else {
			for i := uint(0); i < *flags.IMAPParallelClients; i++ {
				if _, err := e.createAndFillRandomMBox(cl); err != nil {
					return err
				}
			}

			seqSets, err := NewParallelSeqSet(uint32(*flags.IMAPMessageCount),
				*flags.IMAPParallelClients,
				*expungeListFlag,
				*expungeAllFlag,
				*flags.IMAPRandomSeqSetIntervals,
				true,
				*flags.IMAPUIDMode)
			if err != nil {
				return err
			}

			e.seqSets = seqSets

			e.mboxInfo = xslices.Map(e.MBoxes, func(m string) MailboxInfo {
				return MailboxInfo{Name: m, ReadOnly: false}
			})
		}
		return nil
	})
}

func (e *Expunge) TearDown(ctx context.Context, addr net.Addr) error {
	return e.cleanupWithAddr(addr)
}

func (e *Expunge) Run(ctx context.Context, addr net.Addr) error {
	RunParallelClientsWithMailboxes(addr, e.mboxInfo, func(cl *client.Client, index uint) {
		var expungeFn func(*client.Client, *imap.SeqSet) error
		if *flags.IMAPUIDMode {
			expungeFn = func(cl *client.Client, set *imap.SeqSet) error {
				if err := UIDStore(cl, set, "+FLAGS", true, imap.DeletedFlag); err != nil {
					return err
				}
				return cl.Expunge(nil)
			}
		} else {
			expungeFn = func(cl *client.Client, set *imap.SeqSet) error {
				if err := Store(cl, set, "+FLAGS", true, imap.DeletedFlag); err != nil {
					return err
				}
				return cl.Expunge(nil)
			}
		}

		for _, v := range e.seqSets.Get(index) {
			if err := expungeFn(cl, v); err != nil {
				panic(fmt.Sprintf("Seq:%v err:%v", v, err))
			}
		}
	})

	return nil
}

func init() {
	benchmark.RegisterBenchmark(NewExpunge())
}