summaryrefslogtreecommitdiff
path: root/examples/Mem_Map/IO-tests/test_io.cpp
blob: e209534929e2025b8d9440f57f77d8c4d488d96f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// $Id$

// Test program for different methods of copying files.

#include "ace/OS.h"
#include "ace/Profile_Timer.h"
#include "ace/Get_Opt.h"
#include "ace/Signal.h"
#include "IO_Test.h"

// Name of program.
static char *program_name;

// Name of default input file.
static char *input_filename = "/usr/dict/words";

// Name of default output file.
static char *output_filename = "/tmp/foo";

// Check if removing output file upon completion...
static int remove_output = 1;

// Count of the number of iterations to run the tests.
static int iteration_count = 100;

// Profiler used to keep track of file I/O time.
static ACE_Profile_Timer tm;

// Explain usage and exit.

static void 
print_usage_and_die (void)
{
  cerr << "usage: " 
       << program_name 
       << " [-i input_file] [-o output_file] [-n iteration_count] [-r]\n";
  ACE_OS::exit (1);
}

// Clean up the output file on exit from a signal.

extern "C" void
cleanup (int = 0)
{
  if (remove_output)
    ACE_OS::unlink (output_filename);
  ACE_OS::exit (0);
}

// Set up the program name used in error messages.

static void
set_program_name (char name[])
{
  if ((name = strrchr (name, '/')) == 0)
    program_name = name;
  else
    program_name = name + 1;
}

// Parse the command-line arguments and set options.

static void
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, "i:n:o:r");

  for (int c; ((c = get_opt ()) != -1); )
    switch (c)
    {
    case 'i':
      input_filename = get_opt.optarg;
      break;
    case 'n':
      iteration_count = ACE_OS::atoi (get_opt.optarg);
      break;
    case 'o':
      output_filename = get_opt.optarg;
      break;
    case 'r':
      remove_output = 0;
      break;
    default:
      print_usage_and_die ();
      break;
  }
}

// Vector of pointers to derived classes that inherit from IO_Test base class.

static IO_Test *test_vector[100];

static void
run_tests (int iterations, FILE *input_fp, FILE *output_fp)
{
  // If HP/UX didn't suck so badly we could initialize in the global scope...
  test_vector[0] = new Stdio_Test ("Stdio_Test", tm);
  test_vector[1] = new Block_Fread_Fwrite_Test ("Block_Fread_Fwrite_Test", tm);
  test_vector[2] = new Block_Read_Write_Test ("Block_Read_Write_Test", tm);
  test_vector[3] = new Mmap1_Test ("Mmap1_Test", tm);
  test_vector[4] = new Mmap2_Test ("Mmap2_Test", tm);
  // test_vector[5] = new Slow_Read_Write_Test ("Slow"Read_Write_Test", tm)
  test_vector[5] = (IO_Test *) 0;

  for (int i = 0; test_vector[i] != 0; i++)
    {
      if (ACE_OS::ftruncate (fileno (output_fp), 0) == -1)
	::perror ("ftruncate");

      cerr << "--------------------\n" 
	   << "starting " << test_vector[i]->name () << " for " << iterations
	   << " iteration(s):\n";

      if (test_vector[i]->run_test (iterations, input_fp, output_fp) == -1)
	::perror (test_vector[i]->name ());

      ACE_Profile_Timer::ACE_Elapsed_Time et;
      tm.elapsed_time (et);

      cerr << "wallclock time = " << et.real_time
           << ", user time = " << et.user_time
	   << ", system time = " << et.system_time << endl;

      delete test_vector[i];
    }

  cerr << "--------------------\n";
}

int
main (int argc, char *argv[])
{
  FILE *input_fp;
  FILE *output_fp;

  set_program_name (argv[0]);
  parse_args (argc, argv);

  ACE_Sig_Action sa ((ACE_SignalHandler) cleanup, SIGINT);

  if ((input_fp = ACE_OS::fopen (input_filename, "r")) == 0)
    ACE_OS::perror (input_filename), ACE_OS::exit (1);

  ACE_OS::unlink (output_filename);

  if ((output_fp = ACE_OS::fopen (output_filename, "w+")) == 0)
    ACE_OS::perror (output_filename), ACE_OS::exit (1);

  run_tests (iteration_count, input_fp, output_fp);

  if (ACE_OS::fclose (input_fp) == -1 || ACE_OS::fclose (output_fp) == -1)
    ACE_OS::perror ("fclose"), ACE_OS::exit (1);

  cleanup ();
  return 0;
}