-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sort.py
166 lines (136 loc) · 5.81 KB
/
test_sort.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import dask
import dask.dataframe as dd
import json
import pandas as pd
import numpy as np
import os.path
import csv
import boto3
import traceback
import sys
from dask.distributed import Client
from dask.distributed import wait
import time
def load_dataset(client, data_dir, s3_bucket, nbytes, npartitions):
num_bytes_per_partition = nbytes // npartitions
filenames = []
@dask.delayed
def generate_s3_file(i, data_dir, s3_bucket):
s3 = boto3.Session().client('s3')
key = "df-{}-{}.parquet.gzip".format(num_bytes_per_partition, i)
contents = s3.list_objects(Bucket=s3_bucket, Prefix=key)
for obj in contents.get('Contents', []):
if obj['Key'] == key:
print(f"S3 partition {i} exists")
return
filename = os.path.join(data_dir, key)
if not os.path.exists(filename):
print("Generating partition", filename)
nrows = num_bytes_per_partition // 8
dataset = pd.DataFrame(np.random.randint(0, np.iinfo(np.int64).max, size=(nrows, 1), dtype=np.int64), columns=['a'])
dataset.to_parquet(filename, compression='gzip')
print("Writing partition to S3", filename)
with open(filename, 'rb') as f:
s3.put_object(Bucket=s3_bucket, Key=key, Body=f)
#for i in range(npartitions):
# filenames.append(foo(i, data_dir))
#filenames = dask.compute(filenames)[0]
x = []
for i in range(npartitions):
x.append(generate_s3_file(i, data_dir, s3_bucket))
dask.compute(x)
#filenames = []
#for i in range(npartitions):
# filename = "df-{}-{}.parquet.gzip".format(num_bytes_per_partition, i)
# filenames.append(f"s3://dask-on-ray-data/{filename}")
filenames = [f's3://{s3_bucket}/df-{num_bytes_per_partition}-{i}.parquet.gzip' for i in range(npartitions)]
df = dd.read_parquet(filenames)
return df
def trial(client, data_dir, s3_bucket, nbytes, n_partitions, generate_only):
df = load_dataset(client, data_dir, s3_bucket, nbytes, n_partitions)
if generate_only:
return
times = []
start = time.time()
for i in range(10):
print("Trial {} start".format(i))
trial_start = time.time()
if client is None:
print(df.set_index('a', shuffle='tasks', max_branch=float('inf')).head(10, npartitions=-1))
else:
print(df.set_index('a').head(10, npartitions=-1))
trial_end = time.time()
duration = trial_end - trial_start
times.append(duration)
print("Trial {} done after {}".format(i, duration))
if time.time() - start > 60:
break
return times
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--nbytes", type=int, default=1_000_000)
parser.add_argument("--npartitions", type=int, default=100, required=False)
# Max partition size is 1GB.
parser.add_argument("--max-partition-size", type=int, default=1000_000_000, required=False)
parser.add_argument("--num-nodes", type=int, default=1)
parser.add_argument("--generate-only", action="store_true")
parser.add_argument("--skip-existing", action="store_true")
parser.add_argument("--ray", action="store_true")
parser.add_argument("--data-dir", default="/home/ubuntu/dask-benchmarks")
parser.add_argument("--s3-bucket", default="dask-on-ray-data")
parser.add_argument("--dask-nprocs", type=int, default=0)
parser.add_argument("--dask-nthreads", type=int, default=0)
parser.add_argument("--dask-memlimit", type=int, default=0)
args = parser.parse_args()
npartitions = args.npartitions
if args.nbytes // npartitions > args.max_partition_size:
npartitions = args.nbytes // args.max_partition_size
row = {
"num_nodes": args.num_nodes,
"nbytes": args.nbytes,
"npartitions": npartitions,
"dask_nprocs": args.dask_nprocs,
"dask_nthreads": args.dask_nthreads,
"dask_memlimit": args.dask_memlimit,
}
if args.skip_existing and os.path.exists("output.csv"):
with open("output.csv", "r") as csvfile:
reader = csv.DictReader(csvfile)
for done_row in reader:
duplicate = True
for field in row:
if int(row[field]) != int(done_row[field]):
duplicate = False
if duplicate:
print("Already found row with spec", row)
sys.exit(0)
print("Running", row)
if args.ray:
import ray
ray.init(address='auto')
from ray.util.dask import ray_dask_get, dataframe_optimize
dask.config.set(scheduler=ray_dask_get, dataframe_optimize=dataframe_optimize)
client = None
else:
assert args.dask_nprocs != -0
assert args.dask_nthreads != -0
assert args.dask_memlimit != -0
client = Client('localhost:8786')
print(trial(client, args.data_dir, args.s3_bucket, 1000, 10, args.generate_only))
print("WARMUP DONE")
try:
output = trial(client, args.data_dir, args.s3_bucket, args.nbytes, npartitions, args.generate_only)
print("mean over {} trials: {} +- {}".format(len(output), np.mean(output), np.std(output)))
except Exception as e:
output = "x"
print(traceback.format_exc())
write_header = not os.path.exists("output.csv") or os.path.getsize("output.csv") == 0
with open("output.csv", "a+") as csvfile:
fieldnames = ["num_nodes", "nbytes", "npartitions", "dask_nprocs", "dask_nthreads", "dask_memlimit", "duration"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if write_header:
writer.writeheader()
for output in output:
row["duration"] = output
writer.writerow(row)