├── LICENSE
├── README.md
├── client
├── client.go
├── fahongbao.sh
├── qps.sh
├── run.sh
├── upload.sh
└── yaohongbao.sh
├── images
├── 100w.png
├── 2.png
├── client_qps.png
├── cpu.png
├── create_hongbao.png
├── fa.png
├── get_hongbao.png
├── log.png
├── machine.png
├── monitor.png
├── net_3w.png
├── net_6w.png
├── objectdiagrams.png
├── png
│ ├── 2_01.png
│ ├── 2_02.png
│ ├── 2_03.png
│ ├── 2_04.png
│ ├── 2_05.png
│ ├── 2_06.png
│ ├── 2_07.png
│ ├── 2_08.png
│ ├── 2_09.png
│ ├── 2_10.png
│ ├── 2_11.png
│ ├── 2_12.png
│ ├── 2_13.png
│ ├── 2_14.png
│ ├── 2_15.png
│ ├── 2_16.png
│ ├── 2_17.png
│ ├── 2_18.png
│ ├── 2_19.png
│ ├── 2_20.png
│ ├── 2_21.png
│ ├── 2_22.png
│ ├── 2_23.png
│ ├── 2_24.png
│ └── 2_25.png
├── pprof.png
├── qps.png
├── server_qps.png
├── start_qps.png
└── yao.png
├── lib
├── flags
│ └── flags.go
├── mailbox
│ ├── mailbox.go
│ └── mailbox_test.go
├── message
│ ├── message.go
│ └── message_test.go
├── message2
│ ├── message2.pb.go
│ ├── message2.proto
│ └── message2_test.go
├── monitor
│ ├── monitor.go
│ └── monitor_test.go
├── parse
│ └── parse.go
├── qps
│ ├── qps.go
│ └── qps_test.go
└── utils
│ └── utils.go
├── linux
├── create_run.py
├── limits.conf
└── sysctl.conf
├── monitor
└── monitor.go
├── plot
├── hongbao.plt
└── parselog.py
└── server
└── server.go
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 10billionhongbaos
2 |
3 | 一篇习作,教你如何实现单机支持100万用户,同时模拟了2015年微信红包的1400万QPS的场景,让服务器在压力下,轻松地完成业务。
4 |
5 | 技术要点:
6 |
7 | * 优化普通服务器的参数,巧妙设计代码做到单机支持百万用户
8 | * 单机可以支持QPS达 6万,可以满足100亿红包的压力测试。
9 | * 完善的监控体系,验证了模拟是真实可靠的
10 | * 简单的代码实现,希望可以为你的开发做些参考
11 |
12 | 文档链接:[文档链接]( https://github.com/xiaojiaqi/10billionhongbaos/wiki/%E6%89%9B%E4%BD%8F100%E4%BA%BF%E6%AC%A1%E8%AF%B7%E6%B1%82%EF%BC%9F%E6%88%91%E4%BB%AC%E6%9D%A5%E8%AF%95%E4%B8%80%E8%AF%95)
13 |
14 | 觉得有用,请告诉我
15 |
16 |
17 |
18 | 觉得不错,支持一下
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/client/client.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "github.com/10billionhongbaos/lib/flags"
6 | . "github.com/10billionhongbaos/lib/message"
7 | "github.com/10billionhongbaos/lib/monitor"
8 | "github.com/10billionhongbaos/lib/parse"
9 | "github.com/10billionhongbaos/lib/qps"
10 | "github.com/10billionhongbaos/lib/utils"
11 | "net"
12 | "net/http"
13 | "runtime"
14 | "strconv"
15 | "time"
16 | )
17 |
18 | func readConn(conn *net.Conn, userid uint32, gPool *MessagePool) {
19 |
20 | defer (*conn).Close()
21 | for {
22 | p, result := parse.ReadMessage(conn, gPool)
23 | if !result {
24 | break
25 | }
26 | //fmt.Println(p.Cmd)
27 | tnow := time.Now().Unix()
28 | monitor.GMonitor.Add(p.Cmd, 1, tnow)
29 |
30 | monitor.GMonitor.Add(monitor.Response, 1, tnow)
31 | switch p.Cmd {
32 | case YaoHongbaoResponse_ID:
33 | {
34 | monitor.GMonitor.Add(int(p.P4.Result), 1, tnow)
35 | if int(p.P4.Result) == monitor.GetHongbao {
36 | monitor.GMonitor.Add2(monitor.CountYaoHongbaoReq, 1)
37 | monitor.GMonitor.Add2(monitor.SumGetYaohoHongbao, int64(p.P4.Amount))
38 | }
39 | }
40 | case FaHongbaoRequest_ID:
41 | {
42 | // send a GetHongbaoRequest_ID
43 | // bug: 存着 竞争问题
44 |
45 | // fmt.Println("got a fahonbao request", " userid",userid, p.P5.Hongbaoid, p.P5.Userid1, p.P5.Userid2,p.P5.Userid3)
46 | {
47 | a := gPool.Pop()
48 | a.Cmd = GetHongbaoRequest_ID
49 | p2 := &GetHongbaoRequest{}
50 | p2.Hongbaoid = p.P5.Hongbaoid
51 | a.P6 = p2
52 |
53 | parse.WriteMessage(conn, a)
54 | monitor.GMonitor.Add2(monitor.CountFaHongbaoReq, 1)
55 | monitor.GMonitor.Add(GetHongbaoRequest_ID, 1, time.Now().Unix())
56 | }
57 | }
58 |
59 | case GetHongbaoRespone_ID:
60 | {
61 |
62 | // fmt.Println("got a GetHongbaoRespone_ID", userid, p.P7.Hongbaoid, p.P7.Amount)
63 | monitor.GMonitor.Add2(monitor.CountGetFaHongbaoReq, 1)
64 | monitor.GMonitor.Add2(monitor.SumGetFaHongbao, int64(p.P7.Amount))
65 | }
66 |
67 | }
68 | p.Rollback()
69 |
70 | }
71 |
72 | }
73 |
74 | func sender(conn net.Conn, userid uint32, gPool *MessagePool) bool {
75 |
76 | {
77 | a := gPool.Pop()
78 | a.Userid = uint(userid)
79 | a.Cmd = YaoHongbaoRequest_ID
80 | p := YaoHongbaoRequest{}
81 | p.Userid = userid
82 | a.P3 = &p
83 |
84 | monitor.GMonitor.Add(monitor.Request, 1, time.Now().Unix())
85 |
86 | err := parse.WriteMessage(&conn, a)
87 | if err != nil {
88 | return false
89 | }
90 |
91 | }
92 |
93 | return true
94 |
95 | }
96 |
97 | func OneClient(userid uint32) {
98 | var gPool MessagePool
99 |
100 | gPool.Init(30, 30)
101 | for k := 0; k < 30; k++ {
102 | p := &Message{}
103 | p.Init()
104 | p.Pool = &gPool
105 | gPool.Push(p)
106 | }
107 |
108 | conn, err := net.Dial("tcp", *(flags.Server))
109 | if err != nil {
110 | //fmt.Println(err)
111 | return
112 | }
113 | defer conn.Close()
114 | go readConn(&conn, userid, &gPool)
115 |
116 | utils.CorrectSleepOneSecond()
117 |
118 | // send a login request
119 |
120 | {
121 | a := gPool.Pop()
122 | a.Userid = uint(userid)
123 | a.Cmd = LoginRequest_ID
124 | p := LoginRequest{}
125 | p.Userid = userid
126 | a.P1 = &p
127 |
128 | monitor.GMonitor.Add(monitor.Request, 1, time.Now().Unix())
129 |
130 | err := parse.WriteMessage(&conn, a)
131 | if err != nil {
132 | return
133 | }
134 | }
135 |
136 | utils.CorrectSleepOneSecond()
137 |
138 | for {
139 | tnow := time.Now()
140 | if qps.TriggerRequest2(userid, flags.TotalUser, flags.QPS, time.Now().Unix()) {
141 | // fmt.Println("time: ", time.Now().Unix(), " userid ", userid, " send ", conn.LocalAddr())
142 |
143 | // time.Sleep(300* time.Microsecond)
144 | if !sender(conn, userid, &gPool) {
145 | return
146 | }
147 | }
148 | utils.CorrectSleepOneSecond2(tnow)
149 | }
150 | }
151 |
152 | func Setqps(w http.ResponseWriter, req *http.Request) {
153 |
154 | req.ParseForm()
155 | fmt.Println(req.Form)
156 | filelist, found1 := req.Form["qps"]
157 |
158 | if !(found1) {
159 | fmt.Println("no id")
160 | w.Write([]byte("no"))
161 | return
162 | }
163 |
164 | nowQps, err := strconv.Atoi(filelist[0])
165 | if err != nil {
166 | return
167 | }
168 |
169 | flags.QPS = uint32(nowQps)
170 | }
171 |
172 | func httpServer() {
173 |
174 | http.HandleFunc("/qps", Setqps)
175 | //服务器要监听的主机地址和端口号
176 | err := http.ListenAndServe("0.0.0.0:9090", nil)
177 | utils.CheckErr(err)
178 |
179 | }
180 |
181 | func runOneClient(i uint32) {
182 | for {
183 | time.Sleep(time.Duration(int(i)%20) * time.Second)
184 | OneClient(i)
185 | }
186 | }
187 |
188 | func main() {
189 | flags.Parse()
190 | runtime.GOMAXPROCS(runtime.NumCPU())
191 | go monitor.Show(*(flags.MonitorServer), 0)
192 | go httpServer()
193 | var i uint32
194 | fmt.Println(flags.MinUserId, flags.MaxUserId, flags.TotalUser)
195 | for i = flags.MinUserId; i <= flags.MaxUserId; i++ {
196 | go runOneClient(i)
197 | }
198 |
199 | for {
200 | utils.CorrectSleepOneSecond()
201 |
202 | }
203 |
204 | }
205 |
--------------------------------------------------------------------------------
/client/fahongbao.sh:
--------------------------------------------------------------------------------
1 | curl "http://10.29.101.192:8989/fahongbao?qps=200&sum=20000&min=0&max=599999"
2 |
--------------------------------------------------------------------------------
/client/qps.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 |
4 | for i in 182 183 184 185 186 187 188 189 190 191 193 194 195 155 157 158
5 | do
6 | echo $i
7 | curl "http://10.29.101.$i:9090/qps?qps=$1"
8 | done
9 |
--------------------------------------------------------------------------------
/client/run.sh:
--------------------------------------------------------------------------------
1 | ./client -min 540000 -max 599999 -total 600000 -qps 30
2 |
--------------------------------------------------------------------------------
/client/upload.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | index=0
4 | for i in 182 183 184 185 186 187 188 189 190 191 193 194 195 155 156 157 158
5 | do
6 | echo $i
7 | scp client 10.29.101.$i:~/
8 | done
9 |
--------------------------------------------------------------------------------
/client/yaohongbao.sh:
--------------------------------------------------------------------------------
1 | curl "http://10.29.101.192:8989/yaohongbao?qps=200&sum=40000"
2 |
3 |
--------------------------------------------------------------------------------
/images/100w.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/100w.png
--------------------------------------------------------------------------------
/images/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/2.png
--------------------------------------------------------------------------------
/images/client_qps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/client_qps.png
--------------------------------------------------------------------------------
/images/cpu.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/cpu.png
--------------------------------------------------------------------------------
/images/create_hongbao.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/create_hongbao.png
--------------------------------------------------------------------------------
/images/fa.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/fa.png
--------------------------------------------------------------------------------
/images/get_hongbao.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/get_hongbao.png
--------------------------------------------------------------------------------
/images/log.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/log.png
--------------------------------------------------------------------------------
/images/machine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/machine.png
--------------------------------------------------------------------------------
/images/monitor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/monitor.png
--------------------------------------------------------------------------------
/images/net_3w.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/net_3w.png
--------------------------------------------------------------------------------
/images/net_6w.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/net_6w.png
--------------------------------------------------------------------------------
/images/objectdiagrams.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/objectdiagrams.png
--------------------------------------------------------------------------------
/images/png/2_01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_01.png
--------------------------------------------------------------------------------
/images/png/2_02.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_02.png
--------------------------------------------------------------------------------
/images/png/2_03.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_03.png
--------------------------------------------------------------------------------
/images/png/2_04.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_04.png
--------------------------------------------------------------------------------
/images/png/2_05.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_05.png
--------------------------------------------------------------------------------
/images/png/2_06.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_06.png
--------------------------------------------------------------------------------
/images/png/2_07.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_07.png
--------------------------------------------------------------------------------
/images/png/2_08.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_08.png
--------------------------------------------------------------------------------
/images/png/2_09.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_09.png
--------------------------------------------------------------------------------
/images/png/2_10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_10.png
--------------------------------------------------------------------------------
/images/png/2_11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_11.png
--------------------------------------------------------------------------------
/images/png/2_12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_12.png
--------------------------------------------------------------------------------
/images/png/2_13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_13.png
--------------------------------------------------------------------------------
/images/png/2_14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_14.png
--------------------------------------------------------------------------------
/images/png/2_15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_15.png
--------------------------------------------------------------------------------
/images/png/2_16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_16.png
--------------------------------------------------------------------------------
/images/png/2_17.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_17.png
--------------------------------------------------------------------------------
/images/png/2_18.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_18.png
--------------------------------------------------------------------------------
/images/png/2_19.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_19.png
--------------------------------------------------------------------------------
/images/png/2_20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_20.png
--------------------------------------------------------------------------------
/images/png/2_21.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_21.png
--------------------------------------------------------------------------------
/images/png/2_22.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_22.png
--------------------------------------------------------------------------------
/images/png/2_23.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_23.png
--------------------------------------------------------------------------------
/images/png/2_24.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_24.png
--------------------------------------------------------------------------------
/images/png/2_25.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/png/2_25.png
--------------------------------------------------------------------------------
/images/pprof.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/pprof.png
--------------------------------------------------------------------------------
/images/qps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/qps.png
--------------------------------------------------------------------------------
/images/server_qps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/server_qps.png
--------------------------------------------------------------------------------
/images/start_qps.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/start_qps.png
--------------------------------------------------------------------------------
/images/yao.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xiaojiaqi/10billionhongbaos/74987479c98772f40a42369701fba265af3f6bbb/images/yao.png
--------------------------------------------------------------------------------
/lib/flags/flags.go:
--------------------------------------------------------------------------------
1 | package flags
2 |
3 | import (
4 | "flag"
5 | )
6 |
7 | var ListPort *string
8 |
9 | var Server *string
10 | var MonitorServer *string
11 |
12 | var QPS uint32
13 | var MinUserId uint32
14 | var MaxUserId uint32
15 | var TotalUser uint32
16 |
17 | var PQPS *uint
18 | var PMinUserId *uint
19 | var PMaxUserId *uint
20 | var PTotalUser *uint
21 |
22 | func Parse() {
23 |
24 | ListPort = flag.String("list", "1025", "list port")
25 |
26 | Server = flag.String("server", "10.29.101.192:1025", "server address")
27 | MonitorServer = flag.String("monitor", "10.29.101.3:8002", "Monitor server address")
28 |
29 | PQPS = flag.Uint("qps", 100, "QPS")
30 | PMinUserId = flag.Uint("min", 0, "MinUserId")
31 | PMaxUserId = flag.Uint("max", 99, "MaxUserId")
32 | PTotalUser = flag.Uint("total", 100, "TotalUser")
33 | flag.Parse()
34 |
35 | QPS = (uint32)(*PQPS)
36 | MinUserId = (uint32)(*PMinUserId)
37 | MaxUserId = (uint32)(*PMaxUserId)
38 | TotalUser = (uint32)(*PTotalUser)
39 | }
40 |
--------------------------------------------------------------------------------
/lib/mailbox/mailbox.go:
--------------------------------------------------------------------------------
1 | package mailbox
2 |
3 | import (
4 | . "github.com/10billionhongbaos/lib/message"
5 | . "github.com/10billionhongbaos/lib/utils"
6 |
7 | "net"
8 | )
9 |
10 | type MailBox struct {
11 | mailbox chan *Message
12 | Net []*net.Conn
13 | index uint32
14 | }
15 |
16 | type MessageProcess struct {
17 | InputBox MailBox
18 | YaoBox MailBox
19 | }
20 |
21 | func (p *MailBox) Init(i int) {
22 | p.mailbox = make(chan *Message, i)
23 | p.Net = make([]*net.Conn, i)
24 | }
25 |
26 | func (p *MailBox) Push(p1 *Message) bool {
27 |
28 | select {
29 | case p.mailbox <- p1:
30 | return true
31 | default:
32 | return false
33 | }
34 |
35 | }
36 |
37 | func (p *MailBox) Pop() (*Message, bool) {
38 |
39 | var p1 *Message
40 | select {
41 | case p1 := <-p.mailbox:
42 | return p1, true
43 | default:
44 | return p1, false
45 | }
46 |
47 | }
48 |
49 | func (p *MailBox) BlockingPop() (*Message, bool) {
50 |
51 | var p1 *Message
52 | p1 = <-p.mailbox
53 | return p1, true
54 |
55 | }
56 |
57 | func (p *MessageProcess) Init(i1 int, i2 int) {
58 |
59 | p.InputBox.Init(i1)
60 | p.YaoBox.Init(i2)
61 |
62 | }
63 |
64 | func (p *MessageProcess) SetConn(p1 *net.Conn, mailboxnum int, userid int) {
65 | _, y := GetMailBoxAddress(mailboxnum, userid)
66 | p.InputBox.Net[y] = p1
67 | }
68 |
69 | func (p *MessageProcess) GetConn(mailboxnum int, userid int) *net.Conn {
70 |
71 | _, y := GetMailBoxAddress(mailboxnum, userid)
72 | return p.InputBox.Net[y]
73 | }
74 |
75 | func (p *MessageProcess) PushInput(p1 *Message) bool {
76 |
77 | return p.InputBox.Push(p1)
78 | }
79 |
80 | func (p *MessageProcess) PushYao(p1 *Message) bool {
81 | return p.YaoBox.Push(p1)
82 |
83 | }
84 |
85 | func (p *MessageProcess) PopInput() (*Message, bool) {
86 | return p.InputBox.BlockingPop()
87 | }
88 |
89 | func (p *MessageProcess) PopYao() (*Message, bool) {
90 | return p.YaoBox.Pop()
91 | }
92 |
--------------------------------------------------------------------------------
/lib/mailbox/mailbox_test.go:
--------------------------------------------------------------------------------
1 | package mailbox
2 |
3 | import (
4 | . "github.com/10billionhongbaos/lib/message"
5 | "testing"
6 | )
7 |
8 | const (
9 | randomid = 12345
10 | )
11 |
12 | func Test_mailbox(t *testing.T) {
13 | a := MailBox{}
14 | a.Init(1000)
15 | for i := 0; i < 1000; i++ {
16 | var x Message
17 | b := a.Push(&x)
18 | if b != true {
19 | t.Error("wrong")
20 | }
21 | }
22 |
23 | var x Message
24 | b := a.Push(&x)
25 | if b != false {
26 | t.Error("wrong")
27 | }
28 |
29 | for i := 0; i < 1000; i++ {
30 | _, b := a.Pop()
31 | if b != true {
32 | t.Error("wrong")
33 | }
34 | }
35 |
36 | _, b2 := a.Pop()
37 | if b2 != false {
38 | t.Error("wrong")
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/lib/message/message.go:
--------------------------------------------------------------------------------
1 | package message
2 |
3 | import (
4 | // "fmt"
5 | "encoding/binary"
6 | "net"
7 | "sync"
8 | "time"
9 | )
10 |
11 | const (
12 | HeaderLength = 12 // 2 字节长度+2 字节 类型 + 8 字节时间戳
13 | )
14 |
15 | // 用户登录
16 | type LoginRequest struct {
17 | Userid uint32
18 | }
19 |
20 | func (p *LoginRequest) GetLength() int {
21 | return 4
22 | }
23 |
24 | // 用户登录回应
25 | type LoginResponse struct {
26 | Userid uint32
27 | Result uint32
28 | }
29 |
30 | func (p *LoginResponse) GetLength() int {
31 | return 4 + 4
32 | }
33 |
34 | // 摇红包
35 | type YaoHongbaoRequest struct {
36 | Userid uint32
37 | }
38 |
39 | func (p *YaoHongbaoRequest) GetLength() int {
40 | return 4
41 | }
42 |
43 | // 摇红包回应
44 | type YaoHongbaoResponse struct {
45 | Userid uint32
46 | Result uint32 // 00 没摇dao
47 | // 01 摇到了
48 | // 02 系统过载
49 | Hongbaoid uint32
50 | Amount uint32
51 | }
52 |
53 | func (p *YaoHongbaoResponse) GetLength() int {
54 | return 4 * 4
55 | }
56 |
57 | // 发红包
58 | type FaHongbaoRequest struct {
59 | Hongbaoid uint32
60 | Amount uint32
61 | Useramount uint32
62 | Userid1 uint32
63 | Userid2 uint32
64 | Userid3 uint32
65 | }
66 |
67 | func (p *FaHongbaoRequest) GetLength() int {
68 | return 4 * 6
69 | }
70 |
71 | // 拿红包
72 | type GetHongbaoRequest struct {
73 | Userid uint32
74 | Hongbaoid uint32
75 | }
76 |
77 | func (p *GetHongbaoRequest) GetLength() int {
78 | return 4 * 2
79 | }
80 |
81 | // 拿红包回应
82 | type GetHongbaoRespone struct {
83 | Hongbaoid uint32
84 | Amount uint32
85 | }
86 |
87 | func (p *GetHongbaoRespone) GetLength() int {
88 | return 4 * 2
89 | }
90 |
91 | // 还应该有聊天什么的
92 | // ...
93 | const (
94 | LoginRequest_ID = 0x0000
95 | LoginResponse_ID = 0x0001
96 | YaoHongbaoRequest_ID = 0x0002
97 | YaoHongbaoResponse_ID = 0x0003
98 | FaHongbaoRequest_ID = 0x0004
99 | GetHongbaoRequest_ID = 0x0005
100 | GetHongbaoRespone_ID = 0x0006
101 | )
102 |
103 | type Message struct {
104 | Userid uint
105 | Cmd int
106 | TimeStamp uint64
107 | Conn *net.Conn
108 | P1 *LoginRequest
109 | P2 *LoginResponse
110 | P3 *YaoHongbaoRequest
111 | P4 *YaoHongbaoResponse
112 | P5 *FaHongbaoRequest
113 | P6 *GetHongbaoRequest
114 | P7 *GetHongbaoRespone
115 | Buff []byte
116 | Bufflen int
117 | Pool *MessagePool
118 | }
119 |
120 | func (p *Message) Init() {
121 | p.Buff = make([]byte, 100)
122 | p.Bufflen = 0
123 | }
124 |
125 | func (p *Message) SetCmd(i int) {
126 | p.Cmd = i
127 | }
128 |
129 | /*
130 | Message format
131 | 0-1 length
132 | 2-3 cmdtype
133 | */
134 |
135 | func (p *LoginRequest) serialization(buff []byte) {
136 | binary.BigEndian.PutUint32(buff, p.Userid)
137 | //fmt.Println(p.Userid, buff)
138 | }
139 |
140 | func (p *LoginRequest) unserialization(buff []byte) {
141 |
142 | p.Userid = binary.BigEndian.Uint32(buff)
143 | //fmt.Println(p.Userid, buff)
144 | }
145 |
146 | func (p *LoginResponse) serialization(buff []byte) {
147 | binary.BigEndian.PutUint32(buff, p.Userid)
148 | binary.BigEndian.PutUint32(buff[4:], p.Result)
149 | // fmt.Println(p.Userid, p.Result,buff)
150 | }
151 |
152 | func (p *LoginResponse) unserialization(buff []byte) {
153 | p.Userid = binary.BigEndian.Uint32(buff)
154 | p.Result = binary.BigEndian.Uint32(buff[4:])
155 | // fmt.Println(p.Userid, p.Result,buff)
156 | }
157 |
158 | func (p *YaoHongbaoRequest) serialization(buff []byte) {
159 |
160 | binary.BigEndian.PutUint32(buff, p.Userid)
161 | //fmt.Println(p.Userid, buff)
162 | }
163 |
164 | func (p *YaoHongbaoRequest) unserialization(buff []byte) {
165 |
166 | p.Userid = binary.BigEndian.Uint32(buff)
167 | //fmt.Println(p.Userid, buff)
168 | }
169 |
170 | func (p *YaoHongbaoResponse) serialization(buff []byte) {
171 |
172 | binary.BigEndian.PutUint32(buff, p.Userid)
173 | binary.BigEndian.PutUint32(buff[4:], p.Result)
174 |
175 | binary.BigEndian.PutUint32(buff[8:], p.Hongbaoid)
176 |
177 | binary.BigEndian.PutUint32(buff[12:], p.Amount)
178 |
179 | //fmt.Println(p.Userid, buff)
180 | }
181 |
182 | func (p *YaoHongbaoResponse) unserialization(buff []byte) {
183 |
184 | p.Userid = binary.BigEndian.Uint32(buff)
185 | p.Result = binary.BigEndian.Uint32(buff[4:])
186 |
187 | p.Hongbaoid = binary.BigEndian.Uint32(buff[8:])
188 | p.Amount = binary.BigEndian.Uint32(buff[12:])
189 |
190 | //fmt.Println(p.Userid, buff)
191 | }
192 |
193 | func (p *FaHongbaoRequest) serialization(buff []byte) {
194 |
195 | binary.BigEndian.PutUint32(buff, p.Hongbaoid)
196 | binary.BigEndian.PutUint32(buff[4:], p.Amount)
197 | binary.BigEndian.PutUint32(buff[8:], p.Useramount)
198 | binary.BigEndian.PutUint32(buff[12:], p.Userid1)
199 | binary.BigEndian.PutUint32(buff[16:], p.Userid2)
200 | binary.BigEndian.PutUint32(buff[20:], p.Userid3)
201 |
202 | //fmt.Println(p.Userid, buff)
203 | }
204 |
205 | func (p *FaHongbaoRequest) unserialization(buff []byte) {
206 |
207 | p.Hongbaoid = binary.BigEndian.Uint32(buff)
208 | p.Amount = binary.BigEndian.Uint32(buff[4:])
209 | p.Useramount = binary.BigEndian.Uint32(buff[8:])
210 | p.Userid1 = binary.BigEndian.Uint32(buff[12:])
211 | p.Userid2 = binary.BigEndian.Uint32(buff[16:])
212 | p.Userid3 = binary.BigEndian.Uint32(buff[20:])
213 | //fmt.Println(p.Userid, buff)
214 | }
215 |
216 | func (p *GetHongbaoRequest) serialization(buff []byte) {
217 |
218 | binary.BigEndian.PutUint32(buff, p.Userid)
219 | binary.BigEndian.PutUint32(buff[4:], p.Hongbaoid)
220 | }
221 |
222 | func (p *GetHongbaoRequest) unserialization(buff []byte) {
223 |
224 | p.Userid = binary.BigEndian.Uint32(buff)
225 | p.Hongbaoid = binary.BigEndian.Uint32(buff[4:])
226 | }
227 |
228 | func (p *GetHongbaoRespone) serialization(buff []byte) {
229 |
230 | binary.BigEndian.PutUint32(buff, p.Hongbaoid)
231 | binary.BigEndian.PutUint32(buff[4:], p.Amount)
232 | }
233 |
234 | func (p *GetHongbaoRespone) unserialization(buff []byte) {
235 |
236 | p.Hongbaoid = binary.BigEndian.Uint32(buff)
237 | p.Amount = binary.BigEndian.Uint32(buff[4:])
238 | }
239 |
240 | func (p *Message) Serialization() []byte {
241 | len := HeaderLength
242 | var buff []byte
243 | switch p.Cmd {
244 |
245 | case LoginRequest_ID:
246 | {
247 | len += p.P1.GetLength()
248 | buff = p.Buff[0:len]
249 | p.Bufflen = len
250 | p.P1.serialization(buff[HeaderLength:])
251 | }
252 |
253 | case LoginResponse_ID:
254 | {
255 | len += p.P2.GetLength()
256 | //buff = make([]byte, len)
257 | buff = p.Buff[0:len]
258 |
259 | p.Bufflen = len
260 | p.P2.serialization(buff[HeaderLength:])
261 |
262 | }
263 |
264 | case YaoHongbaoRequest_ID:
265 | {
266 |
267 | len += p.P3.GetLength()
268 | //buff = make([]byte, len)
269 | buff = p.Buff[0:len]
270 |
271 | p.Bufflen = len
272 | p.P3.serialization(buff[HeaderLength:])
273 | }
274 |
275 | case YaoHongbaoResponse_ID:
276 | {
277 | len += p.P4.GetLength()
278 | // buff = make([]byte, len)
279 | buff = p.Buff[0:len]
280 |
281 | p.Bufflen = len
282 |
283 | p.P4.serialization(buff[HeaderLength:])
284 |
285 | }
286 |
287 | case FaHongbaoRequest_ID:
288 | {
289 |
290 | len += p.P5.GetLength()
291 | // buff = make([]byte, len)
292 | buff = p.Buff[0:len]
293 |
294 | p.Bufflen = len
295 | p.P5.serialization(buff[HeaderLength:])
296 |
297 | }
298 |
299 | case GetHongbaoRequest_ID:
300 | {
301 | len += p.P6.GetLength()
302 |
303 | //buff = make([]byte, len)
304 | buff = p.Buff[0:len]
305 |
306 | p.Bufflen = len
307 | p.P6.serialization(buff[HeaderLength:])
308 |
309 | }
310 |
311 | case GetHongbaoRespone_ID:
312 | {
313 | len += p.P7.GetLength()
314 | //buff = make([]byte, len)
315 | buff = p.Buff[0:len]
316 |
317 | p.Bufflen = len
318 | p.P7.serialization(buff[HeaderLength:])
319 |
320 | }
321 |
322 | }
323 |
324 | binary.BigEndian.PutUint16(buff[2:], uint16(p.Cmd))
325 | binary.BigEndian.PutUint16(buff, uint16(len))
326 | binary.BigEndian.PutUint64(buff[4:], uint64(time.Now().Unix()))
327 |
328 | return buff
329 |
330 | }
331 |
332 | func (p *Message) Unserialization(buff []byte) bool {
333 | p.Cmd = int(binary.BigEndian.Uint16(buff[2:]))
334 | p.TimeStamp = uint64(binary.BigEndian.Uint64(buff[4:]))
335 | switch p.Cmd {
336 |
337 | case LoginRequest_ID:
338 | {
339 | if p.P1 == nil {
340 | p.P1 = &LoginRequest{}
341 | }
342 | p.P1.unserialization(buff[HeaderLength:])
343 |
344 | }
345 |
346 | case LoginResponse_ID:
347 | {
348 | if p.P2 == nil {
349 | p.P2 = &LoginResponse{}
350 | }
351 | p.P2.unserialization(buff[HeaderLength:])
352 |
353 | }
354 |
355 | case YaoHongbaoRequest_ID:
356 | {
357 | if p.P3 == nil {
358 | p.P3 = &YaoHongbaoRequest{}
359 | }
360 | p.P3.unserialization(buff[HeaderLength:])
361 | }
362 |
363 | case YaoHongbaoResponse_ID:
364 | {
365 | if p.P4 == nil {
366 | p.P4 = &YaoHongbaoResponse{}
367 | }
368 | p.P4.unserialization(buff[HeaderLength:])
369 | }
370 |
371 | case FaHongbaoRequest_ID:
372 | {
373 | if p.P5 == nil {
374 | p.P5 = &FaHongbaoRequest{}
375 | }
376 | p.P5.unserialization(buff[HeaderLength:])
377 | }
378 | case GetHongbaoRequest_ID:
379 | {
380 | if p.P6 == nil {
381 | p.P6 = &GetHongbaoRequest{}
382 | }
383 | p.P6.unserialization(buff[HeaderLength:])
384 | }
385 | case GetHongbaoRespone_ID:
386 | {
387 | if p.P7 == nil {
388 | p.P7 = &GetHongbaoRespone{}
389 | }
390 | p.P7.unserialization(buff[HeaderLength:])
391 | }
392 | default:
393 | return false
394 |
395 | }
396 | return true
397 | }
398 |
399 | func (p *Message) Rollback() {
400 | p.Pool.Push(p)
401 | }
402 |
403 | type MessagePool struct {
404 | l sync.Mutex
405 | maxuser int
406 | user int
407 | queue chan *Message
408 | }
409 |
410 | func (p *MessagePool) Init(len int, maxusernum int) {
411 | p.maxuser = maxusernum
412 | p.queue = make(chan *Message, len)
413 |
414 | }
415 |
416 | func (p *MessagePool) GetPool() *MessagePool {
417 | p.l.Lock()
418 | if p.user+1 < p.maxuser {
419 | p.user += 1
420 | p.l.Unlock()
421 | return p
422 | } else {
423 | p.l.Unlock()
424 | return nil
425 | }
426 |
427 | }
428 |
429 | func (p *MessagePool) Reduce() {
430 | p.l.Lock()
431 | p.user -= 1
432 | p.l.Unlock()
433 |
434 | }
435 |
436 | func (p *MessagePool) Pop() *Message {
437 |
438 | p2 := <-p.queue
439 | return p2
440 | }
441 |
442 | func (p *MessagePool) Push(pmessage *Message) {
443 |
444 | p.queue <- pmessage
445 | }
446 |
--------------------------------------------------------------------------------
/lib/message/message_test.go:
--------------------------------------------------------------------------------
1 | package message
2 |
3 | import (
4 | "fmt"
5 | "testing"
6 | "time"
7 | )
8 |
9 | // 使用protobuff 应该会更快 更好
10 | // 但是fakechat 项目中,性能并不能让我满意,所以我简单的实现一下。
11 | // 如果修改也非常简单
12 |
13 | func testEq(a, b []byte) bool {
14 |
15 | if a == nil && b == nil {
16 | return true
17 | }
18 |
19 | if a == nil || b == nil {
20 | return false
21 | }
22 |
23 | if len(a) != len(b) {
24 | return false
25 | }
26 |
27 | for i := range a {
28 | if a[i] != b[i] {
29 | return false
30 | }
31 | }
32 |
33 | return true
34 | }
35 |
36 | func CheckEq(a, b []byte, t *testing.T) {
37 | if !testEq(a, b) {
38 | t.Error(a)
39 | t.Error(b)
40 | t.Error("Wrong")
41 | }
42 | }
43 |
44 | func Test_monitor(t *testing.T) {
45 |
46 | t1 := time.Now()
47 | var i uint32
48 | for i = 0; i < 20*10*1000; i++ {
49 |
50 | {
51 | a := Message{}
52 | a.Init()
53 | a.Userid = uint(i)
54 | a.Cmd = LoginRequest_ID
55 | p := LoginRequest{}
56 | p.Userid = i
57 | a.P1 = &p
58 | a_serialization := a.Serialization()
59 | if len(a_serialization) != (HeaderLength + a.P1.GetLength()) {
60 | t.Error("wrong ")
61 | }
62 | a1 := Message{}
63 |
64 | a1.Init()
65 | a1.Unserialization(a_serialization)
66 | if (a1.P1.Userid != a.P1.Userid) || (a.Cmd != a1.Cmd) {
67 | t.Error("wrong ")
68 | }
69 | }
70 |
71 | {
72 | a := Message{}
73 |
74 | a.Init()
75 | a.Userid = uint(i)
76 | a.Cmd = LoginResponse_ID
77 | p := LoginResponse{}
78 | p.Userid = i
79 | p.Result = i + 1
80 | a.P2 = &p
81 | a_serialization := a.Serialization()
82 | if len(a_serialization) != (HeaderLength + a.P2.GetLength()) {
83 | t.Error("wrong ")
84 | }
85 |
86 | a1 := Message{}
87 |
88 | a1.Init()
89 | a1.Unserialization(a_serialization)
90 | if (a1.Cmd != a.Cmd) || (a1.P2.Userid != a.P2.Userid) || (a1.P2.Result != a.P2.Result) {
91 | t.Error("wrong ", a1.Cmd, a.Cmd, a1.P2.Userid, a.P2.Userid, a1.P2.Result, a.P2.Result)
92 |
93 | }
94 |
95 | }
96 |
97 | {
98 | a := Message{}
99 |
100 | a.Init()
101 | a.Userid = uint(i)
102 | a.Cmd = YaoHongbaoRequest_ID
103 | p := YaoHongbaoRequest{}
104 | p.Userid = i
105 | a.P3 = &p
106 | a_serialization := a.Serialization()
107 | if len(a_serialization) != (HeaderLength + a.P3.GetLength()) {
108 | t.Error("wrong ")
109 | }
110 |
111 | a1 := Message{}
112 |
113 | a1.Init()
114 | a1.Unserialization(a_serialization)
115 | if (a1.Cmd != a.Cmd) || (a1.P3.Userid != a.P3.Userid) {
116 | t.Error("wrong ")
117 | }
118 |
119 | }
120 |
121 | {
122 | a := Message{}
123 |
124 | a.Init()
125 |
126 | a.Userid = uint(i)
127 | a.Cmd = YaoHongbaoResponse_ID
128 | p := YaoHongbaoResponse{}
129 | p.Userid = i
130 | p.Result = i + 1
131 | p.Hongbaoid = i + 3
132 | p.Amount = i + 1000
133 | a.P4 = &p
134 | a_serialization := a.Serialization()
135 | if len(a_serialization) != (HeaderLength + a.P4.GetLength()) {
136 | t.Error("wrong ")
137 | }
138 |
139 | a1 := Message{}
140 |
141 | a1.Init()
142 | a1.Unserialization(a_serialization)
143 | if (a1.Cmd != a.Cmd) || (a1.P4.Userid != a.P4.Userid) || (a1.P4.Result != a.P4.Result) || (a1.P4.Hongbaoid != a.P4.Hongbaoid) {
144 | t.Error("wrong ")
145 | }
146 |
147 | }
148 |
149 | {
150 | a := Message{}
151 |
152 | a.Init()
153 | a.Userid = uint(i)
154 | a.Cmd = FaHongbaoRequest_ID
155 | p := FaHongbaoRequest{}
156 | p.Hongbaoid = i
157 | p.Amount = i + 1
158 | p.Useramount = i + 2
159 | p.Userid1 = i + 3
160 | p.Userid2 = i + 4
161 | p.Userid3 = i + 5
162 |
163 | a.P5 = &p
164 |
165 | a_serialization := a.Serialization()
166 | if len(a_serialization) != (HeaderLength + a.P5.GetLength()) {
167 | t.Error("wrong ")
168 | }
169 |
170 | a1 := Message{}
171 |
172 | a1.Init()
173 | a1.Unserialization(a_serialization)
174 | if (a1.Cmd != a.Cmd) || (a1.P5.Hongbaoid != a.P5.Hongbaoid) || (a1.P5.Amount != a.P5.Amount) || (a1.P5.Useramount != a.P5.Useramount) || (a1.P5.Userid1 != a.P5.Userid1) || (a1.P5.Userid2 != a.P5.Userid2) || (a1.P5.Userid3 != a.P5.Userid3) {
175 | t.Error("wrong ")
176 | }
177 |
178 | }
179 |
180 | {
181 | a := Message{}
182 |
183 | a.Init()
184 | a.Userid = uint(i)
185 | a.Cmd = GetHongbaoRequest_ID
186 | p := GetHongbaoRequest{}
187 | p.Userid = i
188 | p.Hongbaoid = i + 3
189 | a.P6 = &p
190 | a_serialization := a.Serialization()
191 | if len(a_serialization) != (HeaderLength + a.P6.GetLength()) {
192 | t.Error("wrong ")
193 | }
194 |
195 | a1 := Message{}
196 |
197 | a1.Init()
198 | a1.Unserialization(a_serialization)
199 | if (a1.Cmd != a.Cmd) || (a1.P6.Userid != a.P6.Userid) || (a1.P6.Hongbaoid != a.P6.Hongbaoid) {
200 | t.Error("wrong ")
201 | }
202 |
203 | }
204 |
205 | {
206 | a := Message{}
207 |
208 | a.Init()
209 | a.Userid = uint(i)
210 | a.Cmd = GetHongbaoRespone_ID
211 | p := GetHongbaoRespone{}
212 | p.Amount = i + 1
213 | p.Hongbaoid = i + 3
214 | a.P7 = &p
215 | a_serialization := a.Serialization()
216 | if len(a_serialization) != (HeaderLength + a.P7.GetLength()) {
217 | t.Error("wrong ")
218 | }
219 |
220 | a1 := Message{}
221 |
222 | a1.Init()
223 | a1.Unserialization(a_serialization)
224 | if (a1.Cmd != a.Cmd) || (a1.P7.Hongbaoid != a.P7.Hongbaoid) || (a1.P7.Amount != a.P7.Amount) {
225 | t.Error("wrong ")
226 | }
227 |
228 | }
229 |
230 | }
231 | t2 := time.Now()
232 | fmt.Println(t2.Sub(t1))
233 |
234 | }
235 |
--------------------------------------------------------------------------------
/lib/message2/message2.pb.go:
--------------------------------------------------------------------------------
1 | // Code generated by protoc-gen-go.
2 | // source: message2.proto
3 | // DO NOT EDIT!
4 |
5 | /*
6 | Package message2 is a generated protocol buffer package.
7 |
8 | It is generated from these files:
9 | message2.proto
10 |
11 | It has these top-level messages:
12 | MsgItem
13 | */
14 | package message2
15 |
16 | import proto "github.com/golang/protobuf/proto"
17 | import math "math"
18 |
19 | // Reference imports to suppress errors if they are not otherwise used.
20 | var _ = proto.Marshal
21 | var _ = math.Inf
22 |
23 | type MsgItem struct {
24 | Id *int32 `protobuf:"varint,1,req,name=id" json:"id,omitempty"`
25 | R *int32 `protobuf:"varint,2,req,name=r" json:"r,omitempty"`
26 | XXX_unrecognized []byte `json:"-"`
27 | }
28 |
29 | func (m *MsgItem) Reset() { *m = MsgItem{} }
30 | func (m *MsgItem) String() string { return proto.CompactTextString(m) }
31 | func (*MsgItem) ProtoMessage() {}
32 |
33 | func (m *MsgItem) GetId() int32 {
34 | if m != nil && m.Id != nil {
35 | return *m.Id
36 | }
37 | return 0
38 | }
39 |
40 | func (m *MsgItem) GetR() int32 {
41 | if m != nil && m.R != nil {
42 | return *m.R
43 | }
44 | return 0
45 | }
46 |
47 | func init() {
48 | }
49 |
--------------------------------------------------------------------------------
/lib/message2/message2.proto:
--------------------------------------------------------------------------------
1 | syntax = "proto2";
2 |
3 |
4 | message MsgItem {
5 |
6 | required int32 id = 1;
7 | required int32 r = 2;
8 |
9 | }
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/lib/message2/message2_test.go:
--------------------------------------------------------------------------------
1 | package message2
2 |
3 | import (
4 | "fmt"
5 | "github.com/golang/protobuf/proto"
6 | "testing"
7 | "time"
8 | )
9 |
10 | // 使用protobuff 应该会更快 更好
11 | // 但是fakechat 项目中,性能并不能让我满意,所以我简单的实现一下。
12 | // 如果修改也非常简单
13 |
14 | func testEq(a, b []byte) bool {
15 |
16 | if a == nil && b == nil {
17 | return true
18 | }
19 |
20 | if a == nil || b == nil {
21 | return false
22 | }
23 |
24 | if len(a) != len(b) {
25 | return false
26 | }
27 |
28 | for i := range a {
29 | if a[i] != b[i] {
30 | return false
31 | }
32 | }
33 |
34 | return true
35 | }
36 |
37 | func CheckEq(a, b []byte, t *testing.T) {
38 | if !testEq(a, b) {
39 | t.Error(a)
40 | t.Error(b)
41 | t.Error("Wrong")
42 | }
43 | }
44 |
45 | func Test_monitor(t *testing.T) {
46 |
47 | t1 := time.Now()
48 | var i uint32
49 | for i = 0; i < 20*10*1000; i++ {
50 | {
51 | a := &MsgItem{}
52 | id := int32(i)
53 | a.Id = &id
54 | R := int32(i + 1)
55 | a.R = &R
56 |
57 | a_serialization, err := proto.Marshal(a)
58 | if err != nil {
59 |
60 | t.Error("wrong ")
61 | }
62 | b := &MsgItem{}
63 | err = proto.Unmarshal(a_serialization, b)
64 | if err != nil {
65 | t.Error("wrong ")
66 | }
67 | if (*b.Id != *a.Id) || (*b.R != *a.R) {
68 | t.Error("wrong ")
69 | }
70 | }
71 |
72 | }
73 | t2 := time.Now()
74 |
75 | fmt.Println((t2.Nanosecond() - t1.Nanosecond()) / 1e6)
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/lib/monitor/monitor.go:
--------------------------------------------------------------------------------
1 | package monitor
2 |
3 | import (
4 | "encoding/binary"
5 | "fmt"
6 | . "github.com/10billionhongbaos/lib/utils"
7 | "net"
8 | "strconv"
9 | "sync/atomic"
10 | "time"
11 | )
12 |
13 | const (
14 | Request = 10 // 发送请求总数目
15 | Response = 11 // 收到响应数目
16 | GetHongbao = 14 // 获得红包的数目
17 | NoHongbao = 15 // 没有获得红包
18 | AllHongbao = 16 // 已经获取了所有红包
19 | OverLoad = 17 // 过载保护
20 |
21 | SendHongbao = 18
22 |
23 | Connection = 20 // 累计连接
24 | Discon = 21 // 累计断开
25 |
26 | CountYaoHongbaoReq = 22 //累计发放摇红包数目
27 | CountGetYahoHongbaoReq = 23 //累计收到摇红包数目
28 |
29 | SumYaoHongbao = 24 //累计发放摇红包金额数目
30 | SumGetYaohoHongbao = 25 //累计收到摇红包金额数目
31 | CountFaHongbaoReq = 30 // 累计普通红包数目
32 | CountGetFaHongbaoReq = 31 // 累计获取普通红包数目
33 | SumFaHongbao = 32 // 累计普通红包金额总数
34 | SumGetFaHongbao = 33 // 累计获取普通红包金额总数
35 | TimeRange = 6000 //只记录600秒内的结果
36 | ArrayLength = 60 // 最多纪录项目
37 | )
38 |
39 | const (
40 | UDP_PORT = 8002
41 | )
42 |
43 | type Monitor struct {
44 | data [ArrayLength][TimeRange]int64
45 | PersistenceData [ArrayLength]int64
46 | }
47 |
48 | var GMonitor Monitor
49 |
50 | func (m *Monitor) Add(index int, i int64, timeStamp int64) int64 {
51 | v := timeStamp % TimeRange
52 | return atomic.AddInt64(&m.data[index][v], i)
53 | }
54 |
55 | func (m *Monitor) Add2(index int, i int64) int64 {
56 | return atomic.AddInt64(&m.PersistenceData[index], i)
57 | }
58 |
59 | func (m *Monitor) Get(timeStamp int64) ([ArrayLength]int64, [ArrayLength]int64) {
60 | data := [ArrayLength]int64{}
61 | pdata := [ArrayLength]int64{}
62 | v := timeStamp % TimeRange
63 | for i := 0; i < ArrayLength; i++ {
64 | data[i] = atomic.LoadInt64(&m.data[i][v])
65 | pdata[i] = atomic.LoadInt64(&m.PersistenceData[i])
66 |
67 | }
68 | return data, pdata
69 | }
70 |
71 | func (m *Monitor) Clean(timeStamp int64) {
72 | v := timeStamp % TimeRange
73 | for i := 0; i < ArrayLength; i++ {
74 | atomic.StoreInt64(&m.data[i][v], 0)
75 | }
76 | }
77 | func itoa(x int64) string {
78 |
79 | return strconv.Itoa(int(x))
80 |
81 | }
82 |
83 | func PrintData(timeStamp int64, types int, v [ArrayLength]int64, p [ArrayLength]int64) string {
84 | s := ""
85 |
86 | s += itoa(timeStamp)
87 | s += " " + itoa(int64(types))
88 | s += " Request: " + itoa(v[Request])
89 | s += " Response: " + itoa(v[Response])
90 | s += " GetHongbao: " + itoa(v[GetHongbao])
91 | s += " NoHongbao: " + itoa(v[NoHongbao])
92 | s += " AllHongbao: " + itoa(v[AllHongbao])
93 | s += " OverLoad: " + itoa(v[OverLoad])
94 | s += " yaohongbao: "
95 | s += itoa(p[CountYaoHongbaoReq]) + "/" + itoa(p[CountGetYahoHongbaoReq])
96 | s += " " + itoa(p[SumYaoHongbao]) + " " + itoa(p[SumGetYaohoHongbao])
97 | s += " fahongbao:"
98 | s += itoa(p[CountFaHongbaoReq]) + "/" + itoa(p[CountGetFaHongbaoReq])
99 | s += " " + itoa(p[SumFaHongbao]) + " " + itoa(p[SumGetFaHongbao])
100 | s += " detail "
101 | for i := 0; i <= 6; i++ {
102 | s += itoa(v[i]) + " "
103 | }
104 | return s
105 | }
106 |
107 | func (m *Monitor) ShowStatus(timeStamp int64, types int) ([ArrayLength]int64, [ArrayLength]int64) {
108 | v, p := m.Get(timeStamp)
109 | s := PrintData(timeStamp, types, v, p)
110 | fmt.Println(s)
111 | return v, p
112 | }
113 |
114 | func Show(host string, types int) {
115 | conn, err := net.Dial("tcp", host)
116 | if err != nil {
117 | panic(err)
118 | }
119 | defer conn.Close()
120 |
121 | buff := make([]byte, ArrayLength*8*2+8+8)
122 | tlast := time.Now().Unix() - 4
123 | for {
124 | tnow := time.Now()
125 | t := tnow.Unix() - 3
126 |
127 | // fmt.Println("tlast", tlast, "t", t)
128 | for ; tlast < t; tlast += 1 {
129 | // fmt.Println("for tlast", tlast, "t", t)
130 | v, p := GMonitor.ShowStatus(tlast, types)
131 | var tmp uint64
132 | tmp = uint64(tlast)
133 | binary.BigEndian.PutUint64(buff, tmp)
134 | tmp = uint64(types)
135 | binary.BigEndian.PutUint64(buff[8:], tmp)
136 | index := 16
137 | for i := 0; i < ArrayLength; i++ {
138 | tmp = uint64(v[i])
139 | binary.BigEndian.PutUint64(buff[index:], tmp)
140 | index += 8
141 | }
142 | for i := 0; i < ArrayLength; i++ {
143 | tmp = uint64(p[i])
144 | binary.BigEndian.PutUint64(buff[index:], tmp)
145 | index += 8
146 | }
147 |
148 | _, err = conn.Write(buff)
149 | if err != nil {
150 | fmt.Println("发送数据失败!", err)
151 | //return
152 | }
153 | GMonitor.Clean(tlast)
154 | }
155 | CorrectSleepOneSecond2(tnow)
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/lib/monitor/monitor_test.go:
--------------------------------------------------------------------------------
1 | package monitor
2 |
3 | import (
4 | "fmt"
5 | "testing"
6 | "time"
7 | )
8 |
9 | func Test_monitor(t *testing.T) {
10 | a := &Monitor{}
11 | timestamp := time.Now().Unix()
12 | var i int64
13 | for i = 0; i < TimeRange; i++ {
14 | a.Add(Request, 1+i, timestamp+i)
15 | a.Add(Response, 2+i, timestamp+i)
16 | a.Add(GetHongbao, 3+i, timestamp+i)
17 | a.Add(NoHongbao, 4+i, timestamp+i)
18 | a.Add(AllHongbao, 5+i, timestamp+i)
19 | }
20 |
21 | for i = 0; i < TimeRange; i++ {
22 | a.ShowStatus(timestamp+i, 0)
23 |
24 | v1, _ := a.Get(timestamp + i)
25 |
26 | fmt.Println(v1)
27 | a.Clean(timestamp + i)
28 | v1, _ = a.Get(timestamp + i)
29 | fmt.Println(v1)
30 |
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/lib/parse/parse.go:
--------------------------------------------------------------------------------
1 | package parse
2 |
3 | import (
4 | "encoding/binary"
5 | . "github.com/10billionhongbaos/lib/message"
6 | "net"
7 | )
8 |
9 | func ReadByte(conn net.Conn, buff []byte, length int) bool {
10 | readed := 0
11 |
12 | for {
13 | n, err := conn.Read(buff[readed:length])
14 | if err != nil {
15 | return false
16 | }
17 | readed += n
18 | if readed == length {
19 | return true
20 | }
21 | }
22 | }
23 | func ReadMessage(conn *net.Conn, ppool *MessagePool) (p *Message, result bool) {
24 |
25 | p = ppool.Pop()
26 | buff := p.Buff
27 | result = ReadByte(*conn, buff, 4)
28 | if result != true {
29 | return nil, result
30 | }
31 |
32 | length := binary.BigEndian.Uint16(buff[0:])
33 | length -= 4
34 | if (length > 40) || (length < 4) {
35 | return nil, result
36 | }
37 | result = ReadByte(*conn, buff[4:], int(length))
38 | if p.Unserialization(buff) {
39 | return p, result
40 | }
41 |
42 | return nil, result
43 | }
44 |
45 | func WriteMessage(conn *net.Conn, p *Message) error {
46 | b := p.Serialization()
47 | _, err := (*conn).Write(b[0:p.Bufflen])
48 | p.Rollback()
49 | return err
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/lib/qps/qps.go:
--------------------------------------------------------------------------------
1 | package qps
2 |
3 | func Hash(userid uint32, totaluser uint32) uint32 {
4 | var bucket uint32
5 | bucket = 13
6 | if totaluser <= bucket {
7 | return userid
8 | }
9 | length := totaluser / bucket
10 | remainder := totaluser % bucket
11 | u_length := userid / bucket
12 | u_remainder := userid % bucket
13 | newid := length * u_remainder
14 | if u_remainder > remainder {
15 | newid += remainder
16 | } else {
17 | newid += u_remainder
18 | }
19 | newid += u_length
20 |
21 | return newid
22 | }
23 |
24 | func TriggerRequest2(userid uint32, totaluser uint32, qps uint32, timeStamp int64) bool {
25 |
26 | return TriggerRequest(Hash(userid, totaluser), totaluser, qps, timeStamp)
27 | }
28 |
29 | func TriggerRequest(userid uint32, totaluser uint32, qps uint32, timeStamp int64) bool {
30 | second := uint32(timeStamp)
31 | if qps > totaluser {
32 | qps = totaluser
33 | }
34 | if totaluser%qps == 0 {
35 | id := totaluser / qps
36 | if second%id == userid%id {
37 | return true
38 | } else {
39 | return false
40 | }
41 | }
42 |
43 | index := second * qps % totaluser
44 | if index+qps > totaluser {
45 | if (userid >= index) && userid <= (totaluser-1) {
46 | return true
47 | }
48 | if userid >= 0 && userid <= (qps-(totaluser-index)-1) {
49 | return true
50 | }
51 | return false
52 | } else {
53 | if userid >= index && userid <= (index+qps-1) {
54 | return true
55 | }
56 | return false
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/lib/qps/qps_test.go:
--------------------------------------------------------------------------------
1 | package qps
2 |
3 | import (
4 | "testing"
5 | )
6 |
7 | func Test_hash(t *testing.T) {
8 | var totaluser uint32
9 | var i uint32
10 |
11 | for totaluser = 0; totaluser <= 30000; totaluser++ {
12 | v := make([]int, int(totaluser))
13 | for i = 0; i < totaluser; i++ {
14 | index := Hash(i, totaluser)
15 | v[index] += 1
16 | }
17 | for i = 0; i < totaluser; i++ {
18 | if v[i] != 1 {
19 | t.Error(i, "wrong")
20 | }
21 | }
22 | }
23 | }
24 |
25 | func Test_qps2(t *testing.T) {
26 | // 1484553130
27 | // -min 60000 -max 119999 -total 180000 -qps 3000
28 | sum := 0
29 | sum2 := 0
30 | i := 0
31 | for i = 0; i < 180000; i++ {
32 | b := TriggerRequest(uint32(i), 180000, 3000, 1484553130)
33 | if b == true {
34 | sum += 1
35 | }
36 |
37 | }
38 |
39 | for i = 0; i < 180000; i++ {
40 | b := TriggerRequest2(uint32(i), 180000, 3000, 1484553130)
41 | if b == true {
42 | sum2 += 1
43 | }
44 |
45 | }
46 |
47 | if sum != sum2 {
48 | t.Error(sum, sum2)
49 | }
50 | }
51 |
52 | func Test_qps(t *testing.T) {
53 | if false == TriggerRequest(0, 100, 10, 0) {
54 | t.Error("wrong ")
55 | }
56 |
57 | if false == TriggerRequest(10, 100, 10, 0) {
58 | t.Error("wrong ")
59 | }
60 |
61 | if true == TriggerRequest(11, 100, 10, 0) {
62 | t.Error("wrong ")
63 | }
64 | if false == TriggerRequest(11, 100, 10, 1) {
65 | t.Error("wrong ")
66 | }
67 |
68 | if false == TriggerRequest(99, 100, 10, 9) {
69 | t.Error("wrong ")
70 | }
71 |
72 | if false == TriggerRequest(0, 100, 10, 10) {
73 | t.Error("wrong ")
74 | }
75 |
76 | /*
77 | /0-32
78 | /33-65
79 | /66-98
80 | /99-31
81 | /32-64
82 | */
83 |
84 | // 0 /0-32
85 |
86 | if false == TriggerRequest(0, 100, 33, 0) {
87 | t.Error("wrong ")
88 | }
89 | if false == TriggerRequest(31, 100, 33, 0) {
90 | t.Error("wrong ")
91 | }
92 |
93 | if false == TriggerRequest(32, 100, 33, 0) {
94 | t.Error("wrong ")
95 | }
96 |
97 | if true == TriggerRequest(33, 100, 33, 0) {
98 | t.Error("wrong ")
99 | }
100 | if true == TriggerRequest(64, 100, 33, 0) {
101 | t.Error("wrong ")
102 | }
103 | if true == TriggerRequest(65, 100, 33, 0) {
104 | t.Error("wrong ")
105 | }
106 |
107 | if true == TriggerRequest(66, 100, 33, 0) {
108 | t.Error("wrong ")
109 | }
110 |
111 | if true == TriggerRequest(98, 100, 33, 0) {
112 | t.Error("wrong ")
113 | }
114 |
115 | if true == TriggerRequest(99, 100, 33, 0) {
116 | t.Error("wrong ")
117 | }
118 |
119 | //1 /33-65
120 |
121 | if true == TriggerRequest(0, 100, 33, 1) {
122 | t.Error("wrong ")
123 | }
124 | if true == TriggerRequest(31, 100, 33, 1) {
125 | t.Error("wrong ")
126 | }
127 |
128 | if true == TriggerRequest(32, 100, 33, 1) {
129 | t.Error("wrong ")
130 | }
131 |
132 | if false == TriggerRequest(33, 100, 33, 1) {
133 | t.Error("wrong ")
134 | }
135 | if false == TriggerRequest(64, 100, 33, 1) {
136 | t.Error("wrong ")
137 | }
138 | if false == TriggerRequest(65, 100, 33, 1) {
139 | t.Error("wrong ")
140 | }
141 |
142 | if true == TriggerRequest(66, 100, 33, 1) {
143 | t.Error("wrong ")
144 | }
145 |
146 | if true == TriggerRequest(98, 100, 33, 1) {
147 | t.Error("wrong ")
148 | }
149 |
150 | if true == TriggerRequest(99, 100, 33, 1) {
151 | t.Error("wrong ")
152 | }
153 |
154 | //2 /66-98
155 |
156 | if true == TriggerRequest(0, 100, 33, 2) {
157 | t.Error("wrong ")
158 | }
159 | if true == TriggerRequest(31, 100, 33, 2) {
160 | t.Error("wrong ")
161 | }
162 |
163 | if true == TriggerRequest(32, 100, 33, 2) {
164 | t.Error("wrong ")
165 | }
166 |
167 | if true == TriggerRequest(33, 100, 33, 2) {
168 | t.Error("wrong ")
169 | }
170 | if true == TriggerRequest(64, 100, 33, 2) {
171 | t.Error("wrong ")
172 | }
173 | if true == TriggerRequest(65, 100, 33, 2) {
174 | t.Error("wrong ")
175 | }
176 |
177 | if false == TriggerRequest(66, 100, 33, 2) {
178 | t.Error("wrong ")
179 | }
180 |
181 | if false == TriggerRequest(98, 100, 33, 2) {
182 | t.Error("wrong ")
183 | }
184 |
185 | if true == TriggerRequest(99, 100, 33, 2) {
186 | t.Error("wrong ")
187 | }
188 |
189 | // 3 /99-31
190 |
191 | if false == TriggerRequest(0, 100, 33, 3) {
192 | t.Error("wrong ")
193 | }
194 | if false == TriggerRequest(31, 100, 33, 3) {
195 | t.Error("wrong ")
196 | }
197 |
198 | if true == TriggerRequest(32, 100, 33, 3) {
199 | t.Error("wrong ")
200 | }
201 |
202 | if true == TriggerRequest(33, 100, 33, 3) {
203 | t.Error("wrong ")
204 | }
205 | if true == TriggerRequest(64, 100, 33, 3) {
206 | t.Error("wrong ")
207 | }
208 | if true == TriggerRequest(65, 100, 33, 3) {
209 | t.Error("wrong ")
210 | }
211 |
212 | if true == TriggerRequest(66, 100, 33, 3) {
213 | t.Error("wrong ")
214 | }
215 |
216 | if true == TriggerRequest(98, 100, 33, 3) {
217 | t.Error("wrong ")
218 | }
219 |
220 | if false == TriggerRequest(99, 100, 33, 3) {
221 | t.Error("wrong ")
222 | }
223 |
224 | //4 /32-64
225 | if true == TriggerRequest(0, 100, 33, 4) {
226 | t.Error("wrong ")
227 | }
228 | if true == TriggerRequest(31, 100, 33, 4) {
229 | t.Error("wrong ")
230 | }
231 |
232 | if false == TriggerRequest(32, 100, 33, 4) {
233 | t.Error("wrong ")
234 | }
235 |
236 | if false == TriggerRequest(33, 100, 33, 4) {
237 | t.Error("wrong ")
238 | }
239 | if false == TriggerRequest(64, 100, 33, 4) {
240 | t.Error("wrong ")
241 | }
242 | if true == TriggerRequest(65, 100, 33, 4) {
243 | t.Error("wrong ")
244 | }
245 |
246 | if true == TriggerRequest(66, 100, 33, 4) {
247 | t.Error("wrong ")
248 | }
249 |
250 | if true == TriggerRequest(98, 100, 33, 4) {
251 | t.Error("wrong ")
252 | }
253 |
254 | if true == TriggerRequest(99, 100, 33, 4) {
255 | t.Error("wrong ")
256 | }
257 | }
258 |
--------------------------------------------------------------------------------
/lib/utils/utils.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "fmt"
5 | "os"
6 | "runtime"
7 | "time"
8 | )
9 |
10 | func CheckErr(err error) {
11 | if err != nil {
12 | fmt.Println("Fatal error: ", err.Error())
13 | os.Exit(-1)
14 | }
15 | }
16 |
17 | func GC(sleep int) {
18 | var m runtime.MemStats
19 |
20 | for {
21 | time.Sleep(time.Duration(sleep) * time.Second)
22 | fmt.Println("gc")
23 | // runtime.GC()
24 | // var m runtime.MemStats
25 | runtime.ReadMemStats(&m)
26 | fmt.Printf("%d,%d,%d,%d\n", m.HeapSys, m.HeapAlloc,
27 | m.HeapIdle, m.HeapReleased)
28 |
29 | }
30 |
31 | }
32 |
33 | func CorrectSleepOneSecond() {
34 | t := time.Now()
35 | second := t.UnixNano()
36 | sleep := 1e9 - (second % 1e9)
37 | time.Sleep(time.Duration(sleep) * time.Nanosecond)
38 | }
39 |
40 | func CorrectSleepOneSecond2(t1 time.Time) {
41 | tnow2 := time.Now().UnixNano()
42 |
43 | if tnow2-t1.UnixNano() > 1e9 {
44 | return
45 | }
46 |
47 | time.Sleep(time.Duration(1e9-(tnow2%1e9)) * time.Nanosecond)
48 | }
49 |
50 | func GetMailBoxAddress(mailboxnum int, userid int) (x int, y int) {
51 | x = userid % mailboxnum
52 |
53 | y = userid / mailboxnum
54 | return x, y
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/linux/create_run.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | import sys
4 | i=0
5 | Max= 60*10*1000
6 | Range = 6*10*1000
7 | Qps = 30
8 |
9 | index = -1
10 | if len(sys.argv) != 1:
11 | index = int(sys.argv[1])
12 |
13 | printindex = 0
14 | while True:
15 | if i >= Max-1:
16 | break
17 | if i+Range>= Max-1:
18 |
19 | if index == -1 or index == printindex:
20 | print "./client -min", i, "-max", Max-1, "-total",Max, "-qps",Qps
21 | i = Max -1
22 | printindex += 1
23 | else:
24 | if index == -1 or index == printindex:
25 | print "./client -min", i, "-max", i+Range -1 , "-total",Max, "-qps", Qps
26 | i = i + Range
27 | printindex += 1
28 |
29 |
--------------------------------------------------------------------------------
/linux/limits.conf:
--------------------------------------------------------------------------------
1 | # /etc/security/limits.conf
2 | #
3 | #Each line describes a limit for a user in the form:
4 | #
5 | # -
6 | #
7 | #Where:
8 | # can be:
9 | # - an user name
10 | # - a group name, with @group syntax
11 | # - the wildcard *, for default entry
12 | # - the wildcard %, can be also used with %group syntax,
13 | # for maxlogin limit
14 | # - NOTE: group and wildcard limits are not applied to root.
15 | # To apply a limit to the root user, must be
16 | # the literal username root.
17 | #
18 | # can have the two values:
19 | # - "soft" for enforcing the soft limits
20 | # - "hard" for enforcing hard limits
21 | #
22 | #
- can be one of the following:
23 | # - core - limits the core file size (KB)
24 | # - data - max data size (KB)
25 | # - fsize - maximum filesize (KB)
26 | # - memlock - max locked-in-memory address space (KB)
27 | # - nofile - max number of open files
28 | # - rss - max resident set size (KB)
29 | # - stack - max stack size (KB)
30 | # - cpu - max CPU time (MIN)
31 | # - nproc - max number of processes
32 | # - as - address space limit (KB)
33 | # - maxlogins - max number of logins for this user
34 | # - maxsyslogins - max number of logins on the system
35 | # - priority - the priority to run user process with
36 | # - locks - max number of file locks the user can hold
37 | # - sigpending - max number of pending signals
38 | # - msgqueue - max memory used by POSIX message queues (bytes)
39 | # - nice - max nice priority allowed to raise to values: [-20, 19]
40 | # - rtprio - max realtime priority
41 | # - chroot - change root to directory (Debian-specific)
42 | #
43 | #
-
44 | #
45 |
46 | #* soft core 0
47 | #root hard core 100000
48 | #* hard rss 10000
49 | #@student hard nproc 20
50 | #@faculty soft nproc 20
51 | #@faculty hard nproc 50
52 | #ftp hard nproc 0
53 | #ftp - chroot /ftp
54 | #@student - maxlogins 4
55 |
56 | # End of file
57 |
58 |
59 | root hard nofile 102550
60 | root soft nofile 102550
61 |
--------------------------------------------------------------------------------
/linux/sysctl.conf:
--------------------------------------------------------------------------------
1 | #
2 | # /etc/sysctl.conf - Configuration file for setting system variables
3 | # See /etc/sysctl.d/ for additional system variables
4 | # See sysctl.conf (5) for information.
5 | #
6 |
7 | #kernel.domainname = example.com
8 |
9 | # Uncomment the following to stop low-level messages on console
10 | #kernel.printk = 3 4 1 3
11 |
12 | ##############################################################3
13 | # Functions previously found in netbase
14 | #
15 |
16 | # Uncomment the next two lines to enable Spoof protection (reverse-path filter)
17 | # Turn on Source Address Verification in all interfaces to
18 | # prevent some spoofing attacks
19 | #net.ipv4.conf.default.rp_filter=1
20 | #net.ipv4.conf.all.rp_filter=1
21 |
22 | # Uncomment the next line to enable TCP/IP SYN cookies
23 | # See http://lwn.net/Articles/277146/
24 | # Note: This may impact IPv6 TCP sessions too
25 | #net.ipv4.tcp_syncookies=1
26 |
27 | # Uncomment the next line to enable packet forwarding for IPv4
28 | #net.ipv4.ip_forward=1
29 |
30 | # Uncomment the next line to enable packet forwarding for IPv6
31 | # Enabling this option disables Stateless Address Autoconfiguration
32 | # based on Router Advertisements for this host
33 | #net.ipv6.conf.all.forwarding=1
34 |
35 |
36 | ###################################################################
37 | # Additional settings - these settings can improve the network
38 | # security of the host and prevent against some network attacks
39 | # including spoofing attacks and man in the middle attacks through
40 | # redirection. Some network environments, however, require that these
41 | # settings are disabled so review and enable them as needed.
42 | #
43 | # Do not accept ICMP redirects (prevent MITM attacks)
44 | #net.ipv4.conf.all.accept_redirects = 0
45 | #net.ipv6.conf.all.accept_redirects = 0
46 | # _or_
47 | # Accept ICMP redirects only for gateways listed in our default
48 | # gateway list (enabled by default)
49 | # net.ipv4.conf.all.secure_redirects = 1
50 | #
51 | # Do not send ICMP redirects (we are not a router)
52 | #net.ipv4.conf.all.send_redirects = 0
53 | #
54 | # Do not accept IP source route packets (we are not a router)
55 | #net.ipv4.conf.all.accept_source_route = 0
56 | #net.ipv6.conf.all.accept_source_route = 0
57 | #
58 | # Log Martian Packets
59 | #net.ipv4.conf.all.log_martians = 1
60 | #
61 | fs.file-max=102550
62 |
63 | net.core.netdev_max_backlog=300000
64 | net.core.somaxconn=10000
65 | net.core.rps_sock_flow_entries=132768
66 |
67 | #net.core.rmem_default = 256960
68 | #net.core.rmem_max = 256960
69 | #net.core.wmem_default = 256960
70 | #net.core.wmem_max = 256960
71 | net.ipv4.tcp_timestamps = 0
72 | net.ipv4.tcp_sack =1
73 |
74 | #net.core.rmem_default = 556960
75 | #net.core.rmem_max = 556960
76 | #net.core.wmem_default = 556960
77 | #net.core.wmem_max = 556960
78 | #net.ipv4.tcp_sack =1
79 |
80 |
81 | #net.netfilter.nf_conntrack_max=1000000
82 | net.ipv4.tcp_max_syn_backlog=10000
83 | net.ipv4.tcp_max_tw_buckets=10
84 | net.ipv4.tcp_fin_timeout=10
85 | net.ipv4.tcp_timestamps=0
86 | net.ipv4.tcp_tw_recycle=0
87 | net.ipv4.tcp_tw_reuse=1
88 | net.ipv4.tcp_synack_retries=10
89 | net.ipv4.tcp_window_scaling=1
90 |
91 | net.ipv4.tcp_keepalive_time=10
92 | net.ipv4.tcp_keepalive_intvl=5
93 | net.ipv4.tcp_keepalive_probes=5
94 | net.ipv4.tcp_fin_timeout = 5
95 | net.ipv4.tcp_low_latency=1
96 |
97 | net.ipv4.ip_local_port_range=1024 65535
98 |
--------------------------------------------------------------------------------
/monitor/monitor.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "encoding/binary"
5 | "fmt"
6 | . "github.com/10billionhongbaos/lib/monitor"
7 | "github.com/10billionhongbaos/lib/parse"
8 | . "github.com/10billionhongbaos/lib/utils"
9 | "net"
10 | "os"
11 | "time"
12 | )
13 |
14 | var client1 Monitor
15 | var client2 Monitor
16 |
17 | var server1 Monitor
18 | var server2 Monitor
19 |
20 | func show() {
21 |
22 | ftime := time.Now().Format("2006-01-02_15:04:05")
23 |
24 | f, err := os.Create("log" + ftime + ".txt")
25 | if err != nil {
26 | panic(err)
27 | }
28 | defer f.Close()
29 | s := ""
30 | for {
31 | tnow := time.Now()
32 | t := tnow.Unix() - 30
33 |
34 | {
35 | v1, p1 := client1.Get(t)
36 | v2, p2 := client2.Get(t)
37 |
38 | for i := 0; i < ArrayLength; i++ {
39 | v1[i] += v2[i]
40 | p1[i] += p2[i]
41 | }
42 | s = PrintData(t, 0, v1, v1)
43 | fmt.Println(s)
44 |
45 | s += "\n"
46 | f.Write([]byte(s))
47 | }
48 |
49 | {
50 | v1, p1 := server1.Get(t)
51 | v2, p2 := server2.Get(t)
52 |
53 | for i := 0; i < ArrayLength; i++ {
54 | v1[i] += v2[i]
55 | p1[i] += p2[i]
56 | }
57 | s = PrintData(t, 1, v1, v1)
58 |
59 | fmt.Println(s)
60 |
61 | s += "\n"
62 | f.Write([]byte(s))
63 | }
64 | client1.Clean(t)
65 | client2.Clean(t)
66 | server1.Clean(t)
67 | server2.Clean(t)
68 | CorrectSleepOneSecond2(tnow)
69 | }
70 | }
71 |
72 | func PareseData(buf []byte) {
73 |
74 | var times uint64
75 | var types uint64
76 | var tmp uint64
77 |
78 | var p1 *Monitor
79 | var p2 *Monitor
80 |
81 | times = binary.BigEndian.Uint64(buf)
82 | types = binary.BigEndian.Uint64(buf[8:])
83 | // fmt.Println(times, times)
84 |
85 | // dataindex := int(times) % ArrayLength
86 |
87 | if types == 0 {
88 |
89 | p1 = &client1
90 | p2 = &client1
91 | } else {
92 |
93 | p1 = &server1
94 | p2 = &server1
95 | }
96 |
97 | index := 16
98 |
99 | for i := 0; i < ArrayLength; i++ {
100 | tmp = binary.BigEndian.Uint64(buf[index:])
101 | p1.Add(i, int64(tmp), int64(times))
102 | index += 8
103 | }
104 |
105 | for i := 0; i < ArrayLength; i++ {
106 | tmp = binary.BigEndian.Uint64(buf[index:])
107 | p2.Add(i, int64(tmp), int64(times))
108 | index += 8
109 |
110 | }
111 | }
112 |
113 | func main() {
114 | netListen, err := net.Listen("tcp", "0.0.0.0:8002")
115 | CheckErr(err)
116 | defer netListen.Close()
117 |
118 | go show()
119 | //
120 | for {
121 | conn, err := netListen.Accept()
122 | if err != nil {
123 | continue
124 | }
125 |
126 | // Log(conn.RemoteAddr().String(), " tcp connect success")
127 | go handleConnection(&conn)
128 | }
129 |
130 | }
131 |
132 | func handleConnection(conn *net.Conn) {
133 | defer (*conn).Close()
134 |
135 | data := make([]byte, 65536)
136 |
137 | for {
138 |
139 | if !parse.ReadByte(*conn, data, (ArrayLength*8*2 + 8 + 8)) {
140 | return
141 |
142 | }
143 | PareseData(data)
144 | }
145 |
146 | }
147 |
--------------------------------------------------------------------------------
/plot/hongbao.plt:
--------------------------------------------------------------------------------
1 |
2 | # qps.
3 | set terminal png size 1024,768
4 | set output 'qps.png'
5 | set autoscale
6 | set title "QPS"
7 | set grid
8 | set xlabel 'Time stamp'
9 | set ylabel 'Requests'
10 | set xdata time
11 | set timefmt "%H:%M:%S"
12 | set style data lines
13 | plot "qps.txt" using 1:2 title "Clinet QPS", '' using 1:3 title "Server QPS"
14 | replo
15 |
16 | set terminal png size 1024,768
17 | set output 'clientqps.png'
18 | set autoscale
19 | set title "QPS"
20 | set grid
21 | set xlabel 'Time stamp'
22 | set ylabel 'Requests'
23 | set xdata time
24 | set timefmt "%H:%M:%S"
25 | set style data lines
26 | plot "qps.txt" using 1:2 title "Clinet QPS"
27 | replo
28 |
29 | set terminal png size 1024,768
30 | set output 'serverqps.png'
31 | set autoscale
32 | set title "QPS"
33 | set grid
34 | set xlabel 'Time stamp'
35 | set ylabel 'Requests'
36 | set xdata time
37 | set timefmt "%H:%M:%S"
38 | set style data lines
39 | plot "qps.txt" using 1:3 title "Server QPS"
40 | replo
41 |
42 |
43 | set terminal png size 1024,768
44 | set output 'hongbaoAmount.png'
45 | set autoscale
46 | set title "hongbao Amount"
47 | set grid
48 | set xlabel 'Time stamp'
49 | set ylabel 'hongbao Amount'
50 | set xdata time
51 | set timefmt "%H:%M:%S"
52 | set style data lines
53 | plot "hongbaoAmount.txt" using 1:2 title "get hongbao Amount",'' using 1:3 title "create hongbao Amount"
54 | replo
55 |
56 | set terminal png size 1024,768
57 | set output 'createhongbao.png'
58 |
59 | set autoscale
60 | set title "createhongbao"
61 | set grid
62 | set xlabel 'Time stamp'
63 | set ylabel 'create hongbao number'
64 | set xdata time
65 | set timefmt "%H:%M:%S"
66 | set style data lines
67 | plot "createhongbao.txt" using 1:2 title "create hongbao num"
68 | replo
69 |
70 | #
71 | set terminal png size 1024,768
72 | set output 'gethongbao.png'
73 | set autoscale
74 | set title "gethongbao"
75 | set grid
76 | set xlabel 'Time stamp'
77 | set ylabel 'get hongbao number'
78 | set xdata time
79 | set timefmt "%H:%M:%S"
80 | set style data lines
81 | plot "gethongbao.txt" using 1:2 title "get hongbao num"
82 | replo
83 |
84 |
--------------------------------------------------------------------------------
/plot/parselog.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | import sys
4 | import datetime
5 |
6 | maps1 = {} # for client
7 | maps2 = {} # for server
8 | times = []
9 |
10 | def getTime(i):
11 | return datetime.datetime.fromtimestamp(int(i)).strftime('%H:%M:%S')
12 |
13 |
14 | for line in sys.stdin:
15 | v = line.split()
16 | print v
17 | if v[1] == "0":
18 | maps1[v[0]] = v
19 | if v[1] == "1":
20 | maps2[v[0]] = v
21 | times.append(v[0])
22 |
23 | # client and server qps
24 | f = open('qps.txt', 'w')
25 | for i in times:
26 | s = "" + getTime(i) + " "
27 | if maps1.has_key(i):
28 | v = maps1[i]
29 | s += v[3] + " "
30 | else:
31 | s += "0 "
32 | if maps2.has_key(i):
33 | v = maps2[i]
34 | s += v[3] + " "
35 | else:
36 | s += "0 "
37 | print >> f, s
38 | f.close()
39 |
40 | #hongbaoAmount.png
41 | f = open('hongbaoAmount.txt', 'w')
42 | for i in times:
43 | s = "" + getTime(i) + " "
44 | if maps1.has_key(i):
45 | v = maps1[i]
46 | s += v[17] + " "
47 | else:
48 | s += "0 "
49 | if maps2.has_key(i):
50 | v = maps1[i]
51 | s += v[17] + " "
52 | else:
53 | s += "0 "
54 | print >> f, s
55 | f.close()
56 |
57 | # create hongbao
58 | hongbaonum = 0
59 | f = open('createhongbao.txt', 'w')
60 | for i in times:
61 | s = "" + getTime(i) + " "
62 | if maps2.has_key(i):
63 | v = maps2[i]
64 | #num = int(v[15].split("/")[0])
65 | #strnum = "0"
66 | #if num != 0:
67 | # strnum = str(num - hongbaonum)
68 | # hongbaonum = num
69 | #s += strnum + " "
70 | v = maps2[i]
71 | s += v[7] + " "
72 | else:
73 | s += "0 "
74 | print >> f, s
75 | f.close()
76 |
77 | # get hongbao
78 | f = open('gethongbao.txt', 'w')
79 | for i in times:
80 | s = "" + getTime(i) + " "
81 | if maps1.has_key(i):
82 | v = maps1[i]
83 | s += v[7] + " "
84 | else:
85 | s += "0 "
86 | print >> f, s
87 | f.close()
88 |
89 |
--------------------------------------------------------------------------------
/server/server.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "github.com/10billionhongbaos/lib/flags"
6 | . "github.com/10billionhongbaos/lib/mailbox"
7 | . "github.com/10billionhongbaos/lib/message"
8 | "github.com/10billionhongbaos/lib/monitor"
9 | "github.com/10billionhongbaos/lib/parse"
10 | "github.com/10billionhongbaos/lib/utils"
11 | "math/rand"
12 | "net"
13 | "net/http"
14 | _ "net/http/pprof"
15 | "runtime"
16 | // "runtime/debug"
17 | "strconv"
18 | "time"
19 | )
20 |
21 | const (
22 | MaxUser = 1200000
23 | MailBoxNum = 256
24 | MailBoxUserNum = MaxUser / MailBoxNum
25 | MailBoxMessageLength = MailBoxUserNum
26 | HongBaoBoxMessageLength = 120000
27 | MaxQPS = 65000
28 | HongBaoMailBoxNum = 5
29 | GCCycle = 5
30 | MessagePoolNum = 1024
31 | MessagePoolUser = MaxUser / MessagePoolNum
32 | MessagePoolMessageNum = MaxUser / MessagePoolNum * 3
33 | )
34 |
35 | var gMailbox []MessageProcess
36 | var gHongbaoMailbox []MailBox
37 | var gPool []MessagePool
38 |
39 | func getOnePool(num int) *MessagePool {
40 | for {
41 | p := gPool[num%MessagePoolNum].GetPool()
42 | if p == nil {
43 | num += 1
44 | } else {
45 | return p
46 | }
47 | }
48 | }
49 |
50 | func InitProcess() {
51 | gPool = make([]MessagePool, (MessagePoolNum + 2))
52 | for i := 0; i < (MessagePoolNum + 2); i++ {
53 | num := MessagePoolMessageNum
54 | if i >= MessagePoolNum {
55 | num = MessagePoolMessageNum * 4 * 15
56 | }
57 | gPool[i].Init(num, MessagePoolUser)
58 | for k := 0; k < num; k++ {
59 | p := &Message{}
60 | p.Init()
61 | p.Pool = &gPool[i]
62 | gPool[i].Push(p)
63 | }
64 | }
65 |
66 | gHongbaoMailbox = make([]MailBox, HongBaoMailBoxNum)
67 | for i := 0; i < HongBaoMailBoxNum; i++ {
68 | gHongbaoMailbox[i].Init(120000)
69 | }
70 | gMailbox = make([]MessageProcess, MailBoxNum)
71 | for i := 0; i < MailBoxNum; i++ {
72 | gMailbox[i].Init(MailBoxMessageLength, HongBaoBoxMessageLength)
73 | }
74 | for i := 0; i < MailBoxNum; i++ {
75 | go ProcessRequest(i)
76 | }
77 |
78 | }
79 |
80 | func ProcessRequest(index int) {
81 | for {
82 | p1, _ := gMailbox[index].PopInput()
83 |
84 | switch p1.Cmd {
85 | case YaoHongbaoRequest_ID:
86 | {
87 | p2, b2 := gMailbox[index].PopYao()
88 | if b2 == true && p2 != nil {
89 | // send this request
90 | b2 := parse.WriteMessage(p1.Conn, p2)
91 | if b2 != nil {
92 | (*(p1.Conn)).Close()
93 | }
94 |
95 | monitor.GMonitor.Add(monitor.SendHongbao, 1, time.Now().Unix())
96 |
97 | } else {
98 | a := p1.Pool.Pop()
99 | a.Cmd = YaoHongbaoResponse_ID
100 | p := YaoHongbaoResponse{}
101 | p.Userid = p1.P3.Userid
102 | p.Result = monitor.NoHongbao
103 | p.Hongbaoid = 0
104 | a.P4 = &p
105 |
106 | b2 := parse.WriteMessage(p1.Conn, a)
107 | if b2 != nil {
108 | (*(p1.Conn)).Close()
109 | }
110 | }
111 | }
112 | case GetHongbaoRequest_ID:
113 | {
114 | index := p1.P6.Hongbaoid % HongBaoMailBoxNum
115 | gHongbaoMailbox[index].Push(p1)
116 | }
117 | }
118 | p1.Rollback()
119 |
120 | }
121 | }
122 |
123 | func main() {
124 |
125 | //建立socket,监听端口
126 | flags.Parse()
127 | runtime.GOMAXPROCS(runtime.NumCPU())
128 | //fmt.Println(debug.SetGCPercent(-1) )
129 |
130 | go func() {
131 | http.ListenAndServe("0.0.0.0:6060", nil)
132 | }()
133 |
134 | //go utils.GC(GCCycle)
135 | netListen, err := net.Listen("tcp", "0.0.0.0:"+*flags.ListPort)
136 | utils.CheckErr(err)
137 | defer netListen.Close()
138 | //
139 | YaoHongbaoId = 0x100000
140 | FaHongbaoId = 0x200000
141 | InitProcess()
142 | go monitor.Show(*(flags.MonitorServer), 1)
143 | go httpServer()
144 | for i := 0; i < HongBaoMailBoxNum; i++ {
145 | go ProcessGetHongbao(i)
146 | }
147 | num := 0
148 | for {
149 | conn, err := netListen.Accept()
150 | if err != nil {
151 | continue
152 | }
153 | num += 1
154 | ppool := getOnePool(num)
155 |
156 | // Log(conn.RemoteAddr().String(), " tcp connect success")
157 | go handleConnection(&conn, ppool)
158 | }
159 | }
160 |
161 | //处理连接
162 | func handleConnection(conn *net.Conn, ppool *MessagePool) {
163 | defer (*conn).Close()
164 | defer ppool.Reduce()
165 | // 先拿到login Request
166 | p, result := parse.ReadMessage(conn, ppool)
167 | if !result {
168 | return
169 | }
170 |
171 | tnow := time.Now()
172 | monitor.GMonitor.Add(monitor.Request, 1, tnow.Unix())
173 | monitor.GMonitor.Add(p.Cmd, 1, tnow.Unix())
174 | if p.Cmd != LoginRequest_ID {
175 | fmt.Print("wrong request")
176 | return
177 |
178 | }
179 |
180 | index, _ := utils.GetMailBoxAddress(MailBoxNum, int(p.P1.Userid))
181 | gMailbox[index].SetConn(conn, MailBoxNum, int(p.P1.Userid))
182 | defer gMailbox[index].SetConn(nil, MailBoxNum, int(p.P1.Userid))
183 |
184 | userid := p.P1.Userid
185 | {
186 | a := ppool.Pop()
187 | a.Cmd = LoginResponse_ID
188 | p := LoginResponse{}
189 | p.Userid = userid
190 | p.Result = 0
191 | a.P2 = &p
192 |
193 | b2 := parse.WriteMessage(conn, a)
194 | if b2 != nil {
195 | (*conn).Close()
196 | }
197 | }
198 |
199 | for {
200 | p, result := parse.ReadMessage(conn, ppool)
201 | if !result {
202 | break
203 | }
204 | tnow := time.Now()
205 | if (uint64(tnow.Unix()) > p.TimeStamp) && (uint64(tnow.Unix())-p.TimeStamp > 5) {
206 | //fmt.Println("delay message", uint64(tnow.Unix()), p.TimeStamp, p.Cmd)
207 | p.TimeStamp = uint64(tnow.Unix())
208 | }
209 | qps := monitor.GMonitor.Add(monitor.Request, 1, tnow.Unix())
210 | monitor.GMonitor.Add(p.Cmd, 1, tnow.Unix())
211 | p.Conn = conn
212 |
213 | if qps > MaxQPS && p.Cmd == YaoHongbaoRequest_ID { // too many yao request
214 |
215 | {
216 | a := ppool.Pop()
217 | a.Cmd = YaoHongbaoResponse_ID
218 | p := YaoHongbaoResponse{}
219 | p.Userid = userid
220 | p.Result = monitor.OverLoad
221 | p.Hongbaoid = 0
222 | a.P4 = &p
223 |
224 | b2 := parse.WriteMessage(conn, a)
225 | if b2 != nil {
226 | (*conn).Close()
227 | }
228 |
229 | }
230 | p.Rollback()
231 |
232 | continue
233 | }
234 | gMailbox[index].PushInput(p)
235 |
236 | }
237 |
238 | }
239 |
240 | func getRequestValue(req *http.Request, key string) int {
241 |
242 | filelist, found1 := req.Form[key]
243 |
244 | if !(found1) {
245 | fmt.Println("no qps")
246 | return 0
247 | }
248 |
249 | value, err := strconv.Atoi(filelist[0])
250 | if err != nil {
251 | return 0
252 | }
253 | return value
254 |
255 | }
256 |
257 | func fahongbao(w http.ResponseWriter, req *http.Request) {
258 |
259 | req.ParseForm()
260 | fmt.Println(req.Form)
261 |
262 | sum := 0
263 | qps := 0
264 | minuserid := 0
265 | maxuserid := 0
266 |
267 | qps = getRequestValue(req, "qps")
268 | sum = getRequestValue(req, "sum")
269 | minuserid = getRequestValue(req, "min")
270 | maxuserid = getRequestValue(req, "max")
271 | if minuserid < 0 {
272 | minuserid = -minuserid
273 | }
274 | if maxuserid < 0 {
275 | maxuserid = -maxuserid
276 | }
277 | if maxuserid < minuserid {
278 | t := minuserid
279 | minuserid = maxuserid
280 | maxuserid = t
281 | }
282 | hongbaoid := FaHongbaoId
283 | FaHongbaoId += int64(sum)
284 | fmt.Println(sum, qps, hongbaoid, minuserid, maxuserid)
285 | go FaHongbao(sum, qps, hongbaoid, minuserid, maxuserid)
286 |
287 | }
288 |
289 | func yaohongbao(w http.ResponseWriter, req *http.Request) {
290 |
291 | req.ParseForm()
292 |
293 | fmt.Println(req.Form)
294 | sum := 0
295 | qps := 0
296 |
297 | qps = getRequestValue(req, "qps")
298 | sum = getRequestValue(req, "sum")
299 | fmt.Println("qps:", qps, "sum", sum)
300 | hongbaoid := YaoHongbaoId
301 | YaoHongbaoId += int64(sum)
302 | go YaoHongbao(sum, qps, hongbaoid)
303 |
304 | }
305 |
306 | var YaoHongbaoId int64
307 | var YaoHongbaoMoney int64
308 |
309 | func YaoHongbao(sum int, qps int, hongbaoid int64) {
310 | x := 0
311 | rand.Seed(time.Now().UnixNano() % 1e9)
312 |
313 | utils.CorrectSleepOneSecond()
314 |
315 | tnow := time.Now()
316 | for i := 0; i < sum; i++ {
317 | x += 1
318 | {
319 | a := gPool[MessagePoolNum].Pop()
320 | a.Cmd = YaoHongbaoResponse_ID
321 | p := YaoHongbaoResponse{}
322 | p.Userid = 0
323 | p.Result = monitor.GetHongbao
324 | p.Hongbaoid = uint32(hongbaoid)
325 | money := uint32(rand.Intn(300) + 10)
326 | p.Amount = money
327 | a.P4 = &p
328 | gMailbox[i%MailBoxNum].PushYao(a)
329 |
330 | monitor.GMonitor.Add(monitor.GetHongbao, 1, tnow.Unix())
331 | monitor.GMonitor.Add2(monitor.CountYaoHongbaoReq, 1)
332 | monitor.GMonitor.Add2(monitor.SumYaoHongbao, int64(money))
333 |
334 | }
335 | if x >= qps {
336 | utils.CorrectSleepOneSecond2(tnow)
337 | tnow = time.Now()
338 | x = 0
339 | }
340 | }
341 |
342 | }
343 |
344 | var FaHongbaoId int64
345 | var FaHongbaoMoney int64
346 |
347 | func FaOneHongbao(hongbaoid int64, minuserid int, maxuserid int) {
348 | users := [3]int{}
349 | for i := 0; i < 3; i++ {
350 | users[i] = rand.Intn(maxuserid-minuserid) + minuserid
351 | }
352 | Amount := rand.Intn(300) + 10
353 | for i := 3; i >= 0; i-- {
354 |
355 | a := gPool[MessagePoolNum+1].Pop()
356 | a.Cmd = FaHongbaoRequest_ID
357 | p := &FaHongbaoRequest{}
358 | p.Hongbaoid = uint32(hongbaoid)
359 | p.Amount = uint32(Amount)
360 | p.Useramount = 3
361 | p.Userid1 = uint32(users[0])
362 | p.Userid2 = uint32(users[1])
363 | p.Userid3 = uint32(users[2])
364 | a.P5 = p
365 | if i < 3 {
366 | index, _ := utils.GetMailBoxAddress(MailBoxNum, users[i])
367 | gMailbox[index].PushYao(a)
368 | } else {
369 | gindex := hongbaoid % HongBaoMailBoxNum
370 | gHongbaoMailbox[gindex].Push(a)
371 | monitor.GMonitor.Add2(monitor.CountFaHongbaoReq, 1)
372 | monitor.GMonitor.Add2(monitor.SumFaHongbao, int64(Amount))
373 | }
374 |
375 | }
376 | }
377 |
378 | func FaHongbao(sum int, qps int, hongbaoid int64, minuserid int, maxuserid int) {
379 | x := 0
380 | rand.Seed(time.Now().UnixNano() % 1e9)
381 | utils.CorrectSleepOneSecond()
382 |
383 | tnow := time.Now()
384 | for i := 0; i < sum; i++ {
385 | x += 1
386 | {
387 | FaOneHongbao(hongbaoid, minuserid, maxuserid)
388 | hongbaoid += 1
389 | }
390 | if x >= qps {
391 | utils.CorrectSleepOneSecond2(tnow)
392 | tnow = time.Now()
393 | x = 0
394 | }
395 | }
396 |
397 | }
398 |
399 | func httpServer() {
400 |
401 | http.HandleFunc("/fahongbao", fahongbao)
402 | http.HandleFunc("/yaohongbao", yaohongbao)
403 | //服务器要监听的主机地址和端口号
404 | err := http.ListenAndServe("0.0.0.0:8989", nil)
405 | utils.CheckErr(err)
406 | }
407 |
408 | type HongbaoRecord struct {
409 | p *FaHongbaoRequest
410 | Amount int
411 | Useramount int
412 | GotUseramount int
413 | GotAmount int
414 | }
415 |
416 | func SplitHongbao(Amount int, usernum int) int {
417 | if usernum == 1 {
418 | return Amount
419 | }
420 | if Amount <= usernum {
421 | return 1
422 | }
423 | if Amount/usernum < 1 {
424 | return 1
425 | }
426 | return rand.Intn(Amount / usernum)
427 | }
428 |
429 | func ProcessOneHongbao(p *HongbaoRecord) (bool, int) {
430 |
431 | num := SplitHongbao(p.Amount-p.GotAmount, p.Useramount-p.GotUseramount)
432 | p.GotAmount += num
433 | p.GotUseramount += 1
434 |
435 | if p.GotUseramount == p.Useramount {
436 | return true, num
437 | }
438 | return false, num
439 |
440 | }
441 |
442 | func ProcessGetHongbao(boxindex int) {
443 |
444 | hongbaoMap := make(map[uint32]*HongbaoRecord)
445 |
446 | for {
447 | mess, _ := gHongbaoMailbox[boxindex].BlockingPop()
448 | switch mess.Cmd {
449 | case FaHongbaoRequest_ID:
450 | {
451 | hongbaoid := mess.P5.Hongbaoid
452 | _, exists := hongbaoMap[hongbaoid]
453 | if !exists {
454 | p := &HongbaoRecord{}
455 | p.Amount = int(mess.P5.Amount)
456 | p.Useramount = 3
457 | hongbaoMap[hongbaoid] = p
458 | }
459 | }
460 |
461 | case GetHongbaoRequest_ID:
462 | {
463 | hongbaoid := mess.P6.Hongbaoid
464 | p, exists := hongbaoMap[hongbaoid]
465 | finished := false
466 | num := 0
467 |
468 | tnow2 := time.Now()
469 | if (uint64(tnow2.Unix()) > mess.TimeStamp) && (uint64(tnow2.Unix())-mess.TimeStamp > 5) {
470 | fmt.Println("process delay message box: ", boxindex, " ", monitor.Request, " ", uint64(tnow2.Unix()), " ", mess.TimeStamp, uint64(tnow2.Unix())-mess.TimeStamp)
471 | }
472 |
473 | if exists {
474 | finished, num = ProcessOneHongbao(p)
475 | if finished {
476 | delete(hongbaoMap, hongbaoid)
477 | if len(hongbaoMap) == 0 {
478 | fmt.Println("hongbaomap", len(hongbaoMap))
479 | }
480 | }
481 | // send a response to client
482 |
483 | {
484 | a := mess.Pool.Pop()
485 | a.Cmd = GetHongbaoRespone_ID
486 | p := GetHongbaoRespone{}
487 | p.Hongbaoid = hongbaoid
488 | p.Amount = uint32(num)
489 | a.P7 = &p
490 |
491 | b2 := parse.WriteMessage(mess.Conn, a)
492 | if b2 != nil {
493 | (*mess.Conn).Close()
494 | }
495 | monitor.GMonitor.Add2(monitor.CountGetFaHongbaoReq, 1)
496 |
497 | monitor.GMonitor.Add(GetHongbaoRespone_ID, 1, time.Now().Unix())
498 |
499 | }
500 | }
501 |
502 | }
503 | }
504 | }
505 | }
506 |
--------------------------------------------------------------------------------