File: loadsim.py

package info (click to toggle)
kopanocore 8.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,400 kB
  • sloc: cpp: 175,422; python: 24,623; perl: 7,319; php: 6,056; sh: 2,172; makefile: 1,294; xml: 45; ansic: 1
file content (114 lines) | stat: -rwxr-xr-x 2,577 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
#! /usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-only

# very simple load simulator

import os
import random
import sys
import time
import kopano

parser = kopano.parser('skpc', usage='loadsim [options]')
parser.add_option('-u', '--user', dest='user', action='store', help='user to send mails to')
parser.add_option('-N', '--n-write-workers', dest='n_write_workers', action='store', help='number of write workers to start', default=1)
parser.add_option('-n', '--n-read-workers', dest='n_read_workers', action='store', help='number of read workers to start', default=1)
parser.add_option('-r', '--new-session', dest='restart_session', action='store_false', help='start a new session for each iteration')
parser.add_option('-R', '--random-new-session', dest='random_restart_session', action='store_false', help='randomly start a new session for an iteration')
parser.add_option('-e', '--eml', dest='eml', action='store', help='eml-file to use')

o, a = parser.parse_args()

if o.eml == None:
	print('EML file missing')
	sys.exit(1)

eml_file_data = open(o.eml, 'rb').read()

random.seed()

def read_worker():
	server = None

	while True:
		try:
			if o.restart_session == True or server == None or (o.random_restart_session and random.randint(0, 1) == 1):
				server = kopano.Server(o)

			u = server.user(o.user)
			for folder in u.store.folders():
				for item in folder:
					if random.randint(0, 1) == 1:
						dummy = [(att.filename, att.mimetype, len(att.data)) for att in item.attachments()]

		except KeyboardInterrupt:
			return

		except Exception as e:
			print(e)

def write_worker():
	server = None

	while True:
		try:
			if o.restart_session == True or server == None or (o.random_restart_session and random.randint(0, 1) == 1):
				server = kopano.Server(o)

			item = server.user(o.user).store.inbox.create_item(eml = eml_file_data)

		except KeyboardInterrupt:
			return

		except Exception as e:
			print(e)

pids = []

print('Starting %s writers' % o.n_write_workers)
for i in range(int(o.n_write_workers)):
	pid = os.fork()

	if pid == 0:
		write_worker()
		sys.exit(0)

	elif pid == -1:
		print('fork failed')
		break;

	pids.append(pid)

print('Starting %s readers' % o.n_read_workers)
for i in range(int(o.n_read_workers)):
	pid = os.fork()

	if pid == 0:
		read_worker()
		sys.exit(0)

	elif pid == -1:
		print('fork failed')
		break;

	pids.append(pid)

print('Child processes started')

try:
	while True:
		time.sleep(86400)

except:
	pass

print('Terminating...')

for pid in pids:
	try:
		os.kill(pid, -9)

	except:
		pass

print('Finished.')