-
Notifications
You must be signed in to change notification settings - Fork 41
/
SchedulerBenchmark.ino
200 lines (185 loc) · 5.56 KB
/
SchedulerBenchmark.ino
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* @file SchedulerBenchmark.ino
* @version 1.0
*
* @section License
* Copyright (C) 2015-2016, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* @section Description
* Benchmark Scheduler library.
*
* @section Results Arduino 1.6.7, Arduino Pro-Mini
* SchedulerBenchmark: started
* 1. Initiate scheduler and main task: 4 us
* 2. Yield main task: 11.00 us
* 3. Start a task: 20 us
* 4. Yield between two tasks: 22.89 us
* 5. Delay 10 ms and check increments: 10028:381, 26.32 us
* 6. Start 8 tasks: 148:8, 18.50 us
* 7. Yield and check increments: 120:9, 13.33 us
* 8. Delay 10 ms and check increments: 10088:747, 13.50 us
*
* @section Results Arduino 1.6.7, Arduino Mega 2560
* SchedulerBenchmark: started
* 1. Initiate scheduler and main task: 4 us
* 2. Yield main task: 12.64 us
* 3. Start a task: 24 us
* 4. Yield between two tasks: 26.16 us
* 5. Delay 10 ms and check increments: 10028:338, 29.67 us
* 6. Start 46 tasks: 860:46, 18.70 us
* 7. Yield and check increments: 648:47, 13.79 us
* 8. Delay 10 ms and check increments: 10436:752, 13.88 us
*
* @section Results Arduino 1.6.7, Arduino Due
* SchedulerBenchmark: started
* 1. Initiate scheduler and main task: 2 us
* 2. Yield main task: 1.30 us
* 3. Start a task: 3 us
* 4. Yield between two tasks: 2.80 us
* 5. Delay 10 ms and check increments: 9497:3186, 2.98 us
* 6. Start 51 tasks: 112:51, 2.20 us
* 7. Yield and check increments: 78:52, 1.50 us
* 8. Delay 10 ms and check increments: 9874:6500, 1.52 us
*
* @section Results Arduino 1.6.7, Teensy 3.1 (72 MHz)
* SchedulerBenchmark: started
* 1. Initiate scheduler and main task: 1 us
* 2. Yield main task: 1.10 us
* 3. Start a task: 2 us
* 4. Yield between two tasks: 2.51 us
* 5. Delay 10 ms and check increments: 10003:3105, 3.22 us
* 6. Start 24 tasks: 38:24, 1.58 us
* 7. Yield and check increments: 30:25, 1.20 us
* 8. Delay 10 ms and check increments: 10015:6950, 1.44 us
*/
#include <Scheduler.h>
// Check for SparkFun SAMD21 Breakout
#if defined(ARDUINO_ARCH_SAMD) && (USB_PID == 0x8D21)
#define Serial SerialUSB
#endif
int counter = 0;
void setup()
{
unsigned long start, stop, us;
int nr;
Serial.begin(57600);
while (!Serial);
Serial.println(F("SchedulerBenchmark: started"));
Serial.flush();
// 1. Measure initiate scheduler
start = micros();
Scheduler.begin(256);
stop = micros();
us = stop - start;
Serial.print(F("1. Initiate scheduler and main task: "));
Serial.print(us);
Serial.println(F(" us"));
Serial.flush();
// 2. Measure yield with single task
start = micros();
for (int i = 0; i < 10000; i++)
yield();
stop = micros();
us = stop - start;
Serial.print(F("2. Yield main task: "));
Serial.print(us / 10000.0);
Serial.println(F(" us"));
Serial.flush();
// 3. Measure start of task
start = micros();
Scheduler.startLoop(incrementCounter);
stop = micros();
us = stop - start;
Serial.print(F("3. Start a task: "));
Serial.print(us);
Serial.println(F(" us"));
Serial.flush();
// 4. Measure yield between two tasks
start = micros();
for (int i = 0; i < 10000; i++)
yield();
stop = micros();
us = stop - start;
if (counter != 10000) Serial.println(F("Error: counter"));
Serial.print(F("4. Yield between two tasks: "));
Serial.print(us / 10000.0);
Serial.println(F(" us"));
Serial.flush();
// 5. Measure counter update during 10 ms delay
counter = 0;
start = micros();
delay(10);
stop = micros();
us = stop - start;
Serial.print(F("5. Delay 10 ms and check increments: "));
Serial.print(us);
Serial.print(F(":"));
Serial.print(counter);
Serial.print(F(", "));
Serial.print(us / (float) counter);
Serial.println(F(" us"));
Serial.flush();
// 6. Measure max number of tasks
start = micros();
nr = 0;
while (Scheduler.startLoop(incrementCounter)) nr++;
stop = micros();
us = stop - start;
Serial.print(F("6. Start "));
Serial.print(nr);
Serial.print(F(" tasks: "));
Serial.print(us);
Serial.print(F(":"));
Serial.print(nr);
Serial.print(F(", "));
Serial.print(us / (float) nr);
Serial.println(F(" us"));
Serial.flush();
// 7. Measure one yield cycle with max tasks
counter = 0;
start = micros();
yield();
stop = micros();
us = stop - start;
Serial.print(F("7. Yield and check increments: "));
Serial.print(us);
Serial.print(F(":"));
Serial.print(counter);
Serial.print(F(", "));
Serial.print(us / (float) counter);
Serial.println(F(" us"));
Serial.flush();
// 8. Measure counter update during 10 ms delay with max tasks
counter = 0;
start = micros();
delay(10);
stop = micros();
us = stop - start;
Serial.print(F("8. Delay 10 ms and check increments: "));
Serial.print(us);
Serial.print(F(":"));
Serial.print(counter);
Serial.print(F(", "));
Serial.print(us / (float) counter);
Serial.println(F(" us"));
Serial.flush();
}
void loop()
{
yield();
}
void incrementCounter()
{
counter += 1;
yield();
}