Skip to content

Commit 5119484

Browse files
committed
Tests Using Locust and Faker
1 parent 6e29723 commit 5119484

File tree

7 files changed

+434
-0
lines changed

7 files changed

+434
-0
lines changed

pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@
148148
<version>${scala.plugin.version}</version>
149149
<configuration>
150150
<launchers>
151+
<launcher>
152+
<id>clouseau</id>
153+
<mainClass>com.cloudant.clouseau.Main</mainClass>
154+
<jvmArgs>
155+
<jvmArg>[email protected]</jvmArg>
156+
<jvmArg>-Dclouseau.cookie=monster</jvmArg>
157+
<jvmArg>-Dclouseau.dir=${basedir}/target/clouseau</jvmArg>
158+
</jvmArgs>
159+
</launcher>
151160
<launcher>
152161
<id>clouseau1</id>
153162
<mainClass>com.cloudant.clouseau.Main</mainClass>
@@ -353,6 +362,26 @@
353362
</execution>
354363
</executions>
355364
</plugin>
365+
<!-- Code Coverage report generation -->
366+
<plugin>
367+
<groupId>org.jacoco</groupId>
368+
<artifactId>jacoco-maven-plugin</artifactId>
369+
<version>0.7.9</version>
370+
<executions>
371+
<execution>
372+
<goals>
373+
<goal>prepare-agent</goal>
374+
</goals>
375+
</execution>
376+
<execution>
377+
<id>generate-code-coverage-report</id>
378+
<phase>test</phase>
379+
<goals>
380+
<goal>report</goal>
381+
</goals>
382+
</execution>
383+
</executions>
384+
</plugin>
356385
</plugins>
357386
<extensions>
358387
<extension>

