-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark.ino
More file actions
139 lines (114 loc) · 2.97 KB
/
benchmark.ino
File metadata and controls
139 lines (114 loc) · 2.97 KB
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
/**
* Example to demonstate the speed of ArduinoSimpleLogging
*
* https://github.com/janLo/ArduinoSimpleLogging
*
* Copyright (c) 2017 Puuu
* MIT License
*/
#include <ArduinoSimpleLogging.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#define MSG "This is a test log message!"
const char msg[] = MSG;
const unsigned int repeats = 1000;
String msgString(MSG);
class PrintMock : public Print {
public:
unsigned calls;
size_t write(uint8_t) override {
calls++;
return 1;
}
size_t write(const uint8_t *, size_t length) override {
calls++;
return length;
}
} printMock;
void log_char(Print &logger) {
for (unsigned int i = 0; i < repeats; i++) {
logger.println(msg);
}
}
void log_singlechar(Print &logger) {
for (unsigned int i = 0; i < repeats; i++) {
for (unsigned int c = 0; c < sizeof(msg)-1; c++) {
logger.write((uint8_t)msg[c]);
}
logger.write((uint8_t)'\n');
}
}
void log_progmem(Print &logger) {
for (unsigned int i = 0; i < repeats; i++) {
logger.println(F(MSG));
}
}
void log_String(Print &logger) {
for (unsigned int i = 0; i < repeats; i++) {
logger.println(msgString);
}
}
unsigned long measure_it(Print &logger, void (*func)(Print &)) {
unsigned long start = micros();
func(logger);
unsigned long end = micros();
return end - start;
}
void summarize(unsigned long duration) {
Serial.print("duration ");
Serial.print(duration * 1000 / repeats);
Serial.print(" us, ");
Serial.print(printMock.calls / repeats);
Serial.println(" calls");
}
void log(Print &logger) {
unsigned long duration;
Serial.println("char string");
printMock.calls = 0;
duration = measure_it(logger, log_char);
summarize(duration);
Serial.println("single chars");
printMock.calls = 0;
duration = measure_it(logger, log_singlechar);
summarize(duration);
Serial.println("PROGMEM string");
printMock.calls = 0;
duration = measure_it(logger, log_progmem);
summarize(duration);
Serial.println("String object");
printMock.calls = 0;
duration = measure_it(logger, log_String);
summarize(duration);
}
void setup() {
Serial.begin(115200);
#ifdef ESP8266
// disable wifi for benchmarking
WiFi.mode(WIFI_OFF);
#endif
delay(500);
Serial.println();
// No handler configured - no output
Serial.println("------------------------");
Serial.println("No Handler ...");
log(Logger.debug);
Serial.println("------------------------");
Serial.println();
// Handler for error, logging to debug
Serial.println("------------------------");
Serial.println("Handler for error, logging to debug ...");
Logger.addHandler(Logger.ERROR, printMock);
log(Logger.debug);
Serial.println("------------------------");
Serial.println();
// Handler for debug, logging to debug
Serial.println("------------------------");
Serial.println("Handler for error, logging to error ...");
log(Logger.error);
Serial.println("------------------------");
Serial.println();
}
void loop() {
// do nothing
}