|
1
|
{ |
|
2
|
"cells": [ |
|
3
|
{ |
|
4
|
"cell_type": "markdown", |
|
5
|
"metadata": {}, |
|
6
|
"source": [ |
|
7
|
"*Pull in the packages we need:*" |
|
8
|
] |
|
9
|
}, |
|
10
|
{ |
|
11
|
"cell_type": "code", |
|
12
|
"execution_count": 1, |
|
13
|
"metadata": {}, |
|
14
|
"outputs": [], |
|
15
|
"source": [ |
|
16
|
"require(stats)" |
|
17
|
] |
|
18
|
}, |
|
19
|
{ |
|
20
|
"cell_type": "markdown", |
|
21
|
"metadata": {}, |
|
22
|
"source": [ |
|
23
|
"# Parameters\n", |
|
24
|
"\n", |
|
25
|
"* **cpd** - Checkins per day. Defaults to 20.\n", |
|
26
|
"\n", |
|
27
|
"* **days** - Number of days to simulate. Defaults to 10000, or roughly\n", |
|
28
|
" 40 working years.\n", |
|
29
|
"\n", |
|
30
|
"* **winSz** - Size of the commit window as a fraction of `workSec`. Defaults\n", |
|
31
|
" to 0.01%, which is roughly 3 seconds for the default 8-hour work day, a\n", |
|
32
|
" a long-ish commit time for Fossil.\n", |
|
33
|
"\n", |
|
34
|
"* **workSec** - Seconds in working day, defaulting to 8 hours. This value\n", |
|
35
|
" only affects the reporting output, not the results of the underlying\n", |
|
36
|
" simulation. It's a scaling parameter to humanize the results.\n", |
|
37
|
" \n", |
|
38
|
"* **spread** - Adjustment factor in computing the standard deviation for our \n", |
|
39
|
" normally-distributed random numbers. The default gives \"nice\" distributions,\n", |
|
40
|
" spread over the working day with a low chance of generating values outside\n", |
|
41
|
" the working day.\n", |
|
42
|
" \n", |
|
43
|
" The smaller this value gets (<4), the more spread out the checkins, and\n", |
|
44
|
" so the lower the chance of collisions. You might want to decrease it a bit\n", |
|
45
|
" to simulate early and late workers.\n", |
|
46
|
" \n", |
|
47
|
" As you increase this value (>4) you're simulating a work environment\n", |
|
48
|
" where people tend to check their work in closer and closer to mid-day,\n", |
|
49
|
" which increases the chance of collision." |
|
50
|
] |
|
51
|
}, |
|
52
|
{ |
|
53
|
"cell_type": "code", |
|
54
|
"execution_count": 2, |
|
55
|
"metadata": {}, |
|
56
|
"outputs": [], |
|
57
|
"source": [ |
|
58
|
"collisions <- function(\n", |
|
59
|
" cpd = 20,\n", |
|
60
|
" days = 10000,\n", |
|
61
|
" winSz = 0.01 / 100,\n", |
|
62
|
" workSec = 8 * 60 * 60,\n", |
|
63
|
" spread = 4)\n", |
|
64
|
"{\n", |
|
65
|
" cat(\"Running simulation...\\n\")\n", |
|
66
|
"\n", |
|
67
|
" day = 0\n", |
|
68
|
" collisions = 0\n", |
|
69
|
" winSec = workSec * winSz\n", |
|
70
|
" mean = workSec / 2\n", |
|
71
|
" sd = workSec / spread\n", |
|
72
|
"\n", |
|
73
|
" while (day < days) {\n", |
|
74
|
" # Create the commit time vector as random values in a normal\n", |
|
75
|
" # distribution.\n", |
|
76
|
" times = sort(rnorm(cpd, mean, sd))\n", |
|
77
|
"\n", |
|
78
|
" # Are there any pairs in the time vector that are within the\n", |
|
79
|
" # passed window size? If so, that's a Fossil checkin collision.\n", |
|
80
|
" i = 1\n", |
|
81
|
" while (i < cpd) {\n", |
|
82
|
" if (abs(times[i] - times[i + 1]) < winSec) {\n", |
|
83
|
" collisions = collisions + 1\n", |
|
84
|
" }\n", |
|
85
|
" i = i + 1\n", |
|
86
|
" }\n", |
|
87
|
" \n", |
|
88
|
" day = day + 1\n", |
|
89
|
" }\n", |
|
90
|
" \n", |
|
91
|
" cat(\"Found\", collisions, \"collisions in\", days, (workSec / 3600),\n", |
|
92
|
" \"hour working days with\", winSec, \"second windows.\\n\")\n", |
|
93
|
"}" |
|
94
|
] |
|
95
|
}, |
|
96
|
{ |
|
97
|
"cell_type": "markdown", |
|
98
|
"metadata": {}, |
|
99
|
"source": [ |
|
100
|
"*Run the following cell, possibly changing parameters documented above:*" |
|
101
|
] |
|
102
|
}, |
|
103
|
{ |
|
104
|
"cell_type": "code", |
|
105
|
"execution_count": 3, |
|
106
|
"metadata": {}, |
|
107
|
"outputs": [ |
|
108
|
{ |
|
109
|
"name": "stdout", |
|
110
|
"output_type": "stream", |
|
111
|
"text": [ |
|
112
|
"Running simulation...\n", |
|
113
|
"Found 422 collisions in 10000 8 hour working days with 2.88 second windows.\n" |
|
114
|
] |
|
115
|
} |
|
116
|
], |
|
117
|
"source": [ |
|
118
|
"collisions()" |
|
119
|
] |
|
120
|
} |
|
121
|
], |
|
122
|
"metadata": { |
|
123
|
"kernelspec": { |
|
124
|
"display_name": "R", |
|
125
|
"language": "R", |
|
126
|
"name": "ir" |
|
127
|
}, |
|
128
|
"language_info": { |
|
129
|
"codemirror_mode": "r", |
|
130
|
"file_extension": ".r", |
|
131
|
"mimetype": "text/x-r-source", |
|
132
|
"name": "R", |
|
133
|
"pygments_lexer": "r", |
|
134
|
"version": "3.3.2" |
|
135
|
} |
|
136
|
}, |
|
137
|
"nbformat": 4, |
|
138
|
"nbformat_minor": 2 |
|
139
|
} |
|
140
|
|