Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
SchedulerBenchmark.ino
Go to the documentation of this file.
1 
66 #include <Scheduler.h>
67 
68 // Check for SparkFun SAMD21 Breakout
69 #if defined(ARDUINO_ARCH_SAMD) && (USB_PID == 0x8D21)
70 #define Serial SerialUSB
71 #endif
72 
73 int counter = 0;
74 
75 void setup()
76 {
77  unsigned long start, stop, us;
78  int nr;
79 
80  Serial.begin(57600);
81  while (!Serial);
82  Serial.println(F("SchedulerBenchmark: started"));
83  Serial.flush();
84 
85  // 1. Measure initiate scheduler
86  start = micros();
87  Scheduler.begin(256);
88  stop = micros();
89  us = stop - start;
90  Serial.print(F("1. Initiate scheduler and main task: "));
91  Serial.print(us);
92  Serial.println(F(" us"));
93  Serial.flush();
94 
95  // 2. Measure yield with single task
96  start = micros();
97  for (int i = 0; i < 10000; i++)
98  yield();
99  stop = micros();
100  us = stop - start;
101  Serial.print(F("2. Yield main task: "));
102  Serial.print(us / 10000.0);
103  Serial.println(F(" us"));
104  Serial.flush();
105 
106  // 3. Measure start of task
107  start = micros();
109  stop = micros();
110  us = stop - start;
111  Serial.print(F("3. Start a task: "));
112  Serial.print(us);
113  Serial.println(F(" us"));
114  Serial.flush();
115 
116  // 4. Measure yield between two tasks
117  start = micros();
118  for (int i = 0; i < 10000; i++)
119  yield();
120  stop = micros();
121  us = stop - start;
122  if (counter != 10000) Serial.println(F("Error: counter"));
123  Serial.print(F("4. Yield between two tasks: "));
124  Serial.print(us / 10000.0);
125  Serial.println(F(" us"));
126  Serial.flush();
127 
128  // 5. Measure counter update during 10 ms delay
129  counter = 0;
130  start = micros();
131  delay(10);
132  stop = micros();
133  us = stop - start;
134  Serial.print(F("5. Delay 10 ms and check increments: "));
135  Serial.print(us);
136  Serial.print(F(":"));
137  Serial.print(counter);
138  Serial.print(F(", "));
139  Serial.print(us / (float) counter);
140  Serial.println(F(" us"));
141  Serial.flush();
142 
143  // 6. Measure max number of tasks
144  start = micros();
145  nr = 0;
146  while (Scheduler.startLoop(incrementCounter)) nr++;
147  stop = micros();
148  us = stop - start;
149  Serial.print(F("6. Start "));
150  Serial.print(nr);
151  Serial.print(F(" tasks: "));
152  Serial.print(us);
153  Serial.print(F(":"));
154  Serial.print(nr);
155  Serial.print(F(", "));
156  Serial.print(us / (float) nr);
157  Serial.println(F(" us"));
158  Serial.flush();
159 
160  // 7. Measure one yield cycle with max tasks
161  counter = 0;
162  start = micros();
163  yield();
164  stop = micros();
165  us = stop - start;
166  Serial.print(F("7. Yield and check increments: "));
167  Serial.print(us);
168  Serial.print(F(":"));
169  Serial.print(counter);
170  Serial.print(F(", "));
171  Serial.print(us / (float) counter);
172  Serial.println(F(" us"));
173  Serial.flush();
174 
175  // 8. Measure counter update during 10 ms delay with max tasks
176  counter = 0;
177  start = micros();
178  delay(10);
179  stop = micros();
180  us = stop - start;
181  Serial.print(F("8. Delay 10 ms and check increments: "));
182  Serial.print(us);
183  Serial.print(F(":"));
184  Serial.print(counter);
185  Serial.print(F(", "));
186  Serial.print(us / (float) counter);
187  Serial.println(F(" us"));
188  Serial.flush();
189 }
190 
191 void loop()
192 {
193  yield();
194 }
195 
197 {
198  counter += 1;
199  yield();
200 }
void loop()
int counter
static bool startLoop(func_t taskLoop, size_t stackSize=DEFAULT_STACK_SIZE)
Definition: Scheduler.h:65
static bool begin(size_t stackSize)
Definition: Scheduler.cpp:69
void setup()
void incrementCounter()
SchedulerClass Scheduler
Definition: Scheduler.cpp:53
void yield(void)
Definition: Scheduler.cpp:153