Crossfire Server, Trunk
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;
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) {
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 #else
131  (void)test;
132 #endif
133 
134  /* Dump the HP of the monsters now. We do it in 2 passes,
135  * as I think it is easier to read that way.
136  */
137  for (x = 0; x < TEST_MAP_SIZE; x++) {
138  our_mon = GET_MAP_OB(test_map, x, TEST_MAP_SIZE/2);
139  if (!our_mon) {
140  fail("Monster destroyed at %d, %d\n", x, TEST_MAP_SIZE/2);
141  continue;
142  }
143  fail_unless(mon->name == our_mon->name, "Could not find our monster on the space?");
144 
145 #ifdef PRINT_DEBUG_HP
146  printf("%d, ", our_mon->stats.hp);
147 #else
148 
149  if (our_mon->stats.hp == hp_row[x]) {
150  diff = 0;
151  } else if (our_mon->stats.hp < hp_row[x]) {
152  diff = 100-(STARTING_HP-hp_row[x])*100/((STARTING_HP-our_mon->stats.hp) ? (STARTING_HP-our_mon->stats.hp) : 1);
153  } else {
154  diff = -(100-(STARTING_HP-our_mon->stats.hp)*100/((STARTING_HP-hp_row[x]) ? (STARTING_HP-hp_row[x]) : 1));
155  }
156 
157  if (FABS(diff) > HP_DELTA) {
158  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);
159  }
160 #endif
161  }
162 
163 #ifdef PRINT_DEBUG_HP
164  printf("\n\n");
165 #endif
166 
167  for (x = 0; x < TEST_MAP_SIZE; x++) {
168  our_mon = GET_MAP_OB(test_map, x, x);
169  if (!our_mon) {
170  fprintf(stderr, "Monster destroyed at %d, %d\n", x, x);
171  continue;
172  }
173 
174  fail_unless(mon->name == our_mon->name, "Could not find our monster on the space?");
175 
176 #ifdef PRINT_DEBUG_HP
177  printf("%d, ", our_mon->stats.hp);
178 #else
179  if (our_mon->stats.hp == hp_diag[x]) {
180  diff = 0;
181  } else if (our_mon->stats.hp < hp_diag[x]) {
182  diff = 100-(STARTING_HP-hp_diag[x])*100/((STARTING_HP-our_mon->stats.hp) ? (STARTING_HP-our_mon->stats.hp) : 1);
183  } else {
184  diff = -(100-(STARTING_HP-our_mon->stats.hp)*100/((STARTING_HP-hp_diag[x]) ? (STARTING_HP-hp_diag[x]) : 1));
185  }
186 
187  if (FABS(diff) > HP_DELTA) {
188  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);
189  }
190 #endif
191  }
192 }
193 
194 START_TEST(cast_one_comet) {
195  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 },
196  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 };
197  object *comet, *rod;
198  int tick;
199 
200  rod = create_archetype("rod_heavy");
201  rod->level = 100;
202  comet = create_archetype("spell_comet");
203  object_insert_in_ob(comet, rod);
204 
206 
207  cast_spell(rod, rod, 1, rod->inv, NULL);
208  for (tick = 0; tick < NUM_TICKS_TO_RUN; tick++) {
209  process_events();
210  }
211 
212  check_hp("cast_one_comet", hp_row, hp_diag);
213 }
214 END_TEST
215 
216 START_TEST(cast_random_comet) {
217  object *comet, *rod;
218  int tick, num_cast = 0;
219  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 },
220  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 };
221 
222  rod = create_archetype("rod_heavy");
223  rod->level = 100;
224  comet = create_archetype("spell_comet");
225  object_insert_in_ob(comet, rod);
226 
228 
229  for (tick = 0; tick < NUM_TICKS_TO_RUN; tick++) {
230  if (num_cast < NUM_COMETS_TO_CAST && (tick%1) == 0) {
231  object_remove(rod);
232 
233  /* The idea here on the x is to shuffle the spaces around
234  * a little, as a more typical case is comets
235  * blowing up on different spaces.
236  */
237  object_insert_in_map_at(rod, test_map, NULL, 0, (tick*59)%37, TEST_MAP_SIZE-1);
238 
239  cast_spell(rod, rod, 1, rod->inv, NULL);
240  num_cast++;
241  }
242  process_events();
243  }
244  check_hp("cast_random_comet", hp_row, hp_diag);
245 
246 }
247 END_TEST
248 
249 START_TEST(cast_bunch_comet) {
250  object *comet, *rod;
251  int tick, num_cast = 0;
252  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 },
253  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 };
254 
255  rod = create_archetype("rod_heavy");
256  rod->level = 100;
257  comet = create_archetype("spell_comet");
258  object_insert_in_ob(comet, rod);
259 
261 
262  for (tick = 0; tick < NUM_TICKS_TO_RUN; tick++) {
263  if (num_cast < NUM_COMETS_TO_CAST && (tick%1) == 0) {
264  cast_spell(rod, rod, 1, rod->inv, NULL);
265  num_cast++;
266  }
267  process_events();
268  }
269  check_hp("cast_bunch_comet", hp_row, hp_diag);
270 
271 }
272 END_TEST
273 
274 static Suite *comet_suite(void) {
275  Suite *s = suite_create("comet");
276  TCase *tc_core = tcase_create("Core");
277 
278  /* check by defaults has a 2 second timeout - that isn't
279  * fast enough on my system - a run of 30 comets takes about
280  * 7 seconds. Setting this to 20 is enough, but on a slower
281  * system may not be.
282  */
283  tcase_set_timeout(tc_core, 20);
284 
285  /*setup and teardown will be called before each test in testcase 'tc_core' */
286  tcase_add_checked_fixture(tc_core, setup, teardown);
287 
288  suite_add_tcase(s, tc_core);
289  tcase_add_test(tc_core, cast_one_comet);
290  tcase_add_test(tc_core, cast_bunch_comet);
291  tcase_add_test(tc_core, cast_random_comet);
292 
293  return s;
294 }
295 
296 int main(void) {
297  int nf;
298 
299  Suite *s = comet_suite();
300  SRunner *sr = srunner_create(s);
301 
302  /* Don't want to fork - if we do, we lose the profile (-pg)
303  * compiled information, which is what I used to determine if
304  * things are more efficient.
305  */
306  srunner_set_fork_status(sr, CK_NOFORK);
307 
308  /* Only want to run this once, so don't put it in setup() */
309  cctk_setdatadir(SOURCE_ROOT "lib");
310  init(0, NULL);
311 
312  srunner_set_xml(sr, LOGDIR "/unit/server/comet.xml");
313  srunner_set_log(sr, LOGDIR "/unit/server/comet.out");
314  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
315  nf = srunner_ntests_failed(sr);
316  srunner_free(sr);
317  fprintf(stderr, "Got %"FMT64U" supressions, %"FMT64U" spell merges, %"FMT64U" full tables\n", statistics.spell_suppressions, statistics.spell_merges, statistics.spell_hash_full);
318  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
319 }
GET_MAP_OB
#define GET_MAP_OB(M, X, Y)
Definition: map.h:173
global.h
object_remove
void object_remove(object *op)
Definition: object.c:1806
FLAG_STAND_STILL
#define FLAG_STAND_STILL
Definition: define.h:308
FABS
#define FABS(x)
Definition: define.h:22
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
diamondslots.x
x
Definition: diamondslots.py:15
Statistics::spell_merges
uint64_t spell_merges
Definition: global.h:352
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.c:69
object_new
object * object_new(void)
Definition: object.c:1242
cast_spell
int cast_spell(object *op, object *caster, int dir, object *spell_ob, char *stringarg)
Definition: spell_util.c:1420
liv::maxhp
int16_t maxhp
Definition: living.h:41
liv::hp
int16_t hp
Definition: living.h:40
setup
static void setup(void)
Definition: comet_perf.c:77
NROFATTACKS
#define NROFATTACKS
Definition: attack.h:17
Statistics::spell_hash_full
uint64_t spell_hash_full
Definition: global.h:353
toolkit_common.h
obj::name
sstring name
Definition: object.h:312
object_copy
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:1053
NUM_TICKS_TO_RUN
#define NUM_TICKS_TO_RUN
Definition: comet_perf.c:55
mon
object * mon
Definition: comet_perf.c:75
teardown
static void teardown(void)
Definition: comet_perf.c:120
process_events
void process_events(void)
Definition: server.c:1056
obj::speed_left
float speed_left
Definition: object.h:331
obj::x
int16_t x
Definition: object.h:328
free_map
void free_map(mapstruct *m)
Definition: map.c:1670
obj::speed
float speed
Definition: object.h:330
sproto.h
mapdef
Definition: map.h:324
comet_suite
static END_TEST Suite * comet_suite(void)
Definition: comet_perf.c:274
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
Statistics::spell_suppressions
uint64_t spell_suppressions
Definition: global.h:354
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:281
statistics
struct Statistics statistics
Definition: init.c:100
test_map
mapstruct * test_map
Definition: comet_perf.c:74
obj::y
int16_t y
Definition: object.h:328
obj::stats
living stats
Definition: object.h:371
TEST_MAP_SIZE
#define TEST_MAP_SIZE
Definition: comet_perf.c:54
STARTING_HP
#define STARTING_HP
Definition: comet_perf.c:57
NUM_COMETS_TO_CAST
#define NUM_COMETS_TO_CAST
Definition: comet_perf.c:56
init
void init(int argc, char **argv)
Definition: init.c:1108
main
int main(void)
Definition: comet_perf.c:296
HP_DELTA
#define HP_DELTA
Definition: comet_perf.c:65
object_insert_in_ob
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2820
object_update_speed
void object_update_speed(object *op)
Definition: object.c:1317
FMT64U
#define FMT64U
Definition: compat.h:17
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:2067
START_TEST
START_TEST(cast_one_comet)
Definition: comet_perf.c:194
get_empty_map
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.c:867
obj::resist
int16_t resist[NROFATTACKS]
Definition: object.h:344
obj::level
int16_t level
Definition: object.h:354
obj::inv
struct obj * inv
Definition: object.h:291
check_hp
static void check_hp(const char *test, int hp_row[TEST_MAP_SIZE], int hp_diag[TEST_MAP_SIZE])
Definition: comet_perf.c:124