summaryrefslogtreecommitdiff
path: root/test/src/gtest_dynamic_benchmark.cpp
blob: 01e22e2e88a29865f5efdbbb8ead6fa6798c9ee5 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <gtest/gtest.h>
#include "fieldstat.h"
#include "fieldstat_internal.h"
#include "cJSON.h"
#include <math.h>

/*
spinlock, output interval: 1ms
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 |                 | Thread num 1           | Thread num 2           | Thread num 4           | Thread num 8           | Thread num 16          | Thread num 32          | Thread num 64          |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | operate counter | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1               | 45                     | 17                     | 18                     | 28					   | 48                     | 41					 | 35                     |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10              | 21                     | 19                     | 19                     | 17                     | 13                     | 10					 | 9					  |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100             | 49                     | 33                     | 33                     | 34                     | 32                     | 32 					 | 30                     |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1,000           | 217                    | 284                    | 321                    | 719					   | 536                    | 803                    | 1202                   |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10,000          | 1,961                  | 2,618                  | 3,742                  | 7,068                  | 9,943                  | 19,171                 | 16,970                 |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100,000         | 19,413                 | 29,401                 | 35,992				  | 65,337				   | 97,261                 | 135,835                | 197,634                |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1,000,000       | 193,769                | 278,782                | 359,098                | 883,753                | 1,493,073              | 1,592,048              | 1,974,276              |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10,000,000      | 1,948,891              | 3,861,912              | 5,796,965	          | 7,507,053              | 11,502,201             | 17,721,148             | 20,532,196             |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100,000,000     | 19,425,541             | 30,764,254             | 57,262,235             | 97,336,038             | 117,999,613            | 156,962,386            | 203,782,206            |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
/*
no spinlock, output interval: 1ms
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 |                 | Thread num 1           | Thread num 2           | Thread num 4           | Thread num 8           | Thread num 16          | Thread num 32          | Thread num 64          |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | operate counter | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1               | 39                     | 22                     | 25                     | 81					   | 65                     | 41					 | 45                     |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10              | 34                     | 12                     | 13                     | 8                      | 11                     | 11					 | 9					  |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100             | 30                     | 32                     | 30                     | 29                     | 30                     | 30 					 | 27                     |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1,000           | 201                    | 205                    | 201                    | 205					   | 207                    | 204                    | 202                    |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10,000          | 1,928                  | 1,912                  | 1,901                  | 1,905                  | 1,901                  | 19,215                 | 1,967                  |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100,000         | 18,742                 | 18,674                 | 18,917				  | 18,801				   | 18,815                 | 193,246                | 23,323                 |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1,000,000       | 187,415                | 189,673                | 188,229                | 188,012                | 187,757                | 193,246                | 309,948                |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10,000,000      | 1,879,720              | 1,888,791              | 1,880,554	          | 1,883,515              | 1,882,172              | 1,892,792              | 2,681,721              |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100,000,000     | 18,784,181             | 18,827,600             | 18,812,896             | 18,875,007             | 18,833,475             | 18,811,591             | 26,965,032             |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
/*
spinlock with Struct Alignment, output interval: 1ms
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 |                 | Thread num 1           | Thread num 2           | Thread num 4           | Thread num 8           | Thread num 16          | Thread num 32          | Thread num 64          |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | operate counter | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) | operation duration(us) |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1               | 46                     | 18                     | 19                     | 22					   | 40                     | 53					 | 38                     |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10              | 25                     | 13                     | 14                     | 12                     | 12                     | 10					 | 9					  |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100             | 33                     | 31                     | 33                     | 30                     | 32                     | 30 					 | 30                     |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1,000           | 212                    | 207                    | 536                    | 205					   | 207                    | 207                    | 204                    |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10,000          | 1,882                  | 1,895                  | 4,333                  | 1,890                  | 1,899                  | 1,898                  | 1,907                  |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100,000         | 18,751                 | 18,823                 | 32,792				  | 19,100				   | 18,839                 | 18,860                 | 20,919                 |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 1,000,000       | 187,274                | 187,520                | 187,618                | 188,000                | 188,097                | 191,853                | 248,770                |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 10,000,000      | 18,731,69              | 1,874,701              | 1,875,510	          | 1,875,648              | 1,878,946              | 1,919,397              | 2,523,767              |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 | 100,000,000     | 18,776,370             | 18,781,233             | 18,767,859             | 18,768,590             | 18,830,596             | 18,799,717             | 27,003,506             |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

struct thread_para
{
	int loops;
	int thread_id;
	long long duration;
	struct fieldstat_dynamic_instance *instance;
};

long long current_timestamp()
{
    struct timeval te;
    gettimeofday(&te, NULL);
    return te.tv_sec * 1000LL * 1000LL  + te.tv_usec;
}

void _worker_thread_one_metric(void *arg)
{
	struct thread_para *para = (struct thread_para*)arg;

	int loops = para->loops;
	int thread_id = para->thread_id;
	struct fieldstat_dynamic_instance *instance = para->instance;

	int ret = 0;
	long long start_time, end_time;

	start_time = current_timestamp();
	for(int i = 0; i < loops; i++)
	{
		ret = fieldstat_dynamic_metric_value_incrby(instance, FIELD_TYPE_GAUGE,
													"Active_sessions", 10, 
													NULL, 0, thread_id); 
		EXPECT_EQ(0, ret);
	}
	end_time =current_timestamp();
	para->duration = end_time - start_time;

	return;
}
void *worker_thread_one_metric(void *arg)
{
	_worker_thread_one_metric(arg);
	usleep(1000 * 100);
	return NULL;
}

static void fieldstat_dynamic_benchmark(int n_thread, int n_loops)
{
	int ret = 0;
	//int n_thread = 4;
	//int n_loops = 10;
	struct fieldstat_dynamic_instance *instance = NULL;
	struct thread_para para[n_thread];
	pthread_t thread_ids[n_thread];
	long long sum_durations = 0;

	instance = fieldstat_dynamic_instance_new("firewall", n_thread);
	ret = fieldstat_dynamic_set_line_protocol_server(instance, "127.0.0.1", 8700);
	EXPECT_EQ(0, ret);
	// ret = fieldstat_dynamic_set_output_interval(instance, 1);
	// EXPECT_EQ(0, ret);

	fieldstat_dynamic_instance_start(instance);

	for(int i = 0; i < n_thread; i++)
	{
		para[i].loops = n_loops;
		para[i].instance = instance;
		para[i].thread_id = i;
	}

	for(int i = 0; i < n_thread; i++)
	{
		ret = pthread_create(&(thread_ids[i]), NULL, worker_thread_one_metric, &(para[i]));
		EXPECT_EQ(0, ret);
	}

	void *temp;
	for(int i = 0; i < n_thread; i++)
	{
		pthread_join(thread_ids[i], (void**)&temp);
		sum_durations += para[i].duration;
		//printf("Thread id:%d, operate counter:%d, duration:%lldus\n", i, n_loops, para[i].duration);
	}
	printf("Thread num:%d, operate counter:%d, duration:%lldus\n", n_thread, n_loops, sum_durations/n_thread);
	sleep(2);
	fieldstat_dynamic_instance_free(instance);
}

// TEST(FeildStatDynamicAPI, NThread64Counter10Million)
// {
// 	int n_thread = 0;
// 	int n_loops = 0;

// 	for(int i = 0; i < 7; i++)
// 	{
// 		n_thread = 1 << i;
// 		for(int j = 0; j < 9; j++)
// 		{
// 			n_loops = (int)pow(10, (double)j);
// 			fieldstat_dynamic_benchmark(n_thread, n_loops);
// 		}
// 	}
// }

TEST(FeildStatDynamicAPI, AllConditions)
{
	int n_thread = 0;
	int n_loops = 0;

	for(int i = 0; i < 7; i++)
	{
		n_thread = 1 << i;
		for(int j = 0; j < 9; j++)
		{
			n_loops = (int)pow(10, (double)j);
			fieldstat_dynamic_benchmark(n_thread, n_loops);
		}
	}
}



int main(int argc, char *argv[]) 
{
	testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}