r5298: - got rid of pstring.h from includes.h. This at least makes it a bit
[jra/samba/.git] / source4 / torture / nbench / nbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester - NBENCH test
4    Copyright (C) Andrew Tridgell 1997-2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "pstring.h"
24
25 int nbench_line_count = 0;
26 static int timelimit = 600;
27 static int warmup;
28 static const char *loadfile;
29
30 #define ival(s) strtol(s, NULL, 0)
31
32 /* run a test that simulates an approximate netbench client load */
33 static BOOL run_netbench(struct smbcli_state *cli, int client)
34 {
35         int i;
36         pstring line;
37         char *cname;
38         FILE *f;
39         fstring params[20];
40         const char *p;
41         BOOL correct = True;
42
43         nb_setup(cli, client);
44
45         asprintf(&cname, "client%d", client+1);
46
47         f = fopen(loadfile, "r");
48
49         if (!f) {
50                 perror(loadfile);
51                 return False;
52         }
53
54 again:
55         while (fgets(line, sizeof(line)-1, f)) {
56                 NTSTATUS status;
57
58                 nbench_line_count++;
59
60                 line[strlen(line)-1] = 0;
61
62                 all_string_sub(line,"client1", cname, sizeof(line));
63                 
64                 p = line;
65                 for (i=0; 
66                      i<19 && next_token(&p, params[i], " ", sizeof(fstring));
67                      i++) ;
68
69                 params[i][0] = 0;
70
71                 if (i < 2 || params[0][0] == '#') continue;
72
73                 if (!strncmp(params[0],"SMB", 3)) {
74                         printf("ERROR: You are using a dbench 1 load file\n");
75                         exit(1);
76                 }
77
78                 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0) {
79                         printf("Badly formed status at line %d\n", nbench_line_count);
80                         continue;
81                 }
82
83                 status = nt_status_string_to_code(params[i-1]);
84
85                 DEBUG(9,("run_netbench(%d): %s %s\n", client, params[0], params[1]));
86
87                 if (!strcmp(params[0],"NTCreateX")) {
88                         nb_createx(params[1], ival(params[2]), ival(params[3]), 
89                                    ival(params[4]), status);
90                 } else if (!strcmp(params[0],"Close")) {
91                         nb_close(ival(params[1]), status);
92                 } else if (!strcmp(params[0],"Rename")) {
93                         nb_rename(params[1], params[2], status);
94                 } else if (!strcmp(params[0],"Unlink")) {
95                         nb_unlink(params[1], ival(params[2]), status);
96                 } else if (!strcmp(params[0],"Deltree")) {
97                         nb_deltree(params[1]);
98                 } else if (!strcmp(params[0],"Rmdir")) {
99                         nb_rmdir(params[1], status);
100                 } else if (!strcmp(params[0],"Mkdir")) {
101                         nb_mkdir(params[1], status);
102                 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
103                         nb_qpathinfo(params[1], ival(params[2]), status);
104                 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
105                         nb_qfileinfo(ival(params[1]), ival(params[2]), status);
106                 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
107                         nb_qfsinfo(ival(params[1]), status);
108                 } else if (!strcmp(params[0],"SET_FILE_INFORMATION")) {
109                         nb_sfileinfo(ival(params[1]), ival(params[2]), status);
110                 } else if (!strcmp(params[0],"FIND_FIRST")) {
111                         nb_findfirst(params[1], ival(params[2]), 
112                                      ival(params[3]), ival(params[4]), status);
113                 } else if (!strcmp(params[0],"WriteX")) {
114                         nb_writex(ival(params[1]), 
115                                   ival(params[2]), ival(params[3]), ival(params[4]),
116                                   status);
117                 } else if (!strcmp(params[0],"Write")) {
118                         nb_write(ival(params[1]), 
119                                  ival(params[2]), ival(params[3]), ival(params[4]),
120                                  status);
121                 } else if (!strcmp(params[0],"LockX")) {
122                         nb_lockx(ival(params[1]), 
123                                  ival(params[2]), ival(params[3]), status);
124                 } else if (!strcmp(params[0],"UnlockX")) {
125                         nb_unlockx(ival(params[1]), 
126                                  ival(params[2]), ival(params[3]), status);
127                 } else if (!strcmp(params[0],"ReadX")) {
128                         nb_readx(ival(params[1]), 
129                                  ival(params[2]), ival(params[3]), ival(params[4]),
130                                  status);
131                 } else if (!strcmp(params[0],"Flush")) {
132                         nb_flush(ival(params[1]), status);
133                 } else if (!strcmp(params[0],"Sleep")) {
134                         nb_sleep(ival(params[1]), status);
135                 } else {
136                         printf("[%d] Unknown operation %s\n", nbench_line_count, params[0]);
137                 }
138                 
139                 if (nb_tick()) goto done;
140         }
141
142         rewind(f);
143         goto again;
144
145 done:
146         fclose(f);
147
148         if (!torture_close_connection(cli)) {
149                 correct = False;
150         }
151         
152         return correct;
153 }
154
155
156 /* run a test that simulates an approximate netbench client load */
157 BOOL torture_nbench(void)
158 {
159         BOOL correct = True;
160         extern int torture_nprocs;
161         struct smbcli_state *cli;
162         const char *p;
163
164         p = lp_parm_string(-1, "torture", "timelimit");
165         if (p && *p) {
166                 timelimit = atoi(p);
167         }
168
169         warmup = timelimit / 20;
170
171         loadfile =  lp_parm_string(-1, "torture", "loadfile");
172         if (!loadfile || !*loadfile) {
173                 loadfile = "client.txt";
174         }
175
176         if (!torture_open_connection(&cli)) {
177                 return False;
178         }
179
180         if (!torture_setup_dir(cli, "\\clients")) {
181                 return False;
182         }
183
184         nbio_shmem(torture_nprocs, timelimit, warmup);
185
186         printf("Running for %d seconds with load '%s' and warmup %d secs\n", 
187                timelimit, loadfile, warmup);
188
189         signal(SIGALRM, nb_alarm);
190         alarm(1);
191         torture_create_procs(run_netbench, &correct);
192         alarm(0);
193
194         smbcli_deltree(cli->tree, "\\clients");
195
196         printf("\nThroughput %g MB/sec\n", nbio_result());
197         return correct;
198 }