-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoDB_ex.R
73 lines (58 loc) · 1.96 KB
/
mongoDB_ex.R
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
source("load_packages.R")
# create a connection to the mongoDB server instance
con <- mongo(collection = "weblog", url = "mongodb://localhost/27017")
# range query between two given dates
mongoDB_range <- function(){
con$find(
'{ "timestamp" : {
"$gte" : {"$date" : "1995-06-01T06:00:59Z"},
"$lte" : {"$date" : "1995-11-15T11:59:59Z"}
}}'
)}
# top visitors
mongoDB_count_groupby_host <- function(){
con$aggregate(
'[
{"$group":{"_id":"$host", "count":{"$sum":1}}},
{"$sort": {"count": -1}},
{"$limit": 10}
]'
)}
# count for HTTP_reply's
mongoDB_count_groupby_http <- function(){
con$aggregate(
'[
{"$group":{"_id":"$HTTP_reply", "count":{"$sum":1}}},
{"$sort": {"count": -1}},
{"$limit": 10}
]'
)}
# min and max reply_size
mongoDB_min_max <- function(){
con$aggregate(
'[
{ "$group" : {"_id": "null",
"max" : {"$max" : "$reply_size"},
"min" : {"$min" : "$reply_size"}}}
]'
)}
# avg reply sizes for top 10 visitors
mongoDB_group_by_avg <- function(){
con$aggregate(
'[
{"$group":{"_id":"$host", "count":{"$sum":1},
"avg_reply_size":{"$avg":"$reply_size"}}},
{"$sort": {"count": -1}},
{"$limit": 10}
]'
)}
microbenchmark(mongoDB_count_groupby_host(), mongoDB_count_groupby_http(),
mongoDB_group_by_avg(), mongoDB_min_max(),
mongoDB_range(), times = 10) -> mongoDB_results
write_csv(mongoDB_results, "mongoDB_results_after_index.csv")
# add indexes to all the fields and benchmark the quries again
con$index(add = '{"host": 1, "timestamp": 1,"request": 1,"HTTP_reply": 1,"reply_size": 1}')
microbenchmark(mongoDB_count_groupby_host(), mongoDB_count_groupby_http(),
mongoDB_group_by_avg(), mongoDB_min_max(),
mongoDB_range(), times = 10) -> mongoDB_results_index
write_csv(mongoDB_results_index, "mongoDB_results_after_index.csv")