a649b5fa159288dccf4b2a55640ca9c83140d6a8
[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
23 int nbench_line_count = 0;
24 static int timelimit = 600;
25 static int warmup;
26 static const char *loadfile;
27
28 #define ival(s) strtol(s, NULL, 0)
29
30 /* run a test that simulates an approximate netbench client load */
31 static BOOL run_netbench(struct smbcli_state *cli, int client)
32 {
33         int i;
34         pstring line;
35         char *cname;
36         FILE *f;
37         fstring params[20];
38         const char *p;
39         BOOL correct = True;
40
41         nb_setup(cli, client, warmup);
42
43         asprintf(&cname, "client%d", client+1);
44
45         f = fopen(loadfile, "r");
46
47         if (!f) {
48                 perror(loadfile);
49                 return False;
50         }
51
52 again:
53         while (fgets(line, sizeof(line)-1, f)) {
54                 NTSTATUS status;
55                 double t = end_timer();
56
57                 if (warmup && t >= warmup) {
58                         warmup = 0;
59                         nb_warmup_done();
60                         start_timer();
61                 }
62
63                 if (end_timer() >= timelimit) {
64                         goto done;
65                 }
66
67                 nbench_line_count++;
68
69                 line[strlen(line)-1] = 0;
70
71                 all_string_sub(line,"client1", cname, sizeof(line));
72                 
73                 p = line;
74                 for (i=0; 
75                      i<19 && next_token(&p, params[i], " ", sizeof(fstring));
76                      i++) ;
77
78                 params[i][0] = 0;
79
80                 if (i < 2 || params[0][0] == '#') continue;
81
82                 if (!strncmp(params[0],"SMB", 3)) {
83                         printf("ERROR: You are using a dbench 1 load file\n");
84                         exit(1);
85                 }
86
87                 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0) {
88                         printf("Badly formed status at line %d\n", nbench_line_count);
89                         continue;
90                 }
91
92                 status = nt_status_string_to_code(params[i-1]);
93
94                 DEBUG(9,("run_netbench(%d): %s %s\n", client, params[0], params[1]));
95
96                 if (!strcmp(params[0],"NTCreateX")) {
97                         nb_createx(params[1], ival(params[2]), ival(params[3]), 
98                                    ival(params[4]), status);
99                 } else if (!strcmp(params[0],"Close")) {
100                         nb_close(ival(params[1]), status);
101                 } else if (!strcmp(params[0],"Rename")) {
102                         nb_rename(params[1], params[2], status);
103                 } else if (!strcmp(params[0],"Unlink")) {
104                         nb_unlink(params[1], ival(params[2]), status);
105                 } else if (!strcmp(params[0],"Deltree")) {
106                         nb_deltree(params[1]);
107                 } else if (!strcmp(params[0],"Rmdir")) {
108                         nb_rmdir(params[1], status);
109                 } else if (!strcmp(params[0],"Mkdir")) {
110                         nb_mkdir(params[1], status);
111                 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
112                         nb_qpathinfo(params[1], ival(params[2]), status);
113                 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
114                         nb_qfileinfo(ival(params[1]), ival(params[2]), status);
115                 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
116                         nb_qfsinfo(ival(params[1]), status);
117                 } else if (!strcmp(params[0],"SET_FILE_INFORMATION")) {
118                         nb_sfileinfo(ival(params[1]), ival(params[2]), status);
119                 } else if (!strcmp(params[0],"FIND_FIRST")) {
120                         nb_findfirst(params[1], ival(params[2]), 
121                                      ival(params[3]), ival(params[4]), status);
122                 } else if (!strcmp(params[0],"WriteX")) {
123                         nb_writex(ival(params[1]), 
124                                   ival(params[2]), ival(params[3]), ival(params[4]),
125                                   status);
126                 } else if (!strcmp(params[0],"Write")) {
127                         nb_write(ival(params[1]), 
128                                  ival(params[2]), ival(params[3]), ival(params[4]),
129                                  status);
130                 } else if (!strcmp(params[0],"LockX")) {
131                         nb_lockx(ival(params[1]), 
132                                  ival(params[2]), ival(params[3]), status);
133                 } else if (!strcmp(params[0],"UnlockX")) {
134                         nb_unlockx(ival(params[1]), 
135                                  ival(params[2]), ival(params[3]), status);
136                 } else if (!strcmp(params[0],"ReadX")) {
137                         nb_readx(ival(params[1]), 
138                                  ival(params[2]), ival(params[3]), ival(params[4]),
139                                  status);
140                 } else if (!strcmp(params[0],"Flush")) {
141                         nb_flush(ival(params[1]), status);
142                 } else if (!strcmp(params[0],"Sleep")) {
143                         nb_sleep(ival(params[1]), status);
144                 } else {
145                         printf("[%d] Unknown operation %s\n", nbench_line_count, params[0]);
146                 }
147         }
148
149         rewind(f);
150         goto again;
151
152 done:
153         fclose(f);
154         nb_cleanup(cname);
155
156         if (!torture_close_connection(cli)) {
157                 correct = False;
158         }
159         
160         return correct;
161 }
162
163
164 /* run a test that simulates an approximate netbench client load */
165 BOOL torture_nbench(int dummy)
166 {
167         BOOL correct = True;
168         extern int torture_nprocs;
169         struct smbcli_state *cli;
170         const char *p;
171
172         p = lp_parm_string(-1, "torture", "timelimit");
173         if (p && *p) {
174                 timelimit = atoi(p);
175         }
176
177         warmup = timelimit / 20;
178
179         loadfile =  lp_parm_string(-1, "torture", "loadfile");
180         if (!loadfile || !*loadfile) {
181                 loadfile = "client.txt";
182         }
183
184         if (!torture_open_connection(&cli)) {
185                 return False;
186         }
187
188         nb_setup(cli, -1, warmup);
189         nb_deltree("\\clients");
190
191         nbio_shmem(torture_nprocs);
192
193         printf("Running for %d seconds with load '%s' and warmup %d secs\n", 
194                timelimit, loadfile, warmup);
195
196         signal(SIGALRM, SIGNAL_CAST nb_alarm);
197         alarm(1);
198         torture_create_procs(run_netbench, &correct);
199         alarm(0);
200
201         printf("\nThroughput %g MB/sec\n", 
202                1.0e-6 * nbio_total() / timelimit);
203         return correct;
204 }