#!/usr/bin/perl use strict; use warnings; use autodie; use Bench; use Fcntl qw(:flock); unless (@ARGV == 3) { die "Usage: $0 \n"; } my ($method, $file, $str) = @ARGV; bench sub { if ($method eq 'print') { open my($fh), ">>", $file; for (1..100_000) { print $fh "$str: $_\n"; } close $fh; } elsif ($method eq 'open+print+close') { for (1..100_000) { open my($fh), ">>", $file; print $fh "$str: $_\n"; close $fh; } } elsif ($method eq 'seek+print') { open my($fh), ">>", $file; for (1..100_000) { seek $fh, 0, 2; print $fh "$str: $_\n"; } close $fh; } elsif ($method eq 'flock+print') { open my($fh), ">>", $file; for (1..100_000) { flock $fh, LOCK_EX; print $fh "$str: $_\n"; flock $fh, LOCK_UN; } close $fh; } }; #### 001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001: 1 001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001: 2 ... #### 002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002: 1 002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002002: 2 ...