Crossfire Server, Trunk  R22047
comet_perf.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_alchemy_c =
3  * "$Id: check_alchemy.c 4640 2006-06-07 21:44:18Z tchize $";
4  */
5 
6 /*
7  * CrossFire, A Multiplayer game for X-windows
8  *
9  * Copyright (C) 2007 Mark Wedel & Crossfire Development Team
10  * Copyright (C) 1992 Frank Tore Johansen
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * The authors can be reached via e-mail at crossfire-devel@real-time.com
27  */
28 
29 /*
30  * This tests the comet spell. My main motivation for writing this
31  * was to have a consistent test I could use for performance testing.
32  * But I also wanted to make sure that the results were close before and
33  * after the performance changes - make the spell use less cpu time
34  * but having drastically different results probably isn't a good thing
35  * either.
36  * To really be useful, everything needs to be compiled with profiling
37  * (-pg). This can be done like 'setenv CFLAGS -pg; ./configure;
38  * make clean; make'. The make clean is necessary because it won't
39  * recompile the objects based just on changes the the CFLAGS.
40  *
41  * Note that this test, even after performance changes are done, still
42  * isn't bad - it checks several things - map creation, spell casting,
43  * etc. It wouldn't be hard to use this as a template to test things
44  * like resistance code, etc.
45  */
46 
47 #include <stdlib.h>
48 #include <check.h>
49 
50 #include <global.h>
51 #include <sproto.h>
52 #include <toolkit_common.h>
53 
54 #define TEST_MAP_SIZE 40
55 #define NUM_TICKS_TO_RUN 500
56 #define NUM_COMETS_TO_CAST 30
57 #define STARTING_HP 25000
58 
59 /* The percentage, either plus or minus, that the results can
60  * vary from the baseline and still be considered OK.
61  * Note a good sanity check to make sure you put in the correct
62  * values is to set this to 0.0 - in that case, checks should
63  * pass.
64  */
65 #define HP_DELTA 10
66 
67 /* The first time you set up a test, you want to dump the
68  * initial values to put into the hp_row/hp_diag arrays.
69  * If this is set, it prints those values instead of doing
70  * a comparision.
71  */
72 /*#define PRINT_DEBUG_HP */
73 
75 object *mon;
76 
77 static void setup(void) {
78  object *mon1;
79  int x, i;
80 
82 
83  mon = create_archetype("orc");
84  fail_unless(mon != NULL, "Unable to find orc object");
85 
86  /* We customize the monster a bit so it is consistent -
87  * give it a bunch of HP so it can survive the attacks,
88  * set it speed to 0 so it doesn't do anything, etc.
89  */
90  for (i = 0; i < NROFATTACKS; i++)
91  mon->resist[i] = 95;
92  mon->stats.hp = STARTING_HP;
93  mon->stats.maxhp = STARTING_HP;
94  mon->speed = 0.0;
95  mon->speed_left = 0.0;
98 
99  /* We now make copies of this custom monster and put it into the
100  * map. We make a diagonal from one corner to another,
101  * as well as a line across the middle of the map (\ overlayed with -)
102  * We could fill most of the map with monsters, but I think the
103  * diagonal + horizontal should give a pretty representative
104  * value of creatures being hit.
105  */
106  for (x = 0; x < TEST_MAP_SIZE; x++) {
107  mon1 = object_new();
108  object_copy(mon, mon1);
109  object_insert_in_map_at(mon1, test_map, NULL, 0, x, TEST_MAP_SIZE/2);
110 
111  if (x != TEST_MAP_SIZE/2) {
112  mon1 = object_new();
113  object_copy(mon, mon1);
114  object_insert_in_map_at(mon1, test_map, NULL, 0, x, x);
115  }
116  }
117 
118 }
119 
120 static void teardown(void) {
121  free_map(test_map);
122 }
123 
124 static void check_hp(const char *test, int hp_row[TEST_MAP_SIZE], int hp_diag[TEST_MAP_SIZE]) {
125  object *our_mon;
126  int x, diff;
127 
128 #ifdef PRINT_DEBUG_HP
129  printf("\nDumping debug hp for test %s\n ", test);
130 #endif
131 
132  /* Dump the HP of the monsters now. We do it in 2 passes,
133  * as I think it is easier to read that way.
134  */
135  for (x = 0; x < TEST_MAP_SIZE; x++) {
136  our_mon = GET_MAP_OB(test_map, x, TEST_MAP_SIZE/2);
137  if (!our_mon) {
138  fail("Monster destroyed at %d, %d\n", x, TEST_MAP_SIZE/2);
139  continue;
140  }
141  fail_unless(mon->name == our_mon->name, "Could not find our monster on the space?");
142 
143 #ifdef PRINT_DEBUG_HP
144  printf("%d, ", our_mon->stats.hp);
145 #else
146 
147  if (our_mon->stats.hp == hp_row[x]) {
148  diff = 0;
149  } else if (our_mon->stats.hp < hp_row[x]) {
150  diff = 100-(STARTING_HP-hp_row[x])*100/((STARTING_HP-our_mon->stats.hp) ? (STARTING_HP-our_mon->stats.hp) : 1);
151  } else {
152  diff = -(100-(STARTING_HP-our_mon->stats.hp)*100/((STARTING_HP-hp_row[x]) ? (STARTING_HP-hp_row[x]) : 1));
153  }
154 
155  if (FABS(diff) > HP_DELTA) {
156  fail("Mon (%d, %d) has hp out of range (%d != %d +/- %d, diff %d)\n", our_mon->x, our_mon->y, our_mon->stats.hp, hp_row[x], HP_DELTA, diff);
157  }
158 #endif
159  }
160 
161 #ifdef PRINT_DEBUG_HP
162  printf("\n\n");
163 #endif
164 
165  for (x = 0; x < TEST_MAP_SIZE; x++) {
166  our_mon = GET_MAP_OB(test_map, x, x);
167  if (!our_mon) {
168  fprintf(stderr, "Monster destroyed at %d, %d\n", x, x);
169  continue;
170  }
171 
172  fail_unless(mon->name == our_mon->name, "Could not find our monster on the space?");
173 
174 #ifdef PRINT_DEBUG_HP
175  printf("%d, ", our_mon->stats.hp);
176 #else
177  if (our_mon->stats.hp == hp_diag[x]) {
178  diff = 0;
179  } else if (our_mon->stats.hp < hp_diag[x]) {
180  diff = 100-(STARTING_HP-hp_diag[x])*100/((STARTING_HP-our_mon->stats.hp) ? (STARTING_HP-our_mon->stats.hp) : 1);
181  } else {
182  diff = -(100-(STARTING_HP-our_mon->stats.hp)*100/((STARTING_HP-hp_diag[x]) ? (STARTING_HP-hp_diag[x]) : 1));
183  }
184 
185  if (FABS(diff) > HP_DELTA) {
186  fail("Mon (%d, %d) has hp out of range (%d != %d +/- %d, diff %d)\n", our_mon->x, our_mon->y, our_mon->stats.hp, hp_diag[x], HP_DELTA, diff);
187  }
188 #endif
189  }
190 }
191 
192 START_TEST(cast_one_comet) {
193  int hp_row[TEST_MAP_SIZE] = {25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 24924, 24920, 24916, 24912, 24908, 24904, 24900, 24896, 24892, 24888, 24884, 24880, 24869, 24880, 24884, 24888, 24892, 24896, 24900, 24904, 24908, 24912, 24916, 24920, 24924, 25000, 25000, 25000, 25000, 25000, 25000, 25000 },
194  hp_diag[TEST_MAP_SIZE] = {25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 24924, 24920, 24916, 24912, 24908, 24904, 24900, 24896, 24892, 24888, 24884, 24880, 24869, 24880, 24884, 24888, 24892, 24896, 24900, 24904, 24908, 24912, 24916, 24920, 24924, 25000, 25000, 25000, 25000, 25000, 25000, 25000 };
195  object *comet, *rod;
196  int tick;
197 
198  rod = create_archetype("rod_heavy");
199  rod->level = 100;
200  comet = create_archetype("spell_comet");
201  object_insert_in_ob(comet, rod);
202 
203  object_insert_in_map_at(rod, test_map, NULL, 0, TEST_MAP_SIZE/2, TEST_MAP_SIZE-1);
204 
205  cast_spell(rod, rod, 1, rod->inv, NULL);
206  for (tick = 0; tick < NUM_TICKS_TO_RUN; tick++) {
207  process_events();
208  }
209 
210  check_hp("cast_one_comet", hp_row, hp_diag);
211 }
212 END_TEST
213 
214 START_TEST(cast_random_comet) {
215  object *comet, *rod;
216  int tick, num_cast = 0;
217  int hp_row[TEST_MAP_SIZE] = {23756, 23617, 23516, 23428, 23397, 23291, 23203, 23097, 23014, 22875, 22801, 22782, 22706, 22707, 22620, 22645, 22646, 22595, 22705, 22773, 22809, 22835, 22975, 23098, 23239, 23346, 23462, 23597, 23627, 23675, 23786, 23888, 24001, 24119, 24206, 24306, 24336, 24455, 24565, 24649 },
218  hp_diag[TEST_MAP_SIZE] = {25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 23515, 23351, 23177, 23097, 22946, 22931, 22763, 22706, 22678, 22658, 22728, 22812, 22809, 22712, 22728, 22741, 22726, 22833, 22862, 22967, 23014, 23009, 23167, 23267, 23367, 23459, 23596, 23713, 23750, 23879, 24026, 24160 };
219 
220  rod = create_archetype("rod_heavy");
221  rod->level = 100;
222  comet = create_archetype("spell_comet");
223  object_insert_in_ob(comet, rod);
224 
225  object_insert_in_map_at(rod, test_map, NULL, 0, TEST_MAP_SIZE/2, TEST_MAP_SIZE-1);
226 
227  for (tick = 0; tick < NUM_TICKS_TO_RUN; tick++) {
228  if (num_cast < NUM_COMETS_TO_CAST && (tick%1) == 0) {
229  object_remove(rod);
230 
231  /* The idea here on the x is to shuffle the spaces around
232  * a little, as a more typical case is comets
233  * blowing up on different spaces.
234  */
235  object_insert_in_map_at(rod, test_map, NULL, 0, (tick*59)%37, TEST_MAP_SIZE-1);
236 
237  cast_spell(rod, rod, 1, rod->inv, NULL);
238  num_cast++;
239  }
240  process_events();
241  }
242  check_hp("cast_random_comet", hp_row, hp_diag);
243 
244 }
245 END_TEST
246 
247 START_TEST(cast_bunch_comet) {
248  object *comet, *rod;
249  int tick, num_cast = 0;
250  int hp_row[TEST_MAP_SIZE] = {25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 22355, 22262, 22115, 21966, 21837, 21684, 21554, 21424, 21268, 21137, 21006, 20875, 20534, 20875, 21006, 21137, 21268, 21424, 21554, 21684, 21837, 21966, 22115, 22262, 22355, 25000, 25000, 25000, 25000, 25000, 25000, 25000 },
251  hp_diag[TEST_MAP_SIZE] = {25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 22355, 22262, 22115, 21966, 21837, 21684, 21554, 21424, 21268, 21137, 21006, 20875, 20534, 20875, 21006, 21137, 21268, 21424, 21554, 21684, 21837, 21966, 22115, 22262, 22355, 25000, 25000, 25000, 25000, 25000, 25000, 25000 };
252 
253  rod = create_archetype("rod_heavy");
254  rod->level = 100;
255  comet = create_archetype("spell_comet");
256  object_insert_in_ob(comet, rod);
257 
258  object_insert_in_map_at(rod, test_map, NULL, 0, TEST_MAP_SIZE/2, TEST_MAP_SIZE-1);
259 
260  for (tick = 0; tick < NUM_TICKS_TO_RUN; tick++) {
261  if (num_cast < NUM_COMETS_TO_CAST && (tick%1) == 0) {
262  cast_spell(rod, rod, 1, rod->inv, NULL);
263  num_cast++;
264  }
265  process_events();
266  }
267  check_hp("cast_bunch_comet", hp_row, hp_diag);
268 
269 }
270 END_TEST
271 
272 static Suite *comet_suite(void) {
273  Suite *s = suite_create("comet");
274  TCase *tc_core = tcase_create("Core");
275 
276  /* check by defaults has a 2 second timeout - that isn't
277  * fast enough on my system - a run of 30 comets takes about
278  * 7 seconds. Setting this to 20 is enough, but on a slower
279  * system may not be.
280  */
281  tcase_set_timeout(tc_core, 20);
282 
283  /*setup and teardown will be called before each test in testcase 'tc_core' */
284  tcase_add_checked_fixture(tc_core, setup, teardown);
285 
286  suite_add_tcase(s, tc_core);
287  tcase_add_test(tc_core, cast_one_comet);
288  tcase_add_test(tc_core, cast_bunch_comet);
289  tcase_add_test(tc_core, cast_random_comet);
290 
291  return s;
292 }
293 
294 int main(void) {
295  int nf;
296 
297  Suite *s = comet_suite();
298  SRunner *sr = srunner_create(s);
299 
300  /* Don't want to fork - if we do, we lose the profile (-pg)
301  * compiled information, which is what I used to determine if
302  * things are more efficient.
303  */
304  srunner_set_fork_status(sr, CK_NOFORK);
305 
306  /* Only want to run this once, so don't put it in setup() */
307  cctk_setdatadir(SOURCE_ROOT "lib");
308  init(0, NULL);
309 
310  srunner_set_xml(sr, LOGDIR "/unit/server/comet.xml");
311  srunner_set_log(sr, LOGDIR "/unit/server/comet.out");
312  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
313  nf = srunner_ntests_failed(sr);
314  srunner_free(sr);
315  fprintf(stderr, "Got %"FMT64U" supressions, %"FMT64U" spell merges, %"FMT64U" full tables\n", statistics.spell_suppressions, statistics.spell_merges, statistics.spell_hash_full);
316  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
317 }
#define NUM_TICKS_TO_RUN
Definition: comet_perf.c:55
struct Statistics statistics
Definition: init.c:101
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.c:870
#define SET_FLAG(xyz, p)
Definition: define.h:223
#define FABS(x)
Definition: define.h:22
#define FLAG_STAND_STILL
Definition: define.h:309
uint64_t spell_merges
Definition: global.h:355
void cctk_setdatadir(const char *datadir)
object * mon
Definition: comet_perf.c:75
int main(void)
Definition: comet_perf.c:294
#define FMT64U
Definition: compat.h:13
START_TEST(cast_one_comet)
Definition: comet_perf.c:192
#define TEST_MAP_SIZE
Definition: comet_perf.c:54
mapstruct * test_map
Definition: comet_perf.c:74
static void setup(void)
Definition: comet_perf.c:77
void free_map(mapstruct *m)
Definition: map.c:1689
uint64_t spell_hash_full
Definition: global.h:356
#define NUM_COMETS_TO_CAST
Definition: comet_perf.c:56
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1849
object * object_new(void)
Definition: object.c:1011
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2602
object * create_archetype(const char *name)
Definition: arch.cpp:281
#define STARTING_HP
Definition: comet_perf.c:57
#define HP_DELTA
Definition: comet_perf.c:65
void init(int argc, char **argv)
Definition: init.c:1033
void process_events(void)
Definition: server.c:979
static void check_hp(const char *test, int hp_row[TEST_MAP_SIZE], int hp_diag[TEST_MAP_SIZE])
Definition: comet_perf.c:124
static END_TEST Suite * comet_suite(void)
Definition: comet_perf.c:272
uint64_t spell_suppressions
Definition: global.h:357
#define NROFATTACKS
Definition: attack.h:17
#define GET_MAP_OB(M, X, Y)
Definition: map.h:173
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:824
static void teardown(void)
Definition: comet_perf.c:120
Definition: map.h:326
void object_update_speed(object *op)
Definition: object.c:1086
int cast_spell(object *op, object *caster, int dir, object *spell_ob, char *stringarg)
Definition: spell_util.c:1459
void object_remove(object *op)
Definition: object.c:1588