test/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Locust Test
2+
3+
Test `Clouseau` using [Locust](https://github.com/locustio/locust) and [Faker](https://github.com/joke2k/faker).
4+
5+
## Configuration options
6+
7+
Locust configuration options.
8+
9+
Command line | Description
10+
--- | ---
11+
--headless | Disable the web interface, and start the test
12+
--only-summary | Only print the summary stats
13+
--host | Host to load test
14+
-u | Peak number of concurrent Locust users
15+
-r | Rate to spawn users at (users per second)
16+
-t | Stop after the specified amount of time
17+
--docs-number | The number of generated documents (default: 10)
18+
19+
```
20+
locust -f locustfile.py --headless --only-summary --docs-number 10 -u 1 -r 1 -t 10
21+
```
22+
23+
## Basic Usage
24+
25+
Run `CouchDB` and `Clouseau` in different terminals, and then run the locust test:
26+
27+
```
28+
# Open 4 different terminals and run the command:
29+
./dev/run --admin=adm:pass
30+
mvn scala:run -Dlauncher=clouseau1
31+
mvn scala:run -Dlauncher=clouseau2
32+
mvn scala:run -Dlauncher=clouseau3
33+
```
34+
35+
### Install dependencies:
36+
37+
```
38+
./run install
39+
```
40+
41+
### Run random_tree_generator tests:
42+
43+
```
44+
./run locust
45+
```
46+
47+
### Cleanup
48+
49+
```
50+
./run clean
51+
```

test/data.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import math
2+
import json
3+
from datetime import date
4+
from random import choice
5+
from faker import Faker
6+
7+
8+
def write_to_files(data, filename, files_number=0):
9+
if files_number:
10+
chunks = [data[files_number * i:files_number * (i + 1)]
11+
for i in range(math.ceil(len(data) / files_number))]
12+
idx = filename.find('.json')
13+
for i in range(len(chunks)):
14+
with open(filename[:idx] + str(i) + filename[idx:], 'w') as outfile:
15+
json.dump(chunks[i], outfile)
16+
else:
17+
with open(filename, 'w') as outfile:
18+
json.dump(data, outfile)
19+
20+
21+
def gen_data(n=10, files_number=10, latmin=0, latmax=50, lonmin=0, lonmax=50):
22+
data = []
23+
counter = {}
24+
fake = Faker()
25+
fields = ['married', 'ethnicity', 'gender']
26+
counter['total_rows'] = n
27+
28+
for i in range(n):
29+
data.append({'_id': str(i)})
30+
data[i]['gender'] = choice(['M', 'F'])
31+
data[i]['name'] = fake.name_male() if data[i]['gender'] == 'M' else fake.name_female()
32+
data[i]['date_of_birth'] = fake.iso8601()
33+
data[i]['age'] = date.today().year - int(data[i]['date_of_birth'][:4])
34+
data[i]['married'] = 'False' if data[i]['age'] < 22 else choice(['True', 'False'])
35+
data[i]['ethnicity'] = choice(['White', 'Black', 'Asian', 'Hispanic', 'non-Hispanic'])
36+
data[i]['address'] = {'full_address': fake.address()}
37+
data[i]['address']['city'] = data[i]['address']['full_address'][
38+
data[i]['address']['full_address'].find('\n') + 1: -10]
39+
data[i]['address']['area'] = data[i]['address']['full_address'][-8:-6]
40+
data[i]['address']['zip'] = data[i]['address']['full_address'][-5:]
41+
data[i]['lat'] = float(fake.latitude())
42+
data[i]['lon'] = float(fake.longitude())
43+
44+
for field in fields:
45+
if field not in counter:
46+
counter[field] = {}
47+
counter[field].update({data[i][field]: counter[field].get(data[i][field], 0) + 1})
48+
49+
if latmin <= data[i]['lat'] <= latmax:
50+
counter['lat'] = counter.get('lat', 0) + 1
51+
if lonmin < data[i]['lon'] < lonmax:
52+
counter['lon'] = counter.get('lon', 0) + 1
53+
if latmin <= data[i]['lat'] <= latmax and lonmin < data[i]['lon'] < lonmax:
54+
counter['geo'] = counter.get('geo', 0) + 1
55+
56+
write_to_files(data, 'data.json', files_number)
57+
write_to_files(counter, 'analysis.json')

test/data_thread.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import math
2+
import json
3+
from datetime import date
4+
from random import choice
5+
from faker import Faker
6+
7+
8+
def write_to_files(data, filename, files_number=0):
9+
if files_number:
10+
chunks = [data[files_number * i:files_number * (i + 1)]
11+
for i in range(math.ceil(len(data) / files_number))]
12+
idx = filename.find('.json')
13+
for i in range(len(chunks)):
14+
with open(filename[:idx] + str(i) + filename[idx:], 'w') as outfile:
15+
json.dump(chunks[i], outfile)
16+
else:
17+
with open(filename, 'w') as outfile:
18+
json.dump(data, outfile)
19+
20+
21+
def gen_data(n=10, files_number=10, latmin=0, latmax=50, lonmin=0, lonmax=50):
22+
data = []
23+
counter = {}
24+
fake = Faker()
25+
fields = ['married', 'ethnicity', 'gender']
26+
counter['total_rows'] = n
27+
28+
for i in range(n):
29+
data.append({'_id': str(i)})
30+
data[i]['gender'] = choice(['M', 'F'])
31+
data[i]['name'] = fake.name_male() if data[i]['gender'] == 'M' else fake.name_female()
32+
data[i]['date_of_birth'] = fake.iso8601()
33+
data[i]['age'] = date.today().year - int(data[i]['date_of_birth'][:4])
34+
data[i]['married'] = 'False' if data[i]['age'] < 22 else choice(['True', 'False'])
35+
data[i]['ethnicity'] = choice(['White', 'Black', 'Asian', 'Hispanic', 'non-Hispanic'])
36+
data[i]['address'] = {'full_address': fake.address()}
37+
data[i]['address']['city'] = data[i]['address']['full_address'][
38+
data[i]['address']['full_address'].find('\n') + 1: -10]
39+
data[i]['address']['area'] = data[i]['address']['full_address'][-8:-6]
40+
data[i]['address']['zip'] = data[i]['address']['full_address'][-5:]
41+
data[i]['lat'] = float(fake.latitude())
42+
data[i]['lon'] = float(fake.longitude())
43+
44+
for field in fields:
45+
if field not in counter:
46+
counter[field] = {}
47+
counter[field].update({data[i][field]: counter[field].get(data[i][field], 0) + 1})
48+
49+
if latmin <= data[i]['lat'] <= latmax:
50+
counter['lat'] = counter.get('lat', 0) + 1
51+
if lonmin < data[i]['lon'] < lonmax:
52+
counter['lon'] = counter.get('lon', 0) + 1
53+
if latmin <= data[i]['lat'] <= latmax and lonmin < data[i]['lon'] < lonmax:
54+
counter['geo'] = counter.get('geo', 0) + 1
55+
56+
write_to_files(data, 'data.json', files_number)
57+
write_to_files(counter, 'analysis.json')

0 commit comments

Comments
 (0)